checking if a number is divisible by 6 PHP

PhpModulus

Php Problem Overview


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 ?

Php Solutions


Solution 1 - Php

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

The [modulus][1] 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;

[1]: http://php.net/manual/en/language.operators.arithmetic.php "mod"

Solution 2 - Php

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

Solution 3 - Php

$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.

Solution 4 - Php

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;

Solution 5 - Php

I see some of the other answers calling the modulo twice.

My preference is not to ask php to do the same thing more than once. For this reason, I cache the remainder.

Other devs may prefer to not generate the extra global variable or have other justifications for using modulo operator twice.

Code: (Demo)

$factor = 6;
for($x = 0; $x < 10; ++$x){  // battery of 10 tests
    $number = rand( 0 , 100 );
    echo "Number: $number Becomes: ";
    if( $remainder = $number % $factor ) {  // if not zero
        $number += $factor - $remainder;  // use cached $remainder instead of calculating again
    }
    echo "$number\n";
}

Possible Output:

Number: 80 Becomes: 84
Number: 57 Becomes: 60
Number: 94 Becomes: 96
Number: 48 Becomes: 48
Number: 80 Becomes: 84
Number: 36 Becomes: 36
Number: 17 Becomes: 18
Number: 41 Becomes: 42
Number: 3 Becomes: 6
Number: 64 Becomes: 66

Solution 6 - Php

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;
}

Solution 7 - Php

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++;
}

Solution 8 - Php

Assuming $foo is an integer:

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

Solution 9 - Php

For micro-optimisation freaks:

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

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

Solution 10 - Php

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;
?>

Solution 11 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUtku DalmazView Question on Stackoverflow
Solution 1 - PhpZoFreXView Answer on Stackoverflow
Solution 2 - PhpBandi-TView Answer on Stackoverflow
Solution 3 - PhpPonkadoodleView Answer on Stackoverflow
Solution 4 - PhpzneakView Answer on Stackoverflow
Solution 5 - PhpmickmackusaView Answer on Stackoverflow
Solution 6 - PhpMitch WheatView Answer on Stackoverflow
Solution 7 - PhpJames SimpsonView Answer on Stackoverflow
Solution 8 - PhpmartinrView Answer on Stackoverflow
Solution 9 - PhpChris Jester-YoungView Answer on Stackoverflow
Solution 10 - PhpPrasoon SauravView Answer on Stackoverflow
Solution 11 - PhpRudyView Answer on Stackoverflow