Continuous positive integer sequence for N

Source: Internet
Author: User
Question

Enter a positive integerN, Output all andNContinuous positive integer sequence. For example, enter15, Because1 + 2 + 3 + 4 + 5 = 4 + 5 + 6 = 7 + 8 = 15, So the output3Consecutive Sequences1-5,4-6And7-8.


A solution using mathematical law

Assume that K consecutive positive integers are N, and the first number of consecutive sequences is X, X + (x + 1) + (x + 2) +... + (x + k-1) = n. So we can obtain x = (n-k * (k-1)/2)/K. When the value of X is less than or equal to 0, it indicates that there is no positive integer sequence and N, and then the loop exits. Initialize K = 2, which indicates two consecutive positive integers and N. Then, you can obtain the value of X and determine whether there are two consecutive positive integers and N starting from X, if it does not exist, K ++ continues the loop.

Bool find_sequence (int n) {bool has = false; int K = 2, X, M; // K indicates the number of consecutive sequences, and X indicates the starting value, M is used to determine whether a value meets the conditions. While (true) {x = (n-k * (k-1)/2)/K; // obtain k continuous positive integers and the starting value of n xm = (n-k * (k-1)/2) % K; // m is used to determine whether a continuous positive integer value if (x <= 0) Break meets the condition; // exit the condition. If x <= 0, it exits cyclically. If (! M) {// m is 0, indicating that the continuous subsequence is found as N. Has = true; output (x, k);} k ++;} return has;} void output (int x, int K) {for (INT I = 0; I <K; I ++) {cout <X ++ <"" ;}cout <Endl ;}

Extension

Problem: Can all positive integers be decomposed into continuous positive integer sequences?

Answer:No. Not all positive integers can be decomposed into continuous positive integers. For example, 32 cannot be decomposed into continuous positive integers and. For odd numbers, we can always be written in the form of 2 k + 1. Therefore, we can be decomposed into [K, k + 1], so we can always be decomposed into continuous positive integer sequences. For each even number, it can be decomposed into the product of prime factor, that is, n = POW (2, I) * POW (3, j) * POW (5, k )..., if j, k... if they are all 0, then n = POW (2, K). For this number, all its factors are even, and there is no continuous subsequence and N, for details, see reference 2. Therefore, except for the power of 2, all positive integers n
> = 3 can all be written as the sum of consecutive natural numbers.


Another solution

Mr. He Haitao's blog has another solution, which can be found below:

use two numbers small and big represents the minimum and maximum values of the sequence respectively. First, initialize small to 1 , big is initialized to 2 . From small to big , we move small to the right, which is equivalent to removing small numbers from the sequence. From small to big , move big to the right, which is equivalent to adding big equal to (1 + N)/2 because the sequence must have at least two numbers.

A more straightforward understanding is to first determine the continuous sequence ending with number 2 and whether there is a number equal to N, then the continuous sequence ending with 3 and whether there is a number equal to n.

//////////////////////////////////////// //// // Find continuous sequence, whose sum is N //////////////////////////////////// /// // void findcontinuoussequence (int N) {If (n <3) return; int small = 1; int big = 2; int middle = (1 + N)/2; int sum = Small + big; while (small <middle) {// We are lucky and find the sequence if (sum = N) printcontinuoussequence (small, big ); // if the current sum is greater than N, // move small forward while (sum> N) {sum-= small; small ++; // We are lucky and find the sequence if (sum = N) printcontinuoussequence (small, big) ;}// move big forward big ++; sum + = big ;}} //////////////////////////////////////// //// // print continuous sequence between small and big ////////////////////////////////////// /// // void printcontinuoussequence (INT small, int big) {for (INT I = small; I <= big; ++ I) printf ("% d", I); printf ("\ n ");}

References

1 He Haitao blog: For N continuous positive number sequence [Algorithm]

2 http://www.cnblogs.com/wolenski/archive/2012/08/06/2624732.html

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.