Count ways to express a number as sum of consecutive numbers - GeeksforGeeks

03 Feb, 2022
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 + 54 + 5 + 67 + 8
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. 
 
YouTubeGeeksforGeeks Practice25.5K subscribersCount of Sum of Consecutives | Recently asked questions in DE Shaw & Co. InterviewsWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 53:14•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=uKcvoXmfXZc" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
?list=PLM68oyaqFM7Q-sv3gA5xbzfgVkoQ0xDrW 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to count number of ways to express// N as sum of consecutive numbers.#include <bits/stdc++.h>using namespace std; long int countConsecutive(long int N){    // constraint on values of L gives us the    // time Complexity as O(N^0.5)    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;} // Driver Codeint main(){    long int N = 15;    cout << countConsecutive(N) << endl;    N = 10;    cout << countConsecutive(N) << endl;    return 0;}
// A Java program to count number of ways// to express N as sum of consecutive numbers.public class SumConsecutiveNumber {    // Utility method to compute number of ways    // in which N can be represented as sum of    // consecutive number    static int countConsecutive(int N)    {        // constraint on values of L gives us the        // time Complexity as O(N^0.5)        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;    }     // Driver code to test above function    public static void main(String[] args)    {        int N = 15;        System.out.println(countConsecutive(N));        N = 10;        System.out.println(countConsecutive(N));    }}// This code is contributed by Sumit Ghosh
# Python program to count number of ways to# express N as sum of consecutive numbers. def countConsecutive(N):         # constraint on values of L gives us the    # time Complexity as O(N ^ 0.5)    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 # Driver code N = 15print (countConsecutive(N))N = 10print (countConsecutive(N)) # This code is contributed by Sachin Bisht
// A C# program to count number of// ways to express N as sum of// consecutive numbers.using System; public class GFG {     // Utility method to compute    // number of ways in which N    // can be represented as sum    // of consecutive number    static int countConsecutive(int N)    {         // constraint on values of L        // gives us the time        // Complexity as O(N^0.5)        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;    }     // Driver code to test above    // function    public static void Main()    {        int N = 15;        Console.WriteLine(            countConsecutive(N));         N = 10;        Console.Write(            countConsecutive(N));    }} // This code is contributed by// nitin mittal.
<?php// PHP program to count number// of ways to express N as sum// of consecutive numbers. function countConsecutive($N){    // constraint on values    // of L gives us the    // time Complexity as O(N^0.5)    $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;} // Driver Code$N = 15;echo countConsecutive($N), "\n";$N = 10;echo countConsecutive($N), "\n"; // This code is contributed by ajit?>
<script>    // A Javascript program to count number of    // ways to express N as sum of    // consecutive numbers.         // Utility method to compute    // number of ways in which N    // can be represented as sum    // of consecutive number    function countConsecutive(N)    {                   // constraint on values of L        // gives us the time        // Complexity as O(N^0.5)        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>
  
3
1
 
 
Time Complexity: O(N^0.5)
 
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 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.
 
nitin mittal
jit_t
rameshtravel07
snape_here
amartyaghoshgfg
Linkedin
Visa
Mathematical
Visa
Linkedin
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Print all possible combinations of r elements in a given array of size n
Sieve of Eratosthenes
Operators in C / C++
Program for factorial of a number
Find minimum number of coins that make a given value
The Knight's tour problem | Backtracking-1