Divide a number into equal parts and store their cumulative sum - php

Simple question, how do I get every option when dividing a number? For example:
24 by 6 returns 6, 12, 18, 24
24 by 4 returns 4, 8, 12, 16, 20, 24
24 by 5 returns false
I've got a number in my database, for example 2, and my counter, for example 14. That means every time my counter hits the second number, I want to fire my event. So I thought, if I have the solutions 2, 4, 6, etc, and my counter is equal to one of the solutions, I can fire my event.

It's rather trivial to make.
<?php
/**
* #param int $number The beginning number
* #param int $divider The number dividing by
*
* #return array
* #throws Exception In case $number is not divisible by $divider
*/
function get_number_sequence($number, $divider) {
//In case $number is not divisible by $divider, throw an Exception.
if ($number % $divider !== 0) {
throw new Exception("$number is not divisible by $divider");
}
//Return an array from $divider to $number in steps of $divider.
$result = range($divider, $number, $divider);
return $result;
}
/*
* Testing begins
*/
try {
echo "<pre>";
echo implode(", ", get_number_sequence(24, 4)) . PHP_EOL;
echo implode(", ", get_number_sequence(24, 6)) . PHP_EOL;
echo implode(", ", get_number_sequence(24, 5)) . PHP_EOL;
echo "</pre>";
}
catch (Exception $e) {
echo "Invalid: " . $e->getMessage();
}
Some Points
Don't return false if something exceptional happens, use an Exception as shown in the example.
Use the modulus operator to determine if the number is divisible or not.
Return an array, not a string. It's easier to work with.

should be easy
do a modulus on X by Y . If 0 then do a division on X by Y. create a loop which will run from 1 to (division on X by Y) and output Y multiplied by the loop counter

function steps($target,$step) {
if (($target % $step) != 0)
return FALSE;
$steps = range($step,$target,$step);
return $steps;
}
$target = 24;
for ($step = 2; $step < 13; ++$step) {
echo '$step = ',$step,PHP_EOL;
$steps = steps($target,$step);
var_dump($steps);
}

function findQuotients($number, $divider)
{
$arr = array();
if($number % $divider != 0)
{
//return "false";
}
else
{
$loop = $number / $divider;
//$output="";
for($i = 1; $i <= $loop; $i++)
{
//$output .= $i * $divider. " ";
array_push($arr, $i * $divider);
}
}
return $arr;
}
echo print_r(findQuotients(24, 6));
echo print_r(findQuotients(24, 4));
echo print_r(findQuotients(24, 5));

Try this
$number = 24;
$divider = 6;
if($number % $divider != 0 )
{
return false;
}
$div = $number / $divider;
for($i = 1; $i <= $div; $i++)
{
echo $i*$divider;
}

The following snippet will do the trick. It's a simple loop to iterate until $num is <= 0. $num will be subtracted by the divider and each turn the next multiple of $div will be stored as a "divider step".
$num = 24;
$div = 4;
if ($num % $div != 0) {
exit('invalid');
}
$divider = array();
for ($i = 1; $num > 0; $i++) {
$divider[] = ($i * $div);
$num -= $div;
}
echo 'in: ' . $num . '<br />';
echo 'div: ' . $div . '<br />';
echo '<pre>';
print_r($divider);
exit;

based on your description you want to multiply a number and then on a given result you want to white a function:
$num = 6;
$counter = 2;
$solution = 24;
while ($num * $counter) {
$result= $num * $counter;
if ($result = $solution) {
echo $result;
// here would go your event
break;
}
}

Related

How can I calculate min, max and average values from random numbers?

