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 firstname.lastname@example.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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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
- Ways to express a number as product of two different factors
- Count number of ways to arrange first N numbers
- Express an odd number as sum of prime numbers
- Count of ways to write N as a sum of three numbers
- Count of N-digit numbers in base K with no two consecutive zeroes
- Count number of ways to get Odd Sum
- Count number of binary strings without consecutive 1’s : Set 2
- Count number of ways to divide a number in 4 parts
- Find the number of consecutive zero at the end after multiplying n numbers
- Check if a number can be expressed as a sum of consecutive numbers
- Count number of ways to cover a distance | Set 2
- Bell Numbers (Number of ways to Partition a Set)