Represents a positive integer as the sum of consecutive natural numbers.

Source: Internet
Author: User

Represent a positive integer as the sum of consecutive natural numbers. For example, if an integer 15 is given, the continuous natural numbers to be output are 1 + 2 + 3 + 4 + 5 = 4 + 5 + 6 = 7 + 8 = 15. The continuous natural number sequence in the question can be regarded as an ascending ordered array, and the first two ends of the array are left and right of the start interval. Accumulate the values in the range. If the accumulated value is smaller than the given integer, move the right endpoint to the right and add the next number. If the accumulated value is greater than the given integer, moving the left endpoint to the right indicates removing the minimum value at the leftmost end. If the value is equal to the given integer, you need to assign a value to the left and right endpoints of the specified range after the output, until the left endpoint value is less than (number + 1)/2. The code is as follows:

1 void printContinuous (int begin, int end, int value) 2 {3 for (int I = begin; I! = End; I ++) 4 cout <I <"+"; 5 cout <end <"=" <value <endl; 6} 7 8 void findContinuous (int n) 9 {10 int begin = 1, end = 2; // begin and end represent the interval 11 int middle = (n + 1)/2 for the continuous positive number of n, respectively; // middle indicates the intermediate number of n, middle * 2> = n, so you can control begin <middle to 12 int sum = begin + end; 13 14 while (begin <middle) 15 {16 if (sum = n) // if the sum is equal to n, print 17 {18 printContinuous (begin, end, n); 19 // recalculates the sum value from begin + 1 to 20 begin ++; 21 end = begin + 1; 22 sum = begin + end; 23} 24 else if (sum> n) // if sum> n, shift begin right, that is, subtract the leftmost number 25 {26 sum-= begin; 27 begin ++; 28} 29 else // If sum <n, then the end is shifted to the right, add 30 {31 end ++; 32 sum + = end; 33} 34} 35}

The above solution can meet the requirements of the question. Now we try to use a mathematical method to solve this question. The question requires that a given integer be expressed as the sum of consecutive natural numbers, while a continuous natural number sequence can be considered as an arithmetic difference series, so the question can be re-described, returns an equal-difference sequence composed of natural numbers of given integers. The formula for the sum of the first n terms of the arithmetic difference series is a1 * n + n * (n-1) * d/2. a1 indicates the first value, n indicates the number of items, and d indicates the tolerances. Based on the formula, you can write the code:

1 void findContinuous2 (int n) 2 {3 for (int I = 1; I <(n + 1)/2; I ++) 4 {5 for (int j = 1; j <(n + 1)/2; j ++) 6 {7 // indicates starting with I, the number of arithmetic numbers and 8 int sum = I * j + (j * (j-1)/2) until j after I; 9 if (sum = n) 10 {11 printContinuous (I, I + j-1, n); 12} 13} 14} 15}

It can be seen that mathematics is still very important for some algorithms. It cannot be said that it will definitely improve the running efficiency of the program. However, to solve some problems, mathematical knowledge will help us better understand our solutions.

Represents a positive integer as the sum of consecutive natural numbers.

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.