Open In App

Count ways to express a number as sum of consecutive numbers

Last Updated : 23 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:
Explanation: 
15 can be represented as: 
 

  1. 1 + 2 + 3 + 4 + 5
  2. 4 + 5 + 6
  3. 7 + 8

Input: N = 10 
Output:
 

 

Recommended Practice

 

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++




// 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 Code
int main()
{
    long int N = 15;
    cout << countConsecutive(N) << endl;
    N = 10;
    cout << countConsecutive(N) << endl;
    return 0;
}


Java




// 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


Python3




# 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 = 15
print (countConsecutive(N))
N = 10
print (countConsecutive(N))
 
# This code is contributed by Sachin Bisht


C#




// 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
// 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
?>


Javascript




<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>


 
 

Output: 

3
1

 

 

Time Complexity: O(N^0.5)

Auxiliary Space: O(1)

 

 

 



Similar Reads

Count ways to express a number as sum of exactly two numbers
Given a positive integer N. The task is to find the number of ways in which you can express N as a sum of exactly two numbers A and B (N = A + B) where A &gt; 0, B &gt; 0 and B &gt; A. Examples: Input: N = 8 Output: 3 Explanation: N = 8 can be expressed as (1, 7), (2, 6), (3, 5) Input: N = 14 Output: 6 Approach: An observation here is that for ever
3 min read
Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
Given an integer N, the task is to print N integers ? 109 such that no two consecutive of these integers are co-prime and every 3 consecutive are co-prime. Examples: Input: N = 3 Output: 6 15 10Input: N = 4 Output: 6 15 35 14 Approach: We can just multiply consecutive primes and for the last number just multiply the gcd(last, last-1) * 2. We do thi
22 min read
Express a number as sum of consecutive numbers
Given a number N, write a function to express N as sum of two or more consecutive positive numbers. If there is no solution, output -1. If there are multiple solution, then print one of them.Examples: Input : N = 10 Output : 4 + 3 + 2 + 1 Input : N = 8 Output : -1 Input : N = 24 Output : 9 + 8 + 7 Recommended PracticeConsecutive numbers for sumTry
5 min read
Count ways to express a number as sum of powers
Given two integers x and n, we need to find number of ways to express x as sum of n-th powers of unique natural numbers. It is given that 1 &lt;= n &lt;= 20.Examples: Input : x = 100 n = 2 Output : 3 Explanation: There are three ways to express 100 as sum of natural numbers raised to power 2. 100 = 10^2 = 8^2+6^2 = 1^2+3^2+4^2+5^2+7^2 Input : x = 1
5 min read
Count ways to express even number ‘n’ as sum of even integers
Given an positive even integer 'n'. Count total number of ways to express ‘n’ as sum of even positive integers. Output the answer in modulo 109 + 7Examples: Input: 6 Output: 4 Explanation There are only four ways to write 6 as sum of even integers: 1. 2 + 2 + 2 2. 2 + 4 3. 4 + 2 4. 6 Input: 8 Output: 8 Approach is to find pattern or recursive funct
7 min read
Count ways to express 'n' as sum of odd integers
Given an positive integer n. Count total number of ways to express 'n' as sum of odd positive integers. Examples: Input: 4 Output: 3 Explanation There are only three ways to write 4 as sum of odd integers: 1. 1 + 3 2. 3 + 1 3. 1 + 1 + 1 + 1 Input: 5 Output: 5 Simple approach is to find recursive nature of problem. The number 'n' can be written as s
6 min read
Count of different ways to express N as the sum of 1, 3 and 4
Given N, count the number of ways to express N as sum of 1, 3 and 4. Examples: Input : N = 4 Output : 4 Explanation: 1+1+1+1 1+3 3+1 4 Input : N = 5 Output : 6 Explanation: 1 + 1 + 1 + 1 + 1 1 + 4 4 + 1 1 + 1 + 3 1 + 3 + 1 3 + 1 + 1Recommended PracticeCount ways to express N as the sum of 1,3 and 4Try It! Approach : Divide the problem into sub-prob
9 min read
Count ways to form Triplet of consecutive integers having the given numbers
Given an integer N (N ? 3), two distinct numbers X and Y, the task is to find the maximum number of possible ways by which a third drawn number (the third number lies in range [1, N]) can make a triplet of consecutive with given two numbers. Also, print the triplets. Examples: Input: N = 3, X = 2, Y = 3Output:11 2 3Explanation: There is only 1 way
12 min read
Count prime numbers that can be expressed as sum of consecutive prime numbers
Given an integer N, the task is to find the number of prime numbers up to N that can be expressed as a sum of consecutive primes. Examples: Input: N = 45Output: 3Explanation:Below are the prime numbers up to 45 that can be expressed as sum of consecutive prime numbers: 5 = 2 + 317 = 2 + 3 + 5 + 741 = 2 + 3 + 5 + 7 + 11 + 13 Therefore, the count is
8 min read
Count of ways to generate Sequence of distinct consecutive odd integers with sum N
Given an integer N, the task is to find the total number of ways a sequence can be formed consisting of distinct consecutive odd integers that add up to N. Examples: Input: N = 45Output: 3Explanation: 3 ways to choose distinct consecutive odd numbers that add up to 45 are - {5, 7, 9, 11, 13}, {13, 15, 17} and {45}. Input : N = 20Output : 1Explanati
6 min read
Article Tags :