Given a number N, find the number of ways to represent this number as a sum of 2 or more consecutive natural numbers.

**Examples:**

Input :15 Output :3 15 can be represented as: 1+2+3+4+5 4+5+6 7+8 Input :10 Output :1 10 can only be represented as: 1+2+3+4

The idea is to represent N as a sequence of length L+1 as:

N = a + (a+1) + (a+2) + .. + (a+L)

=> N = (L+1)*a + (L*(L+1))/2

=> a = (N- L*(L+1)/2)/(L+1)

We substitute the values of L starting from 1 till L*(L+1)/2 < N

If we get 'a' as a natural number then the solution should be counted.

## C/C++

`// C++ program to count number of ways to express ` `// N as sum of consecutive numbers. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `long` `int` `countConsecutive(` `long` `int` `N) ` `{ ` ` ` `// constraint on values of L gives us the ` ` ` `// time Complexity as O(N^0.5) ` ` ` `long` `int` `count = 0; ` ` ` `for` `(` `long` `int` `L = 1; L * (L + 1) < 2 * N; L++) ` ` ` `{ ` ` ` `float` `a = (1.0 * N-(L * (L + 1)) / 2) / (L + 1); ` ` ` `if` `(a-(` `int` `)a == 0.0) ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `long` `int` `N = 15; ` ` ` `cout << countConsecutive(N) << endl; ` ` ` `N = 10; ` ` ` `cout << countConsecutive(N) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// A Java program to count number of ways ` `// to express N as sum of consecutive numbers. ` `public` `class` `SumConsecutiveNumber ` `{ ` ` ` `// Utility method to compute number of ways ` ` ` `// in which N can be represented as sum of ` ` ` `// consecutive number ` ` ` `static` `int` `countConsecutive(` `int` `N) ` ` ` `{ ` ` ` `// constraint on values of L gives us the ` ` ` `// time Complexity as O(N^0.5) ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `L = ` `1` `; L * (L + ` `1` `) < ` `2` `* N; L++) ` ` ` `{ ` ` ` `float` `a = (` `float` `) ((` `1.0` `* N-(L * (L + ` `1` `)) / ` `2` `) / (L + ` `1` `)); ` ` ` `if` `(a-(` `int` `)a == ` `0.0` `) ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code to test above function ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `int` `N = ` `15` `; ` ` ` `System.out.println(countConsecutive(N)); ` ` ` `N = ` `10` `; ` ` ` `System.out.println(countConsecutive(N)); ` ` ` `} ` `} ` `// This code is contributed by Sumit Ghosh ` |

*chevron_right*

*filter_none*

## Python

`# Python program to count number of ways to ` `# express N as sum of consecutive numbers. ` ` ` `def` `countConsecutive(N): ` ` ` ` ` `# constraint on values of L gives us the ` ` ` `# time Complexity as O(N^0.5) ` ` ` `count ` `=` `0` ` ` `L ` `=` `1` ` ` `while` `( L ` `*` `(L ` `+` `1` `) < ` `2` `*` `N): ` ` ` `a ` `=` `(` `1.0` `*` `N ` `-` `(L ` `*` `(L ` `+` `1` `) ) ` `/` `2` `) ` `/` `(L ` `+` `1` `) ` ` ` `if` `(a ` `-` `int` `(a) ` `=` `=` `0.0` `): ` ` ` `count ` `+` `=` `1` ` ` `L ` `+` `=` `1` ` ` `return` `count ` ` ` `# Driver code ` ` ` `N ` `=` `15` `print` `countConsecutive(N) ` `N ` `=` `10` `print` `countConsecutive(N) ` ` ` `# This code is contributed by Sachin Bisht ` |

*chevron_right*

*filter_none*

## C#

`// A C# program to count number of ` `// ways to express N as sum of ` `// consecutive numbers. ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Utility method to compute ` ` ` `// number of ways in which N ` ` ` `// can be represented as sum ` ` ` `// of consecutive number ` ` ` `static` `int` `countConsecutive(` `int` `N) ` ` ` `{ ` ` ` ` ` `// constraint on values of L ` ` ` `// gives us the time ` ` ` `// Complexity as O(N^0.5) ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `L = 1; L * (L + 1) ` ` ` `< 2 * N; L++) ` ` ` `{ ` ` ` `float` `a = (` `float` `) ((1.0 ` ` ` `* N-(L * (L + 1)) ` ` ` `/ 2) / (L + 1)); ` ` ` ` ` `if` `(a - (` `int` `)a == 0.0) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code to test above ` ` ` `// function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 15; ` ` ` `Console.WriteLine( ` ` ` `countConsecutive(N)); ` ` ` ` ` `N = 10; ` ` ` `Console.Write( ` ` ` `countConsecutive(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count number ` `// of ways to express N as sum ` `// of consecutive numbers. ` ` ` `function` `countConsecutive(` `$N` `) ` `{ ` ` ` `// constraint on values ` ` ` `// of L gives us the ` ` ` `// time Complexity as O(N^0.5) ` ` ` `$count` `= 0; ` ` ` `for` `(` `$L` `= 1; ` ` ` `$L` `* (` `$L` `+ 1) < 2 * ` `$N` `; ` `$L` `++) ` ` ` `{ ` ` ` `$a` `= (int)(1.0 * ` `$N` `- (` `$L` `* ` ` ` `(int)(` `$L` `+ 1)) / 2) / (` `$L` `+ 1); ` ` ` `if` `(` `$a` `- (int)` `$a` `== 0.0) ` ` ` `$count` `++; ` ` ` `} ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver Code ` `$N` `= 15; ` `echo` `countConsecutive(` `$N` `), ` `"\n"` `; ` `$N` `= 10; ` `echo` `countConsecutive(` `$N` `), ` `"\n"` `; ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3 1

The Time complexity for this program is O(N^0.5), because of the condition in the for loop.

This article is contributed by **Pranav Marathe**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count ways to express a number as sum of exactly two numbers
- Express a number as sum of consecutive numbers
- Count ways to express even number ‘n’ as sum of even integers
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Ways to express a number as product of two different factors
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Express an odd number as sum of prime numbers
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Minimum numbers needed to express every integer below N as a sum
- Count of N digit Numbers whose sum of every K consecutive digits is equal
- Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Count of ways to write N as a sum of three numbers
- Count of ways in which N can be represented as sum of Fibonacci numbers without repetition
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of palindromes required to express N as a sum | Set 2
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Check if a number can be expressed as a sum of consecutive numbers
- Count number of ways to arrange first N numbers
- Minimum number of distinct powers of 2 required to express a given binary number