# Count of sum of consecutives

Posted: 11 Mar, 2021

Difficulty: Hard

#### You are given a positive integer 'N.' Your task is to find the number of ways to represent the given number 'N' as the sum of two or more consecutive natural numbers.

##### Input Format:

```
The first line contains an integer, ‘T,’ which denotes the number of test cases or queries to be run. Then, the 'T' test cases follow.
The first and the only line of each test case contains one integer 'N', as described in the problem statement.
```

##### Output Format:

```
For each test case, return one integer denoting the number of ways to represent the given number 'N' as the sum of 2 or more consecutive natural numbers.
```

##### Note:

```
You do not need to print anything. It has already been taken care of. Just implement the given function.
```

##### Constraints:

```
1 <= T <= 100
1 <= N <= 10^8
Time Limit: 1 sec
```

Approach 1

**Approach:**

In this brute force approach,. We will iterate from 1 to ‘**N’** using the iterator **i** and will check if there is a possible sequence of consecutive positive integers such that their sum is ‘**N’**, and the ending integer of the consecutive sequence is '**i'**. The checking will be done as follows:

- We know that the ending position is ‘
**i’**. Therefore, we need to find a starting point for the consecutive sequence. Let that integer is 'k'. - Now we can say that (
**i***(**i**+1))/2 - (**j***(**j**+1))/2 = ‘**N’**, where ‘**j’**= ‘**k**-1’ (since sum of first n natural numbers is given by the equation (N*(N+1))/2). - After simplifying the equation in the above point we will get : (
**j**^2 +**j**+ 2***N**-**i**^2 -**i**) = 0 - Now, if there exists a positive integral root for ‘
**j**’ in the above equation, then there is an answer, and we will count this sequence in our answer, or else it is not possible, then we will not count this sequence in the answer.

**Steps:**

- Create a variable to store the answer (say, ANS) with 'ANS' = 0.
- Run a loop from ‘
**i’**= 0 to ‘**N’**and do:- Create three variables (say A,B and C) and initialize ‘A’ = 1, ‘B’ = 1 and ‘C’ = 2 * ‘
**N’**-**i*****i**-**i**. - Create a variable ‘D’
- Create a variable 'SQROOT'
- Create a boolean variable ‘CHECK’
- If ‘D’ >= 0 and
**SQROOT * SQROOT**== D :- If
**(-B + SQROOT)**is divisible by**(2 * A)**and**(-B + SQROOT) / (2 * A)**>= 0 and**(i - ((-B + SQROOT) / (2 * A)))**>= 2 :- ‘CHECK’ = true

- If
**(-B - SQROOT)**is divisible by**(2 * A)**and**(-B - SQROOT) / (2 * A)**>= 0 and**(i - ((-B - SQROOT) / (2 * A)))**>= 2 :- ‘CHECK’ = true

- If
- If ‘CHECK’ is true:
- Increment the ‘ANS’

- Increment the ‘ANS’

- Create three variables (say A,B and C) and initialize ‘A’ = 1, ‘B’ = 1 and ‘C’ = 2 * ‘
- Finally, return the ‘ANS’ variable.

Approach 2

**Approach:**

- In this approach, we will try to represent ‘
**N’**as the sum of ‘M+1’ consecutive natural numbers. - Therefore ‘N’ can be written as :
- ‘N’ = a + (a + 1) + (a+2) + (a+3) + … + (a + M) , where ‘a’ is the starting number in the sequence.
- Now, ‘N’ = (M+1)*a + (M*(M+1))/2.
- Finally, a = (N - (M*(M+1))/2) / (M+1).
- Here we need to find all possible values of m such that the above equation gives a positive integral value of ‘a’.
- Now since we need positive integral values of ‘a’, So (M*(M+1))/2 < ‘N’ and ‘M’ can have at most N^(1/2) different values.
- So, we will iterate from ‘M’ = 1 to N^(1/2) and check whether we can get an integral value of ‘a’. If it is possible, then this current sequence will be counted in our answer.

**Steps:**

- Create a variable to store the answer (say, ANS) with ‘ANS’ = 0.
- Run a loop from ‘M’ = 1 till M
***(M+1)**< 2***N**and do:- If (
**N - (M*(M+1))/2**) is divisible by (M**+1**) :- Increment the 'ANS' variable by 1.

- If (
- Finally, return the ‘ANS’

SIMILAR PROBLEMS

# Palindromes And Indexes

Posted: 7 Jul, 2021

Difficulty: Moderate

# Ceiling in a sorted array

Posted: 8 Jul, 2021

Difficulty: Moderate

# Game of 3

Posted: 11 Jul, 2021

Difficulty: Easy

# Minimum Knight Moves

Posted: 20 Aug, 2021

Difficulty: Hard

# Find Student

Posted: 1 Dec, 2021

Difficulty: Easy