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);
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 2 years ago.
Improve this question
I want to have an php check for easy passwords patterns like
123456
123456789
abcde
98764321
101010
202020
Is it possible to have a check like this that doesn't rely in maintaining an array of pre-defined strings ?
Just iterate to each letter and check if the next one is same, one less or one more as current. So you can create a score of uniqueness.
For last case you can check if there are duplicate characters and remove from score, too.
$passwords = ['123456', '123456789', 'abcde', '98764321', '101010', '202020', 'xhdgszu'];
foreach ($passwords as $password) {
$score = $length = strlen($password);
$chars = str_split($password);
for ($i = 0; $i < $length - 1; $i++) {
$currentChar = $chars[$i];
$nextChar = $chars[$i + 1];
if ($currentChar === $nextChar
|| ord($currentChar) - 1 === ord($nextChar)
|| ord($currentChar) + 1 === ord($nextChar)
) {
$score--;
continue;
}
}
$unique = array_unique($chars);
$score -= $length - count($unique);
echo "{$password}: {$score} / {$length} ", ($score < $length) ? 'Bad' : 'Good', PHP_EOL;
}
123456: 1 / 6 Bad
123456789: 1 / 9 Bad
abcde: 1 / 5 Bad
98764321: 2 / 8 Bad
101010: -3 / 6 Bad
202020: 2 / 6 Bad
xhdgszu: 7 / 7 Good
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 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