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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print a consecutive sequence ` `// to express N if possible. ` `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.printf(N + ` `" = "` `); ` ` ` `printConsecutive(last, first+` `1` `); ` ` ` `return` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `System.out.print(` `"-1"` `); ` ` ` `} ` ` ` ` ` `// main function ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `12` `; ` ` ` `findConsecutive(n); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

Output:

12 = 3 + 4 + 5

This article is contributed by **Roshni Agarwal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count ways to express a number as sum of consecutive numbers
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Express an odd number as sum of prime numbers
- Count ways to express a number as sum of exactly two numbers
- Minimum numbers needed to express every integer below N as a sum
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Count ways to express even number ānā as sum of even integers
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of palindromes required to express N as a sum | Set 2
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Check if a number can be expressed as a sum of consecutive numbers
- Minimum number of distinct powers of 2 required to express a given binary number
- Find the prime numbers which can written as sum of most consecutive primes
- Expressing factorial n as sum of consecutive numbers
- Sum of Hamming difference of consecutive numbers from 0 to N | Set 2
- Count of N digit Numbers whose sum of every K consecutive digits is equal
- Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2
- Ways to express a number as product of two different factors
- Find the number of consecutive zero at the end after multiplying n numbers