SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    SitePoint Evangelist
    Join Date
    Sep 2006
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code PHP:
    $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
    Code PHP:
    $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.
    zalucius

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    <?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;
    ?>

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2006
    Location
    Gold Coast, Australia
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP Code:
    f(n) = + ... + 
    The sum of such a sequence is equal to:

    PHP Code:
    f(n) = * (1) / 
    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?

    PHP Code:
    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).

    PHP Code:
    6
    9
    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.

    PHP Code:
    7

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

    PHP Code:
    1236
    1
    24
    You can turn this into a general algorithm. Consider this recursive implementation:

    PHP Code:
    $all all_sequences(144);
    echo 
    "\nAll sequences:\n\n";
    foreach (
    $all as $arr) {
      echo 
    implode(', '$arr) . "\n";
    }

    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:

    PHP Code:
    1238
    1
    247
    1
    256
    1
    346
    2
    34
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •