Given the number N, write a program that computes the numbers E1, E2, …En with the following properties:
1) N = E1 + E2 + … + En;
2) E1 * E2 * … En is maximum.
3) E1..En, are integers. No negative values 🙂
How would you do that ? I have a solution based on divide et impera but i want to check if is optimal.
Example: N=10
5,5 S=10,P=25
3,2,3,2 S=10,P=36
No need for an algorithm, mathematic intuition can do it on its own:
Step 1: prove that a result set with numbers higher than 3 is at most as good as a result set with only 3’s and 2’s
Given any number x in your result set, one might consider whether it would be better to divide it into two numbers.
The sum should still be x.
What does this show? Only that you should only have 3’s and 2’s in your final set, because otherwise it is suboptimal (or equivalent to an optimal set).
Step 2: you should have as many 3’s as possible
Now, as 3² > 2³, you should have as many 3’s as possible as long as the remainder is not 1.
Conclusion: for every N >= 3:
Please correct this post. The times when I was writing well-structured mathematical proofs is far away…
Note 1: (2,4) is the only pair of distinct integers such that x^y = y^x. You can prove that with:
and the function
ln(t)/tis strictly decreasing after its global maximum, reached between 2 and 3, so if you want two distinct integers such thatln(x)/x = ln(y)/y, one of them must be lower or equal to 2. From that you can infer that only (2,4) works