Greedy algorithm----positive integer decomposition problem and the same, product maximum

Source: Internet
Author: User

First, the problem description

Set n is a positive integer. It is now required to decompose n into the sum of several natural numbers, and the product of these natural numbers is the largest.

This article breaks down this big problem into two small problems:

(1) These natural numbers are different.

(2) These natural numbers can be the same

Second, the solution of ideas

This is actually a mathematical problem, the overall purpose is to decompose the number of the closer, their product is the largest, and do not break down 1, at least starting from 2.

for (1) This problem, because these several natural numbers are not the same, so can only start from 2 decomposition, such as 2,3,4 ... this order

Here, we make this decomposition problem concrete:

9=2+3+4, just can decompose these three consecutive numbers;

10=2+3+4+1, because 1 will not increase the product, but will occupy and, so it should be added to the first three number, in order to ensure maximum and different, add to 4, then decomposition to 10=2+3+5;

11=2+3+4+2, if the 2 is directly added to 4, the product is 2*3*6=36, if added to a continuous number, such as 3,4, to ensure that the gap between these numbers is the smallest, then the product is 2*4*5=40;

12=2+3+4+3; The three is divided into three one and added to the previous three numbers to ensure continuity;

13=2+3+4+4; because it is four one, so the method we take is the last number plus 2, the other plus 1, then the product is: 3*4*6=72;

Therefore, the overall strategy is to break down into contiguous natural numbers and distribute the remaining numbers evenly from the back to the next.

For the (2) problem, because the number of decomposition can be the same, so first compare the decomposition into the same number and the different number of which product large. Such as

6=3+3;6=2+4;

3*3=9;

2*4=8;

So the difference between the decomposition of the same number guarantees the smallest, and thus the product is also the largest. Naturally knowing the advantages of decomposing into the same number, for an integer, we should be as many as possible to break down into what kind of number?

For 4, can only be split into a 1, because to avoid the dismantling of the 2*2, and the effect of the size of itself is consistent;

For 5, can only be broken into 2+3, because to avoid the demolition of 1, and the effect of 2*3 greater than 5 itself;

For 6, the effect of splitting into a 3+3 (9) is better than the effect of 2+2+2 (8);

For 7, the effect of splitting into 3+4 is the best;

For 8, we may be the first reaction is 4+4, the effect is 16, but in fact the best decomposition 3+3+2, the effect is 18;

For 10来 said, theoretically is 10=5+5;

But since 5 itself has a better decomposition, so 10 = (2+3) + (2+3);

So the decomposition of the strategy is: first decomposed into as many as 3, and then decomposed into 2, do not divide 1!

Consider an arbitrary number, divided by 3, with only 3 cases:

(1) Divisible, the remainder is 0, such as 9, decomposition into 3+3+3 is optimal;

(2) Not divisible, the remainder is 1, such as 10, decomposed into 3+3+4 or 3+3+2+2; Except for three, the remainder is 2 or 4;

(3) Not divisible, the remainder is 2, such as 11, decomposed into 3+3+3+2;

Third, program design

(1) The natural number is not the same:

(2) Decomposition to the same natural number

Iv. Results of the procedure

(1) The case of a different integer:

2*3*5=30;

(2) Where the same integer can be

3*3*2*2=36;

Greedy algorithm----positive integer decomposition problem and the same, product maximum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.