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!
Need an algorithm to divide a number into unequal integral value parts
Collapse
X
-
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
-
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
-
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 codeComment
-
Explain your planetary conditions
I do not understand your conditions:- 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"?
- The total mass of moons must not exceed 25% the mass of the planet.
Why? If twin start exist the twin planets do, too. - No two moons can be of same mass.
Why do you exclude equal masses? - Moons must be greater or equal to 25sm
How do you call smaller fragments? - Moon’s mass must be an integer, no fractions.
OK, I understand. It is a simplification in to reduce your problem to integer programming. - You must use all the mass you can.
Condition #4 contradicts to condition #6.
Regards
FrankComment
- Solar Mass Units (sm). You need to find maximum no. of moons that the planet can have.
Comment