tags:

views:

453

answers:

10

Hi,

I want to check if a number is divisible by 6 and if not i need to increase it until it becomes divisible.

how can i do that ?

Thanks

A: 

Use the Mod % (modulus) operator

if ($x % 6 == 0) return 1;


function nearest_multiple_of_6($x) {
    if ($x % 6 == 0) return $x;    

    return (($x / 6) + 1) * 6;
}
Mitch Wheat
+3  A: 

So you want the next multiple of 6, is that it?

You can divide your number by 6, then ceil it, and multiply it again:

$answer = ceil($foo / 6) * 6;
zneak
That's an insightful algorithm.
Karl
Boo! Why use floating-point when you don't have to! :)It is also non-portable, because int/int division in many languages will give a (truncated) integer result, so ceil will never see a need to round it up to the next integer value, therefore the expression will give the wrong result.
Bandi-T
I second that, using floating point for what is essentially an integer problem is horrible and may not give the right answer in all situations.
ZoFreX
I think choosing not to use the modulus operator in this instance is a bit weird...
Bryan M.
yes i realized mod operator is better thx
Ahmet vardar
+1  A: 

Simply run a while loop that will continue to loop (and increase the number) until the number is divisible by 6.

while ($number % 6 != 0) {
    $number++;
}
James Simpson
it can be done without looping...
Mitch Wheat
The naive method saves programmer time, and my boss/client is always on about that.
Karl
If the programmer does not know about the % operator, his time is not worth saving.
Seva Alekseyev
A: 

For micro-optimisation freaks:

if ($num % 6 != 0)
    $num += 6 - $num % 6;

More evaluations of %, but less branching/looping. :-P

Chris Jester-Young
+1  A: 
$num += (6-$num%6)%6;

no need for a while loop! Modulo (%) returns the remainder of a division. IE 20%6 = 2. 6-2 = 4. 20+4 = 24. 24 is divisible by 6.

Wallacoloo
Sure about that? :P
ZoFreX
I think this is wrong - you are adding the remainder to the number. So if the number was 8, remainder is 2, you add the remainder 2 to 8, to get the result: 10. 10 is not divisible by 6
Bandi-T
I fixed that typo literally 5 seconds after I posted this...
Wallacoloo
This will add 6 to the number if it is already divisible by 6.
Justin Smith
ahh, good catch. didn't even consider that.
Wallacoloo
Ok, this is correct now.
Bandi-T
A: 

Why don't you use the Modulus Operator?

Try this:

while ($s % 6 != 0) $s++;

Or is this what you meant?

<?

 $s= <some_number>;
 $k= $s % 6;

 if($k !=0)    $s=$s+6-$k;
?>
Prasoon Saurav
I think this is wrong - you are adding the remainder to the number. So if the number was 8, remainder is 2, you add the remainder 2 to 8, to get the result: 10. 10 is not divisible by 6.
Bandi-T
Yeah I had missed something. Corrected now.
Prasoon Saurav
+7  A: 

Simple:

if($number %6 != 0) {
  $number += 6 - ($number % 6);
}

The modulus operator gives the remainder of the division, so $number % 6 is the amount left over when dividing by 6. This will be faster than doing a loop and continually rechecking.

If decreasing is acceptable then this is even faster:

$number -= $number % 6;
ZoFreX
+1  A: 
if ($variable % 6 == 0) {
    echo 'This number is divisible by 6.';
}:

Make divisible by 6:

$variable += (6 - ($variable % 6)) % 6; // faster than while for large divisors
Bandi-T
The second % operator there is very clever. Probably faster than mine :(
ZoFreX
Thank you! :) Votes are welcomed!
Bandi-T
A: 

result = initial number + (6 - initial number % 6)

Rudy
This isn't quite correct. Subtract the remainder from 6 before adding it!
ZoFreX
A: 

Assuming $foo is an integer:

$answer = (int) (floor(($foo+5)/6)*6)
martinr