I have the following code:
<?php
echo "<p>Exercise 5:</p>";
echo "Numbers: ";
$random = 0;
while ($random < 10) {
$rand = rand(2, 80);
echo "$rand";
$random = $random + 1;
if ($random < 10) {
echo ", ";
};
}
echo "<br><br>Min value: <br>";
echo "Max value: <br>";
echo "Average value: <br>";
?>
How can I calculate the min, max and average value of the 10 numbers?
$min = min($rand) doesn't work...
$max = max($rand) doesn't work either...
$rand is a single value, minimum of a single value is irrelevant. Min takes an array as parameter (or several values), so save your values in an array, e.g. like this.
$array = array();
while($random < 10) {
$rand = rand(2, 80);
$array[] = $rand;
$random++; // short for random = random + 1
}
echo min($array);
Works also with max.
Moreover, average = sum / count, you have array_sum and count function in PHP, I let you figure out how to do that.
Edit: Augustin is right about division by zero. Consider adding a condition when making a division by a variable.
One solutions thinking in division by zero could be:
$random = 0;
$numbers = array();
while ($random < 10) {
$rand = rand(2, 80);
echo "$rand";
$numbers[] = $rand;
$random ++;
}
$min = min($numbers);
$max = max($numbers);
if($totalNumbers = count($numbers) > 0) {
$average = array_sum($numbers) / count($numbers);
}else{
$average = 0;
}
echo "<br><br>Min value: $min <br>";
echo "Max value: $max <br>";
echo "Average value: $average <br>";
I don't understand why min or max doesn't work, but in this case, you could make a custom max min function:
function customMaxMin($numbers)
{
$max = 0;
$min = 0;
foreach ($numbers as $number) {
$max = $number;
$min = $number;
if ($max > $number) {
$max = $number;
}
if ($min < $number) {
$min = $number;
}
}
return array('max' => $max, 'min' => $min);
}

Divide whole numbers into X parts

I need to divide an integer value into x parts (dynamic) using php inside a for loop (Note:Both the number to be split and the split value are dynamic)
for eg: I have a value 127 and divide it into 2parts it would be 63 and 64.
$number = y; //for example is 127
$parts = x; //for example is 2
for($i=1;$i<$parts;$i++){
//first iteration should output 63
//second iteration should output 64 (the last iteration should be always higher is the $number is not divisible by $parts)
}
Check out this example. I use the modulo operator. This works whether it's an even or odd number. You could wrap this all in a function also.
$x = 127;
$a = 0;
$b = 0;
$a = floor($x/2);
$b = ($x % 2) + $a;
echo "A: " . $a . "| B: " . $b; //A: 63| B: 64
Try it in a function.
function remainders($x, $num) {
$results = array();
$firstOp = floor($x / $num);
for($a = 1; $a <= $num; $a++) {
if($a != $num) {
$results[] = $firstOp;
}
else {
if($x % 2 == 1) {
$results[] = $firstOp + 1;
}
else {
$results[] = $firstOp;
}
}
}
return $results;
}
Then you can iterate through the returned array or do what you want.
$splitNum = remainders(183, 4); //split the number 183 in 4 parts.
foreach($splitNum as $var) { echo $var . ", "; }
Try this:
$number = 127; //for example is 127
$parts = 3; //for example is 3
$sep = ", ";
$n=floor($number/$parts);
for($i=1;$i<=$parts;$i++){
if ($i==$parts) {
$n=$number-($n*($i-1));
$sep="";
}
echo $n.$sep;
}

How can I make radical function in php?

I was trying to make function that gives two value. First value was the respite in the radical and the second value is the number that we want to put it in radical.
It's my code:
function radical($respite = 2, $num)
{
$numbers = array();
for ($i = 1; $i < 10; $i++) {
$numbers[] = '0.' . "$i";
}
for ($i = 1; $i < 10; $i++) {
$numbers[] = '1.' . "$i";
}
// I wanted do these loop until to creat numbers from 0.1 to 100,
// but i under stand it's silly work and wrong.
for ($i = 0; $i < sizeof($numbers); $i++) {
if ($respite == 2) {
$hesan = $number["$i"] * $number["$i"];
if ($hesab == $num) {
return $hesab;
}
} elseif ($respite == 3) {
$hesan = $number["$i"] * $number["$i"] * $number["$i"];
if ($hesab == $num) {
return $hesab;
}
}
}
}
I tried to create numbers from 0.1 to 100. and I wanted write if $number[$i] * $number[$i] = $num return the $number, but I saw it's silly work and wrong my means create numbers from 0.1 to 100 by this way.
For radical(2, 9) the output should be 4, because 3 * 3 = 9.
If the first value is 3 radical(3, 8) the output should be 2, because 2 * 2 * 2= 8
Can someone make function to do radical with respite ? or improve my code ?
If you want to do this not as an exercise but for productive use, I suggest this:
function radical($num, $respite = 2)
{
return $num ** (1 / $respite);
}
echo radical(27, 3) . "\n" .
radical(8, 3) . "\n" .
radical(36, 2) . "\n" .
radical(16, 2);
Output:
3
2
6
4
You can see it here
The reason I changed the argument order is that you can't have a required parameter after an optional one.
Hope this is what you are looking for..
Try this code snippet here
function radical($respite = 2, $num)
{
for($x=0;$x<=10;$x+=0.1)
{
$numbers[]=$x;
}
for ($i = 0; $i < sizeof($numbers); $i++)
{
if ($respite == 2)
{
if ((string)($numbers[$i] * $numbers[$i]) == (string)$num)
{
return $numbers[$i];;
}
}
elseif ($respite == 3)
{
if ((string)($numbers[$i] * $numbers[$i]* $numbers[$i]) == (string)$num)
{
return $numbers[$i];
}
}
}
}
print_r(radical(3, 27));//3
print_r(radical(3, 8));//2
print_r(radical(2, 36));//6
print_r(radical(2, 16));//4
I'm not sure what you're going for exactly, but I think this cleans up what you have now at least:
function radical($num, $respite = 2) {
$numbers = array();
for ($i = 1; $i <= 1000; $i++) {
$numbers[] = $i * 0.1;
}
foreach($numbers as $i) {
if ($respite == 2) {
$hesab = $i * $i;
if ($hesab == $num) {
return $i;
}
}
elseif($respite == 3) {
$hesab = $i * $i * $i;
if ($hesab == $num) {
return $i;
}
}
}
return 0;
}

PHP letter looping number to chr

I am trying to turn numbers into letters to create references for rows.
I have:
public static function references($idx) {
$str = '';
$i = ceil($idx/25);
if(65+$idx > 90) {
} else {
$str = chr(65+$idx);
}
return $chr;
}
But I don't know where to go from here.
Valid outputs would be:
first item: A
28th item: AB
...
Input is an index that comes in from a loop, i.e. 0, 1, 2, 3 etc
I figured it out:
public static function references($n)
{
$r = '';
for ($i = 1; $n >= 0 && $i < 10; $i++) {
$r = chr(0x41 + ($n % pow(26, $i) / pow(26, $i - 1))) . $r;
$n -= pow(26, $i);
}
return $r;
}

php - for with 3 values

i have:
# = 1
# = 0,5
% = 0
max = 10
for example if i have: 3 then should show me:
###$$$$$$$
if i have 3,5 :
####$$$$$$
etc
if i have 3,99 then = 3,5
if i have 3,49 then = 3,0
etc
how can i use this with foreach or for?
for whole number i can make:
$number = 8;
$one = 10 - $number;
$three = 0 + $number;
and
for($i=1;$i <= $one){
echo "#";
}
for($i=1;$i <= $three){
echo "$";
}
but how is the best solution if $number = 3,57
If I've understood what you're after, this should do what you want:
<?php
function printItOut($number) {
$s = '';
for ($i = 0; $i < 10; $i++) {
if ($i < $number%10) {
$s .= '#';
} else if ($i < ($number+0.5)%10) {
$s .= '#';
} else {
$s .= '$';
}
}
return $s;
}
echo printItOut(3.49), "\n";
echo printItOut(3.5), "\n";
echo printItOut(3.99), "\n";
echo printItOut(4), "\n";
Outputs:
###$$$$$$$
####$$$$$$
####$$$$$$
####$$$$$$
Inside the for loop, I using the modulus operator to find the integer remainder of dividing $number by 10. So 3%10 gives a result of 3, 3.49%10 also results in 3.
In the first 'else if' block, I'm checking whether the number is 0.5 or more, since (3.49+0.5) is 3.99, and 3.99%10 is 3; but 3.5+0.5 is 4, and 4%10 is 4.

Categories