Jump to content


Photo

Multiples of Integers


  • Please log in to reply
3 replies to this topic

#1 Jarin

Jarin
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 14 April 2006 - 04:29 AM

Well, considering I'm not very math savvy, I need a bit of help. I'm trying to determine whether one integer is evenly divisible into another integer, IE is 1033343 a multiple of 3, is 105 a multiple of 5, etc. How would one go about determining this in a memory-conservative manner?

Thanks in advance.


#2 KrisNz

KrisNz
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • LocationAuckland, New Zealand

Posted 14 April 2006 - 04:38 AM

[!--quoteo(post=364686:date=Apr 14 2006, 02:29 PM:name=Jarin)--][div class=\'quotetop\']QUOTE(Jarin @ Apr 14 2006, 02:29 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Well, considering I'm not very math savvy, I need a bit of help. I'm trying to determine whether one integer is evenly divisible into another integer, IE is 1033343 a multiple of 3, is 105 a multiple of 5, etc. How would one go about determining this in a memory-conservative manner?

Thanks in advance.
[/quote]
use the '%' operator which gives you the remainder of a division. e.g $i = 5 % 5; #$i=0;

#3 hadoob024

hadoob024
  • Members
  • PipPipPip
  • Advanced Member
  • 192 posts

Posted 14 April 2006 - 04:39 AM

You could probably just use the Modulus operator. This basically divides 2 numbers and tells you what the remainder is. So you could test and see if it's equal to 0, and if so, then you know that one number is a multiple of another. Here's an example using the numbers from your example:

$a = 1033343;
$b = 3;
$result = $a % $b;
if ($result == 0)
       echo 'the two numbers are evenly divisible';
else
       echo 'the two numbers are not evenly divisible';


#4 Jarin

Jarin
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 14 April 2006 - 03:17 PM

Never knew that operator existed. Thanks a lot guys, much appreciated!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users