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
Related
How to reduce a number to single digit by adding its individual digits recursively :
Example 1 : $n = 99999 >> 45 >> 9
Example 2 : $n = 444444 >> 24 >> 6
Example 3 : $n = 8888888888888885 >> 125 >> 8;
then get equal to at last we want to get single digit.
You can use array_sum and str_split into a while loop until the final value of $n has the length equal to 1.
$n = 4444;
while (strlen($n) > 1) {
$n = array_sum(str_split($n));
}
var_dump($n);
Without array_sum and str_split you can use something like:
$n = '4444';
while (strlen($n) > 1) {
$s = 0;
for ($i = 0; $i < strlen($n); $i++) {
$s += $n[$i];
}
$n = (string) $s;
}
var_dump($n);
You can calculate this in a much more simple and elegant way, let me try to explain. For example if you have the number 53, you can divide it by 9 and it’s remainder will be it’s reduced number. Don’t ask me how I figured this out, I was just tinkering with numbers. So what you can do is use modulus, (53 % 9 = 8!) (517 % 9 = 4!). Perfect right? Almost, if the number is a multiple of 9 like 45 for example if you “modulus” it by 9 you will receive it’s remaineder which is 0 and we would expect 9 because 45 reduced to a single digit is 9. So you can just make a quick and easy else if statement checking for an output of 0, and if it’s 0 just return 9. Done! Whatever number you out in from 1 to infinty it will reduce it perfectly. Hope this helps :)
I want to distribute an amount among some users equally.If amount is not divisible equally then all other member will get equal amount expect the last member who will get rest of the money.Below is what i have tried
$number = $_POST['number'];
$noOfTime = $_POST['no_of_time'];
$perHead = ceil($number / $noOfTime);
for ($i = 1; $i <= $noOfTime; $i++) {
if ($i == $noOfTime) {
echo $perHead * $noOfTime - $number;
} else {
echo $perHead;
}
}
Here if number is 7 and member are 4 the first 3 member will the 2 and the last will get 1. Like 2,2,2,1.
But this logic seems to be not working for all cases.
Please help.Thanks.
I think it should help you.
$no = 22;
$users = 8;
// count from 0 to $users number
for ($i=0;$i<$users;$i++)
// if the counting reaches the last user AND $no/$users rests other than 0...
if ($i == $users-1 && $no % $users !== 0) {
// do the math rounding fractions down with floor and add the rest!
echo floor($no / $users) + ($no % $users);
} else {
// else, just do the math and round it down.
echo floor($no / $users)." ";
}
OUTPUTS:
2 2 2 2 2 2 2 8
EDIT: I nested an if verification so the logic won't fail even if users are 1 or 2. And since it received more upvotes, I commented the code to make it more clear.
I've been trying to solve Project Euler problem 1 and I feel I'm missing something fundamental. Would you please help me fix my bug?
I tried this first:
<?php
/*
** Project Euler Problem 1
** If we list all the natural numbers below 10 that are multiples of 3 or 5,
** we get 3, 5, 6 and 9. The sum of these multiples is 23.
** Find the sum of all the multiples of 3 or 5 below 1000.
*/
$numberOne = 3;
$numberTwo = 5;
$endingIndex = 1000;
$multiplesOfNumbers = array();
for ($index = 1; $index <= $endingIndex; $index++) {
if ($index % $numberOne == 0 && $index % $numberTwo == 0) {
$multipleOfNumbers[] = $index;
}
}
echo $multiplesOfNumbers;
?>
Output:
Array
So I tried to do it with array_push instead, like this:
<?php
/*
** Project Euler Problem 1
** If we list all the natural numbers below 10 that are multiples of 3 or 5,
** we get 3, 5, 6 and 9. The sum of these multiples is 23.
** Find the sum of all the multiples of 3 or 5 below 1000.
*/
$numberOne = 3;
$numberTwo = 5;
$endingIndex = 1000;
$multiplesOfNumbers = array();
for ($index = 1; $index <= $endingIndex; $index++) {
if ($index % $numberOne == 0 && $index % $numberTwo == 0) {
// $multipleOfNumbers[] = $index;
array_push($multiplesOfNumbers, $index);
}
}
echo $multiplesOfNumbers;
?>
The output is the same. What am I missing?
Try this way:
print_r($multiplesOfNumbers);
echo will not print the array elements. use print_r instead
You have to loop through the array to get the values of the array (using a foreach) or use something like var_dump() or print_r() to echo the array.
I've putted a script together a bit with array's and stuff.
Now in the end, it checks for a score with checkboxes and divides it by ten.
Then with number format i round it off two 2 decimals, it looks like this.
$number = $score / $total;
$number = $number * 10;
$number = number_format(round($number,2),2,',','.');
echo "The number is: $number <br/>";
Then later on, i'm doing this.
if($number < 4 && $number > 0)
echo 'You're number is between zero and 4';
else if($number > 6 && $number < 4)
echo ' You're number is between 4and 6';
else if($number < 8 && $number > 6)
echo' You're number is between 6 and 8';
else if($number < 10 && $number > 8)
You're number is between 8 and 10';
Now, if the number is like, 0.50 or 1.50 or 2 or 5.50 it's shows the text i've provided.
However, if the number is like 4,13 or 7,85 or 9,13 it doesn't.
Searched now quite a while but can't figure it out.
Do you people see a solution?
Hope i was clear enough!
Thanks in advance.
Is this what you're looking for?
<?php
$score = 7; //Example values
$total = 32;
$number = $score / $total;
$number = $number * 10;
$number = round($number,2);
echo 'The number is: ',number_format($number,2,'.',','),' <br/>';
if($number < 4 && $number > 0)
echo 'Your number is between zero and 4';
else if($number < 6 && $number > 4)
echo 'Your number is between 4 and 6';
else if($number < 8 && $number > 6)
echo 'Your number is between 6 and 8';
else if($number < 10 && $number > 8)
echo 'Your number is between 8 and 10';
?>
The following statement will never validate true:
if($number > 6 && $number < 4)
Do you know any number that's higher than six and smaller than four?
The code also contains numerous syntax errors, for example:
echo 'You're number is between zero and 4';
The apostrophe needs to be escaped using a backslash:
echo 'You\'re number is between zero and 4';
Or use double quotes for strings containing apostrophes:
echo "You're number is between zero and 4";
Finally, grammar whise, you're should be your :)
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)