Need an algorithm to divide a number into unequal integral value parts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KenpachiZ
    New Member
    • Aug 2011
    • 2

    Need an algorithm to divide a number into unequal integral value parts

    Ok, so here is the problem. I have a number, and I need to divide it into maximum unequal parts such that no part is less than 25 in value. Even if you cannot solve the problem, any help or ideas are appreciated. Thanks!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    dou you mean a*b=y or a+b=y? what if the number is less than 25? you need to be a bit more specific to get a useful answer.

    Comment

    • KenpachiZ
      New Member
      • Aug 2011
      • 2

      #3
      As input you will be given the Mass of a Planet in Solar Mass Units (sm). You need to find maximum no. of moons that the planet can have.

      The total mass of moons must not exceed 25% the mass of the planet.

      No two moons can be of same mass.

      Moons must be greater or equal to 25sm

      Moon’s mass must be an integer, no fractions.

      You must use all the mass you can.

      Example:

      input: 1000sm

      output: 25,26,27,28,29, 30,31,54

      Find an equation which gives the maximum no of moons, that you can have, for a given Mass ‘n’

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Just do what the example did. Start with 25. If subtracting that from 25% of the total would leave a mass greater than your current number, subtract that number from the 25% amount and output the number as the mass of a moon. Otherwise, use the rest of the 25% as the mass of the last moon.

        Comment

        • aelisenko
          New Member
          • Oct 2011
          • 7

          #5
          Code:
          <?php
          
          $planet = 1000;                         //set mass of planet
          $min_mass = 25;                         //set min mass requirement
          $max_mass = $planet/4;                  //set max total mass for all moons
          $moons = round(($max_mass/$min_mass));  //max moon count... not really but not more than this.
          
          
          for($i = 0; $i <= $moons; $i++){
            
            //set moon mass
            $moon = $min_mass + $i;
            
            /*
            * if the mass of the moon is taken from the total allowed, would the total left over be
            * less than the minimum. If yes, then whatever is left is the last possible moon.
            */
            if(($max_mass - $moon) < $min_mass){
              echo $max_mass;
              break;
            }
            
            //Do something with the moon...
            echo "$moon, ";
          
            //reduce the total mass allowed once the moon has been used
            $max_mass -= $moon; 
            
          }
          Last edited by Dormilich; Oct 23 '11, 07:25 AM. Reason: please use [CODE] [/CODE] tags when posting code

          Comment

          • franknagy
            New Member
            • Oct 2011
            • 27

            #6
            Explain your planetary conditions

            I do not understand your conditions:
            1. Solar Mass Units (sm). You need to find maximum no. of moons that the planet can have.
              What is the definition of "Solar Mass Unit"?
            2. The total mass of moons must not exceed 25% the mass of the planet.
              Why? If twin start exist the twin planets do, too.
            3. No two moons can be of same mass.
              Why do you exclude equal masses?
            4. Moons must be greater or equal to 25sm
              How do you call smaller fragments?
            5. Moon’s mass must be an integer, no fractions.
              OK, I understand. It is a simplification in to reduce your problem to integer programming.
            6. You must use all the mass you can.
              Condition #4 contradicts to condition #6.


            Regards
            Frank

            Comment

            Working...