Simple Program in PHP

I need a program in php in such a way that when we input a number like 145 in a form the output should be 50 50 45… if someone know it… plz help me

I recently came across this problem and through alot of help, came across this nice solution. Albeit I’m using 4 numbers to get to my solution, you can change the output to 3 in the final function

Ok, this problem actually revolves around linear sequences. With a minimum value of 1 consider the sequence:

f(n) = 1 + 2 + ... + n - 1 + n

The sum of such a sequence is equal to:

f(n) = n * (n + 1) / 2

so for n = 4, as an example, the sum is 10. That means if you’re selecting 4 different numbers the minimum total with no zeroes and no negatives is 10. Now go in reverse: if you have a total of 10 and 4 numbers then there is only one combination of (1,2,3,4).

So first you need to check if your total is at least as high as this lower bound. If it is less there is no combination. If it is equal, there is precisely one combination. If it is higher it gets more complicated.

Now imagine your constraints are a total of 12 with 4 numbers. We’ve established that f(4) = 10. But what if the first (lowest) number is 2?

2 + 3 + 4 + 5 = 14

So the first number can’t be higher than 1. You know your first number. Now you generate a sequence of 3 numbers with a total of 11 (being 12 - 1).

1 + 2 + 3 = 6
2 + 3 + 4 = 9
3 + 4 + 5 = 12

The second number has to be 2 because it can’t be one. It can’t be 3 because the minimum sum of three numbers starting with 3 is 12 and we have to add to 11.

Now we find two numbers that add up to 9 (12 - 1 - 2) with 3 being the lowest possible.

3 + 4 = 7
4 + 5 = 9

The third number can be 3 or 4. With the third number found the last is fixed. The two possible combinations are:

1, 2, 3, 6
1, 2, 4, 5

You can turn this into a general algorithm. Consider this recursive implementation:

$all = all_sequences(14, 4);
echo "\
All sequences:\
\
";
foreach ($all as $arr) {
  echo implode(', ', $arr) . "\
";
}

function all_sequences($total, $num, $start = 1) {
  if ($num == 1) {
    return array($total);
  }
  $max = lowest_maximum($start, $num);
  $limit = (int)(($total - $max) / $num) + $start;
  $ret = array();
  if ($num == 2) {
    for ($i = $start; $i <= $limit; $i++) {
      $ret[] = array($i, $total - $i);
    }
  } else {
    for ($i = $start; $i <= $limit; $i++) {
      $sub = all_sequences($total - $i, $num - 1, $i + 1);
      foreach ($sub as $arr) {
        array_unshift($arr, $i);
        $ret[] = $arr;
      }
    }
  }
  return $ret;
}

function lowest_maximum($start, $num) {
  return sum_linear($num) + ($start - 1) * $num;
}

function sum_linear($num) {
  return ($num + 1) * $num / 2;
}

Output:

All sequences:

1, 2, 3, 8
1, 2, 4, 7
1, 2, 5, 6
1, 3, 4, 6
2, 3, 4, 5

One implementation of this would be to get all the sequences and select one at random. This has the advantage of equally weighting all possible combinations, which may or may not be useful or necessary to what you’re doing.

That will become unwieldy with large totals or large numbers of elements, in which case the above algorithm can be modified to return a random element in the range from $start to $limit instead of every value.

This is the answer I got to my question on Stackoverflow, I hope it helps you as much as it has helped me.

Try this:

<?php
$number = 145;
$output = "";

$remainder = $number&#37;50; //45
$count = ($number - $remainder)/50; //number of 50s = 2

for($i=0; $i<$count; $i++)
{
$output .= "50 ";
}

$output .= "$remainder";
echo $output;
?>

What would your math equation be to produce this? I guess I don’t have enough info from your question to form the equation for you.

you could probably do something like this:

Divide the value by 50, store the value in a variable, and floor() it.
Then multiply that result by 50 again, and subtract that value from the original value.


$number = $_POST['number'];
$factor = 50;
$whole = floor($number / $factor);
$rest = $number - ($whole * $factor);
echo str_repeat($factor.' ', $whole) . ' '.$rest;

An alternartive, could be to use a loop


$factor = 50;  
$number = $_POST['number'];
for($i = $factor; $i < $number; $i = $i+$factor)
{
	echo $factor.' ';
}
echo $number - ($i - $factor);

There are probably lots of ways to do it, I hope these examples points you in the right direction.