gogoWebsite

The sum of continuous integers

Updated to 3 hours ago

The sum of continuous integers

Description
Given a positive integer N, write N as the form of several consecutive sums of numbers (length >= 2). For example, N = 15 can be written as 1 + 2 + 3 + 4 + 5, or 4 + 5 + 6, or 7 + 8. If it cannot be written as the sum of several consecutive integers, No Solution is output.

Input
Enter 1 number N (3 <= N <= 10^9).

Output
Output the first number among continuous integers. If there are multiple numbers arranged in incremental order, if they cannot be decomposed into the sum of several consecutive integers, then output No Solution.

Sample Input

15

Sample Output

1
4
7

A math problem based on the first n terms and formula of the arithmetic sequence S=na1+n(n1)2d S = n a 1 + n ( n − 1 ) 2 d , substitute d=1 to solve the first item a1=2Sn(n1)2n a 1 = 2 S − n ( n − 1 ) 2 n Then determine the range of n violence. The lower limit of n is 2. The upper limit is as follows. Continue to obtain the formula from the previous n terms

n2+2n(a112)=2S n 2 + 2 n ( a 1 − 1 2 ) = 2 S
So
n2<2S n 2 < 2 S
Right now
n<2S n < 2 S

#include<iostream>
#include<cmath>

using namespace std;

int main()
{
    int n,flag=0;
    cin>>n;
    for(int i=sqrt(2*n);i>=2;i--)
    {
        if((2*n-i*i+i)%(2*i)==0&&2*n-i*i+i>0)
            cout<<(2*n-i*i+i)/(2*i)<<endl,flag=1;
    }
    if(!flag) cout<<"No Solution"<<endl;
    return 0;
}