Express a number as sum of consecutive numbers
Last Updated :
04 Oct, 2021
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
Sum of first n natural numbers = n * (n + 1)/2
Sum of first (n + k) numbers = (n + k) * (n + k + 1)/2
If N is sum of k consecutive numbers, then
following must be true.
N = [(n+k)(n+k+1) - n(n+1)] / 2
OR
2 * N = [(n+k)(n+k+1) - n(n+1)]
Below is the implementation based on above idea.
C++
// C++ program to print a consecutive sequence
// to express N if possible.
#include <bits/stdc++.h>
using namespace std;
// Print consecutive numbers from
// last to first
void printConsecutive(int last, int first)
{
cout << first++;
for (int x = first; x<= last; x++)
cout << " + " << x;
}
void findConsecutive(int N)
{
for (int last=1; last<N; last++)
{
for (int first=0; first<last; first++)
{
if (2*N == (last-first)*(last+first+1))
{
cout << N << " = ";
printConsecutive(last, first+1);
return;
}
}
}
cout << "-1";
}
// Driver code
int main()
{
int n = 12;
findConsecutive(n);
return 0;
}
Java
// Java program to print a consecutive sequence
// to express N if possible.
import java.util.*;
class GFG
{
// Print consecutive numbers from
// last to first
static void printConsecutive(int last, int first)
{
System.out.print(first++);
for (int x = first; x<= last; x++)
System.out.print(" + " + x);
}
static void findConsecutive(int N)
{
for (int last = 1; last < N; last++)
{
for (int first = 0; first < last; first++)
{
if (2*N == (last-first)*(last+first+1))
{
System.out.print(N+ " = ");
printConsecutive(last, first+1);
return;
}
}
}
System.out.print("-1");
}
// Driver code
public static void main(String[] args)
{
int n = 12;
findConsecutive(n);
}
}
// This code is contributed by umadevi9616
Python3
# Python3 program to print a consecutive
# sequence to express N if possible.
# Print consecutive numbers
# from last to first
def printConsecutive(last, first):
print (first, end = "")
first += 1
for x in range(first, last + 1):
print (" +", x, end = "")
def findConsecutive(N):
for last in range(1, N):
for first in range(0, last):
if 2 * N == (last - first) * (last + first + 1):
print (N, "= ", end = "")
printConsecutive(last, first + 1)
return
print ("-1")
# Driver code
n = 12
findConsecutive(n)
# This code is contributed by Shreyanshi Arun.
C#
// C# program to print a consecutive sequence
// to express N if possible.
using System;
class GfG
{
// Print consecutive numbers from
// last to first
static void printConsecutive(int last, int first)
{
Console.Write(first++);
for (int x = first; x <= last; x++)
Console.Write(" + "+x);
}
static void findConsecutive(int N)
{
for (int last = 1; last < N; last++)
{
for (int first = 0; first < last; first++)
{
if (2 * N == (last - first)
* (last + first + 1))
{
Console.Write(N + " = ");
printConsecutive(last, first + 1);
return;
}
}
}
Console.Write("-1");
}
// Driver code
public static void Main ()
{
int n = 12;
findConsecutive(n);
}
}
// This code is contributed by vt_m
PHP
<?php
// PHP program to print a consecutive
// sequence to express N if possible.
// Print consecutive numbers from
// last to first
function printConsecutive($last, $first)
{
echo $first++;
for ($x = $first; $x<= $last; $x++)
echo " + " , $x;
}
function findConsecutive($N)
{
for ($last = 1; $last < $N; $last++)
{
for ($first = 0; $first < $last; $first++)
{
if (2 * $N == ($last - $first) *
($last + $first + 1))
{
echo $N , " = ";
printConsecutive($last, $first + 1);
return;
}
}
}
echo "-1";
}
// Driver Code
$n = 12;
findConsecutive($n);
// This code is contributed by nitin mittal
?>
JavaScript
<script>
// Javascript program to print a consecutive
// sequence to express N if possible.
// Print consecutive numbers from
// last to first
function printConsecutive(last, first)
{
document.write(first++);
for (let x = first; x<= last; x++)
document.write( " + " + x);
}
function findConsecutive(N)
{
for (let last = 1; last < N; last++)
{
for (let first = 0; first < last; first++)
{
if (2 * N == (last - first) *
(last + first + 1))
{
document.write(N + " = ");
printConsecutive(last, first + 1);
return;
}
}
}
document.write("-1");
}
// Driver Code
let n = 12;
findConsecutive(n);
// This code is contributed by _saurabh_jaiswal
</script>
Output:
12 = 3 + 4 + 5
Reference :
https://math.stackexchange.com/questions/139842/in-how-many-ways-can-a-number-be-expressed-as-a-sum-of-consecutive-numbers
Similar Reads
Check if a number can be expressed as a sum of consecutive numbers Given a number n, the task is to check whether it can be expressed as a sum of two or more consecutive numbers or not. Example Input : n = 10 Output : trueIt can be expressed as sum of two consecutivenumbers 1 + 2 + 3 + 4.Input : n = 16 Output : falseIt cannot be expressed as sum of two consecutiven
12 min read
Number expressed as sum of five consecutive integers Given an integer n, the task is to find whether n can be expressed as sum of five consecutive integer. If yes, find the five consecutive integers, else print â-1â.Examples: Input : n = 15 Output : 1 2 3 4 5 15 = 1 + 2 + 3 + 4 + 5 Input : n = 18 Output : -1 Method 1: (Brute Force) The idea is to run
11 min read
Expressing factorial n as sum of consecutive numbers Given two numbers N and M. Find the number of ways in which factorial N can be expressed as a sum of two or more consecutive numbers. Print the result modulo M.Examples: Input : N = 3, M = 7 Output : 1 Explanation: 3! can be expressed in one way, i.e. 1 + 2 + 3 = 6. Hence 1 % 7 = 1 Input : N = 4, M
13 min read
Expressing a number as sum of consecutive | Set 2 (Using odd factors) Given a number n, 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 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 disc
6 min read
Sum of consecutive two elements in a array Given an array print sum of the pairwise consecutive elements. Examples: Input : 8, 5, 4, 3, 15, 20 Output : 13, 9, 7, 18, 35 Input : 5, 10, 15, 20 Output : 15, 25, 35 The solution is to traverse the array and saving the sum of consecutive numbers in the variable sum. Implementation: C++ // C++ prog
3 min read