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 : n = 15 Output : 3 15 can be represented as: 1 + 2 + 3 + 4 + 5 4 + 5 + 6 7 + 8 Input :10 Output :2 10 can only be represented as: 1 + 2 + 3 + 4

We have already discussed one approach in below post.

Count ways to express a number as sum of consecutive numbers

Here a new approach is discussed. Suppose that we are talking about the sum of numbers from X to Y ie [X, X+1, …, Y-1, Y]

Then the arithmetic sum is

(Y+X)(Y-X+1)/2

If this should be N, then

2N = (Y+X)(Y-X+1)

Note that one of the factors should be even and the other should be odd because Y-X+1 and Y+X should have opposite parity because Y-X and Y+X have the same parity. Since 2N is anyways even, we find the number of odd factors of N.

For example, n = 15 all odd factors of 15 are 1 3 and 5 so the answer is 3.

## C++

`// C++ program to count number of ways to express ` `// N as sum of consecutive numbers. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// returns the number of odd factors ` `int` `countOddFactors(` `long` `long` `n) ` `{ ` ` ` `int` `odd_factors = 0; ` ` ` ` ` `for` `(` `int` `i = 1; 1ll * i * i <= n; i++) { ` ` ` `if` `(n % i == 0) { ` ` ` ` ` `// If i is an odd factor and ` ` ` `// n is a perfect square ` ` ` `if` `(1ll * i * i == n) { ` ` ` `if` `(i & 1) ` ` ` `odd_factors++; ` ` ` `} ` ` ` ` ` `// If n is not perfect square ` ` ` `else` `{ ` ` ` `if` `(i & 1) ` ` ` `odd_factors++; ` ` ` ` ` `int` `factor = n / i; ` ` ` `if` `(factor & 1) ` ` ` `odd_factors++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `odd_factors - 1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// N as sum of consecutive numbers ` ` ` `long` `long` `int` `N = 15; ` ` ` `cout << countOddFactors(N) << endl; ` ` ` ` ` `N = 10; ` ` ` `cout << countOddFactors(N) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number ` `// of ways to express N as sum ` `// of consecutive numbers. ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` `// returns the number ` `// of odd factors ` `static` `int` `countOddFactors(` `long` `n) ` `{ ` ` ` `int` `odd_factors = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `1` `; ` `1` `* i * i <= n; i++) ` ` ` `{ ` ` ` `if` `(n % i == ` `0` `) ` ` ` `{ ` ` ` ` ` `// If i is an odd factor and ` ` ` `// n is a perfect square ` ` ` `if` `(` `1` `* i * i == n) ` ` ` `{ ` ` ` `if` `((i & ` `1` `) == ` `1` `) ` ` ` `odd_factors++; ` ` ` `} ` ` ` ` ` `// If n is not ` ` ` `// perfect square ` ` ` `else` `{ ` ` ` `if` `((i & ` `1` `) == ` `1` `) ` ` ` `odd_factors++; ` ` ` ` ` `int` `factor = (` `int` `)n / i; ` ` ` `if` `((factor & ` `1` `) == ` `1` `) ` ` ` `odd_factors++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `odd_factors - ` `1` `; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `// N as sum of consecutive numbers ` ` ` `long` `N = ` `15` `; ` ` ` `System.out.println(countOddFactors(N)); ` ` ` ` ` `N = ` `10` `; ` ` ` `System.out.println(countOddFactors(N)); ` `} ` `} ` ` ` `// This code is contributed by ` `// Manish Shaw(manishshaw1) ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count number ` `# of ways to express N as sum ` `# of consecutive numbers. ` ` ` `# returns the number ` `# of odd factors ` `def` `countOddFactors(n) : ` ` ` `odd_factors ` `=` `0` ` ` `i ` `=` `1` ` ` `while` `((` `1` `*` `i ` `*` `i) <` `=` `n) : ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `) : ` ` ` ` ` `# If i is an odd factor and ` ` ` `# n is a perfect square ` ` ` `if` `(` `1` `*` `i ` `*` `i ` `=` `=` `n) : ` ` ` `if` `(i & ` `1` `) : ` ` ` `odd_factors ` `=` `odd_factors ` `+` `1` ` ` ` ` `# If n is not perfect square ` ` ` `else` `: ` ` ` `if` `((i & ` `1` `)) : ` ` ` `odd_factors ` `=` `odd_factors ` `+` `1` ` ` ` ` `factor ` `=` `int` `(n ` `/` `i) ` ` ` `if` `(factor & ` `1` `) : ` ` ` `odd_factors ` `=` `odd_factors ` `+` `1` ` ` `i ` `=` `i ` `+` `1` ` ` `return` `odd_factors ` `-` `1` ` ` `# Driver Code ` ` ` `# N as sum of consecutive numbers ` `N ` `=` `15` `print` `(countOddFactors(N)) ` ` ` `N ` `=` `10` `print` `(countOddFactors(N)) ` ` ` `# This code is contributed by ` `# Manish Shaw(manishshaw1) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number of ` `// ways to express N as sum of ` `// consecutive numbers. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// returns the number ` ` ` `// of odd factors ` ` ` `static` `int` `countOddFactors(` `long` `n) ` ` ` `{ ` ` ` `int` `odd_factors = 0; ` ` ` ` ` `for` `(` `int` `i = 1; 1 * i * i <= n; i++) ` ` ` `{ ` ` ` `if` `(n % i == 0) ` ` ` `{ ` ` ` ` ` `// If i is an odd factor and ` ` ` `// n is a perfect square ` ` ` `if` `(1 * i * i == n) ` ` ` `{ ` ` ` `if` `((i & 1) == 1) ` ` ` `odd_factors++; ` ` ` `} ` ` ` ` ` `// If n is not ` ` ` `// perfect square ` ` ` `else` `{ ` ` ` `if` `((i & 1) == 1) ` ` ` `odd_factors++; ` ` ` ` ` `int` `factor = (` `int` `)n / i; ` ` ` `if` `((factor & 1) == 1) ` ` ` `odd_factors++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `odd_factors - 1; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `void` `Main() ` ` ` `{ ` ` ` `// N as sum of consecutive numbers ` ` ` `long` `N = 15; ` ` ` `Console.WriteLine(countOddFactors(N)); ` ` ` ` ` `N = 10; ` ` ` `Console.WriteLine(countOddFactors(N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count number ` `// of ways to express N as sum ` `// of consecutive numbers. ` ` ` `// returns the number ` `// of odd factors ` `function` `countOddFactors(` `$n` `) ` `{ ` ` ` `$odd_factors` `= 0; ` ` ` ` ` `for` `(` `$i` `= 1; 1 * ` `$i` `* ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `if` `(` `$n` `% ` `$i` `== 0) ` ` ` `{ ` ` ` ` ` `// If i is an odd factor and ` ` ` `// n is a perfect square ` ` ` `if` `(1 * ` `$i` `* ` `$i` `== ` `$n` `) ` ` ` `{ ` ` ` `if` `(` `$i` `& 1) ` ` ` `$odd_factors` `++; ` ` ` `} ` ` ` ` ` `// If n is not perfect square ` ` ` `else` `{ ` ` ` `if` `(` `$i` `& 1) ` ` ` `$odd_factors` `++; ` ` ` ` ` `$factor` `= ` `$n` `/ ` `$i` `; ` ` ` `if` `(` `$factor` `& 1) ` ` ` `$odd_factors` `++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `$odd_factors` `- 1; ` `} ` ` ` `// Driver Code ` ` ` `// N as sum of consecutive numbers ` `$N` `= 15; ` `echo` `(countOddFactors(` `$N` `) . (` `"\n"` `)); ` ` ` `$N` `= 10; ` `echo` `(countOddFactors(` `$N` `)); ` ` ` `// This code is contributed by ` `// Manish Shaw(manishshaw1) ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

3 1

The Time complexity for this program is O(N^0.5).

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:

- Expressing factorial n as sum of consecutive numbers
- Expressing a fraction as a natural number under modulo 'm'
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Maximum number of prime factors a number can have with exactly x factors
- Queries on sum of odd number digit sums of all the factors of a number
- Find number of factors of N when location of its two factors whose product is N is given
- Check if a number exists having exactly N factors and K prime factors
- Find sum of odd factors of a number
- Sum of all odd factors of numbers in the range [l, r]
- Number of elements with odd factors in given range
- Check whether count of odd and even factors of a number are equal
- First element of every K sets having consecutive elements with exactly K prime factors less than N
- Sum of Factors of a Number using Prime Factorization
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Generate an alternate odd-even sequence having sum of all consecutive pairs as a perfect square
- Number of factors of very large number N modulo M where M is any prime number
- Sum of M maximum distinct digit sum from 1 to N that are factors of K
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Find four factors of N with maximum product and sum equal to N | Set-2

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.