Given a number N, find the number of ways to represent this number as a sum of 2 or more consecutive natural numbers.
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.
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 email@example.com. 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.
- Count digits in a factorial | Set 2
- How to swap two numbers without using a temporary variable?
- Write a program to reverse digits of a number
- Lucky Numbers
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Ugly Numbers
- Write a program to add two numbers in base 14
- Minimum number of jumps to reach end
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Print all combinations of points that can compose a given number
- Count of Binary Digit numbers smaller than N
- Program for Fibonacci numbers
- Check if a number is multiple of 5 without using / and % operators