Given a number n, find the number of ways to represent this number as a sum of 2 or more consecutive natural numbers.
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
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.
The Time complexity for this program is O(N^0.5).
- Expressing factorial n as sum of consecutive numbers
- Expressing a fraction as a natural number under modulo 'm'
- Queries on sum of odd number digit sums of all the factors of a number
- Number with maximum number of prime factors
- Sum of all the factors of a number
- Find sum of even factors of a number
- Prime factors of a big number
- Find sum of odd factors of a number
- Product of factors of number
- Number of elements with even factors in the given range
- Sum of Factors of a Number using Prime Factorization
- Maximize the product of four factors of a Number
- Find minimum sum of factors of number
- Number of elements with odd factors in given range
- Check whether a number has exactly three distinct factors or not
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : manishshaw1