Given an integer N, the task is to 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
Explanation:
15 can be represented as:
- 1 + 2 + 3 + 4 + 5
- 4 + 5 + 6
- 7 + 8
Input: N = 10
Output: 1
Approach: 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.
?list=PLM68oyaqFM7Q-sv3gA5xbzfgVkoQ0xDrW
C++
#include <bits/stdc++.h>
using namespace std;
long int countConsecutive( long int N)
{
long int count = 0;
for ( long int L = 1; L * (L + 1) < 2 * N; L++) {
double a = (1.0 * N - (L * (L + 1)) / 2) / (L + 1);
if (a - ( int )a == 0.0)
count++;
}
return count;
}
int main()
{
long int N = 15;
cout << countConsecutive(N) << endl;
N = 10;
cout << countConsecutive(N) << endl;
return 0;
}
|
Java
public class SumConsecutiveNumber {
static int countConsecutive( int N)
{
int count = 0 ;
for ( int L = 1 ; L * (L + 1 ) < 2 * N; L++) {
double a = ( double )(( 1.0 * N - (L * (L + 1 )) / 2 ) / (L + 1 ));
if (a - ( int )a == 0.0 )
count++;
}
return count;
}
public static void main(String[] args)
{
int N = 15 ;
System.out.println(countConsecutive(N));
N = 10 ;
System.out.println(countConsecutive(N));
}
}
|
Python3
def countConsecutive(N):
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
N = 15
print (countConsecutive(N))
N = 10
print (countConsecutive(N))
|
C#
using System;
public class GFG {
static int countConsecutive( int N)
{
int count = 0;
for ( int L = 1; L * (L + 1)
< 2 * N;
L++) {
double a = ( double )((1.0
* N
- (L * (L + 1))
/ 2)
/ (L + 1));
if (a - ( int )a == 0.0)
count++;
}
return count;
}
public static void Main()
{
int N = 15;
Console.WriteLine(
countConsecutive(N));
N = 10;
Console.Write(
countConsecutive(N));
}
}
|
PHP
<?php
function countConsecutive( $N )
{
$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 ;
}
$N = 15;
echo countConsecutive( $N ), "\n" ;
$N = 10;
echo countConsecutive( $N ), "\n" ;
?>
|
Javascript
<script>
function countConsecutive(N)
{
let count = 0;
for (let L = 1; L * (L + 1) < 2 * N; L++)
{
let a = ((1.0 * N-(L * (L + 1)) / 2) / (L + 1));
if (a - parseInt(a, 10) == 0.0)
count++;
}
return count;
}
let N = 15;
document.write(countConsecutive(N) + "</br>" );
N = 10;
document.write(countConsecutive(N));
</script>
|
Time Complexity: O(N^0.5)
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Jun, 2022
Like Article
Save Article