Decomposition of positive integers into the sum of several contiguous natural numbers

Source: Internet
Author: User

Title: Enter a positive integer that outputs all possible positive integer sequences if the number can be represented by a sum of several consecutive positive integers.

A positive integer may be represented as a sum of N (n>=2) consecutive positive integers, such as:
15=1+2+3+4+5
15=4+5+6
15=7+8

Some numbers can be written as the sum of successive n (>1) natural numbers, such as 14=2+3+4+5; some cannot, say, 8. So how do you tell if a number can be written as the sum of successive n natural numbers?

If a number M can be written as the sum of successive n natural numbers starting with a, then m=a+ (a+1) + (a+2) +...+ (a+n-1) =n*a+n* (n-1)/2, a!=0 is required, otherwise a+1 is the continuous n-1 integer, that is, a requirement (m-n* (n-1)/2 %n==0, so it's easy to tell if a number can be written in the form of a continuous n natural number, traversing n=2...sqrt (M), and outputting all the solutions.

void divide (int num)  {      int i,j,a;      for (i=2; i<=sqrt (float) num) (++i)      {          if (num-i* (i-1)/2)%i==0)          {a= (num-i*              (i-1)/2)/I;              if (a>0)              {for                  (j=0; j<i; ++j)                      cout<<a+j<< "";              }              cout<<endl;          }}}     

The second question is what number can be written as the sum of successive n natural numbers, and what number cannot?

The programming experiment found that all the other numbers except 2^n could be written in that form. The following explains why.
If the number of M meets the conditions, then there is m=a+ (a+1) + (a+2) +...+ (a+n-1) = (2*a+n-1) *N/2, and 2*a+n-1 and N must be an odd-numbered one even, that is, M must have an odd factor, and all 2^n have no odd factor, and therefore certainly do not meet the criteria.
It is proved that only m has an odd factor, that is, M!=2^n,m can be written as the sum of successive n natural numbers. Suppose that M has an odd factor a, then m=a*b.

    1. If B is also an odd number, as long as A-1/2>0,m can be written as B (A-1)/2, a continuous a natural numbers, the conclusion of A and B exchange, still set up. 15=3*5=1+2+3+4+5=4+5+6.
    2. If B is an even number, then we have an odd number A and an even number B.
    • 2.1 B (A-1)/2>0,m can be written as a continuous a natural number starting with B (A-1)/2. 24=3*8=7+8+9.
    • 2.2 if (a+1)/2-b>0,m can be written in (a+1)/2-b the beginning of a continuous 2*b a natural number. 38=19*2=8+9+10+11.

The above two inequalities must have at least one set up, so it can be proved that as long as M has an odd factor, it can be written as the sum of successive n natural numbers.

Another algorithm for the decomposition of positive integers:
SUM (i,j) for I summation to J and
Make I=1 j=2
If sum (i,j) >n i++
else if sum (i,j) <n J + +
else cout I...J

#include <iostream>   using namespace std;    int add (int m,int n)  {      int sum=0;      for (int i=m;i<=n;i++)          sum+=i;      return sum;  }    void divide (int num)  {      int i=1,j=2,flag;      int sum=0;      while (I<=NUM/2)      {       sum=add (i,j);       while (Sum!=num)       {          if (sum>num)              i++;          else              j + +;          Sum=add (I,J);       }       for (int k=i;k<=j;k++)          cout<<k<< "";       ++i;       cout<<endl;      }  }    int main ()  {      int num;      cout<< "Please input your number:" <<endl;      cin>>num;      Divide (num);      return 0;  }  

Decomposition of positive integers into the sum of several contiguous 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.