Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What math can you use in this following example:
I want to divide a number by 2, but the result should always be a whole number.That means I cannot divide by 2 when the number is odd.
For ex.
when I divide 5 by 2.I want the output as 3 or 2, but not 2.5
Use the math functions ceil or floor:
$number = 5 / 2;
echo ceil( $number); // Outputs 3
echo floor( $number); // Outputs 2
$number = 4 / 2;
echo ceil( $number); // Outputs 2
echo floor( $number); // Outputs 2
Demo
$n = 5;
floor($n / 2); // 2
ceil($n / 2); // 3
$n = 6;
floor($n / 2); // 3
ceil($n / 2); // 3
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Imagine you want to add the sum of 2 dice so the output looks like this in 6 lines:
2 3 4 5 6 7 3 4 5 6 7 8 9 4 5 6 7 8 9 10 11 5 6 ... 7 8... 8 9...
By only programming a single loop ?
I tried to do it with nested loops but could figure out the logic for it.
You can do with one while loop and two variables for two dices.
<?php
$x = 1;
$y = 1;
while ( $x <= 6 ) {
echo $x + $y;
$y++;
if ( $y > 6 ) {
$y = 1;
$x++;
}
}
It's essentially a mechanical counter. When the first wheel completes a rotation, it resets, and advances the second wheel. Except in this case they are dice. When $y is greater than six, it resets and advances $x.
You would use similar code when dealing with dates, advancing months when the days tick over etc.
The code would be simpler using two loops.
for ( $x = 1; $x <= 6; $x++ ) {
for ( $y = 1; $y <= 6; $y++ ) {
echo $x + $y;
}
}
As this was not allowed, the while loop is the 'first wheel' and the if statement determines when to reset and advance the second.
https://www.hackerrank.com/ is good if you want to practice solving coding problems
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to get a number between 1 to 6 with more chances to be close to 1.
i have tried this:
<li>{{Faker\Factory::create()->biasedNumberBetween($min = 10, $max = 20, $function = 'unbiased')}}</li>
What i am trying to do is to generate a number from 1 to 6 rand(1,6); but make the numbers be closer to one as the lower numbers will have more weight than the others.
Something like this ?
<?php
function weightedRand($min, $max, $weightedMax) {
$arr = array();
for($i = 0; $i < 10; $i++) {
$arr[] = rand($min, $weightedMax);
}
$arr[] = rand($min, $max);
return $arr[rand(0,10)];
}
echo weightedRand(1,6, 3);
?>
numbers below 4 will now be more likely than numbers above :)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
With PHP I need to add all the digits in a number.
Example : if I have 142 it will be 1+4+2 = 7
its should be 1 to 9 if not its should plus again 11 will be 1+1 = 2
Here is a function, based on Dave Chen's answer :
function sumDigits($input) {
$sum = array_sum(str_split($input));
if($sum > 9) {
$sum = sumDigits($sum);
}
return $sum;
}
This code :
echo sumDigits("1239");
Outputs: 6
1 + 2 + 3 + 9 = 15 = 1 + 5 = 6
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to create a php code to execute a list of divisions, i have tried to put a code together but being a novice, it's not quite working.
<?php
$division(6,true);
for ($i = 1; $i <= 6; $i++) {
if(($division / $i) == $result)
{echo "<p>$division &division; $i = $result</p>";}
}
?>
$division is meant to be the main number, $division will get divided by every number upto and including $division.
$i should list a string of number to from 1 to 6 in this case.
then $divide ÷ $i = $result.
I am hoping for this to print out the list as shown below.
6 ÷ 1 = 6
6 ÷ 2 = 3
6 ÷ 3 = 2
6 ÷ 4 = 1.5
6 ÷ 5 = 1.2
try this...? I'm not sure what the IF statement is trying to accomplish, though
<?php
division(6);
function division($num)
{
for ($i = 1; $i < $num; $i++)
{
$result = $num / $i;
//if(($division / $i) == $result)
//{
echo "$num / $i = $result<br/>";
//}
}
}
?>
output:
6 / 1 = 6
6 / 2 = 3
6 / 3 = 2
6 / 4 = 1.5
6 / 5 = 1.2
edit: please note that % is usually the modulus operator in programming.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to divide an integer value into 3 parts using php
for eg: I have a value 9 the answer is like 3 + 3 + 3
if i have value 10 the ans like 3 + 3 + 4 or something like that
You can use a modulus function.
<?php
$yourInt=10;
$remainder=$yourInt % 3;
$third=floor($yourInt/3);
$lastBit=$third+$remainder;
echo "The numbers are $third + $third + $lastBit.";
?>
Output:
The numbers are 3 + 3 + 4.
If you have a value that is >= 3 you can do the following:
$number = 10;
$number1and2 = floor($number/3);
$number3 = $number - (2*$number1and2);
$num = 11;
$d = [$num/3,$num/3,$num/3];
$round = array_map('round', $d);
list($first, $second, $third) = $round;
$round = (is_float($num/3)) ? $num-(round($num/3)*3) : 0;
echo $first.' '.$second.' '.($third += (is_float($num/3)) ? $num-(round($num/3)*3) : 0);