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 ?
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;
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
$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.
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;
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
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;
}
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++;
}
Assuming $foo is an integer:
$answer = (int) (floor(($foo + 5) / 6) * 6)
For micro-optimisation freaks:
if ($num % 6 != 0)
$num += 6 - $num % 6;
More evaluations of %, but less branching/looping. :-P
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;
?>
result = initial number + (6 - initial number % 6)
Related
I got this Exercise :
Create a while-loop that subtracts 5.23 from the number 526 until the number is between (not equal to) 41 and 51. Answer with the final result as a float, rounded to 2 decimals.
What I doing wrong ?
$number = 1;
$sum = 526;
while ($number > 41 && $number < 51)
{
$sum-=5.23;
$number++;
}
Your loop never actually runs as 1 is never > 41 && < 51
$sum = 526;
while (1)
{
$sum -= 5.23;
if ( $sum > 41 && $sum < 51 ) { break; }
}
$ANSWER = sprintf('%2.2f', $sum);
echo $ANSWER;
Writing some code connected with factorials (counting sum of numbers of factorial) I noticed that 13! modulus 10 equals 4.
function fact($n)
{
if ($n == 0) return 1;
return $n * fact($n-1);
}
function sum_num($n)
{
$sum = 0;
while ($n > 0)
{
$sum = $sum + ($n % 10);
$n = floor($n/10);
}
return $sum;
}
$n = 13;
$buff = fact($n);
echo $n."! = ".$buff;
echo "<br>";
echo "Summ ".$n."! = ".sum_num($buff);
Output is:
13! = 6227020800
Summ 13! = 31
But Summ should be 27. I begun search and found that on the first step I am getting 4 instead 0.
6227020800 % 10 = 4
And I don't understand why?
6227020800 requires 33 bits. The most siginficant bit gets truncated and you get you the number 1932053504 (modulo 10 of which is 4).
For arbitrary precision math use bc* functions, e.g. bcmod(6227020800, 10).
May be you should use bcmath php module instead for this operations.
http://php.net/manual/en/book.bc.php
Just take care that this module uses strings as inputs.
I'm pretty sure, you use a 32-bit version. Here the max value is 4.294.967.296.
Okay i will have a number from my database. This could be 3, 15 , 138 etc. Basically any number.
Now if the number is not a multiple of 6 i want to find out how many more until it becomes a multiple of 6.
So for instance if my number is 4, i want it to say you need 2 more to reach a multiple of 6.
How can i achieve this? Also when giving an answer could you please explain how the formula works.
I have tried this which someone suggested
$number = 4;
if($number % 6 != 0) {
echo $number += 6 - ($number % 6);
}
But this just prints 6 out
Using += modifies $number by the value returned
echo $number += 6 - ($number % 6);
Results in: 4 += 6 - 4 or $number = 6
Should be
echo $number = 6 - ($number % 6);
Results in: $number = 6 - 4 or $number = 2
Your numbers are backwards:
$number = 4;
if(6 % $number != 0) {
echo (6 % $number) - 6;
}
https://3v4l.org/1V5Rb
I have this if statement:
if($_GET["angle_1"] > 39) {
$markers["###ANGLE###"] = "45";
} elseif($_GET["angle_1"] > 29 && $_GET["angle_1"] < 40) {
$markers["###ANGLE###"] = "35";
} elseif($_GET["angle_1"] < 30) {
$markers["###ANGLE###"] = "25";
} else {
$markers["###ANGLE###"] = "45";
}
Is there a better / simpler way to do this check, f.x. with round that will round the integer to the nearest 5, i.e. 28 -> 25 or 34 -> 35 etc. and in that, if the integer is less than 25, it will always be 25 and if the integer is higher than 45, it will always be 45 and again if the integer is between 30 and 40 it will always be 35.
That returned value will be used to display an image.
EDIT:
I have 3 images: image_25, image_35 and image_45, therefore the need to round.
Let say $x has the number:
$x = 39;
If you want the closest multiple of 5 (39 --> 40):
$x = round($x / 5) * 5;
If you want to round up (36 --> 40):
$x = ceil($x / 5) * 5;
If you want to round down (39 --> 35):
$x = floor($x / 5) * 5;
After defining $x, you can use the following to make sure its in the 25-45 range:
$x = ($x > 45) ? 45 : ($x < 25) ? 25 : $x;
Give this a try:
$var = 5 * round($n / 5);
Taken from here
I have a simple round function. It rounds to an even number. I want to make sure that number is divisible by 16. Anyone know an easy way to round the number to the nearest number divisible evenly by 16?
$num=round(480/$other_num); //will output some number.
$num = 39;
$num = round($num / 16) * 16; // 32
Perhaps you can bit-and the number with 0xf and add 1?
the easy way seems to be divide by 16, then use the "classical round" and multiply back by 16.
$num=round(480/16)*16;
function round16($num) {
if ($num % 16 == 0) return $num;
$num2 = $num;
$remain = 0;
do {
$remain = --$num % 16;
} while ($remain != 0 && $num >= $num2 - 7);
if ($remain == 0) return $num;
do {
$remain = ++$num2 % 16;
} while ($remain != 0);
return $num2;
}
Probably not the most efficient way to do it.