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.

Examples:

Input : n = 10 Output : true It can be expressed as sum of two consecutive numbers 1 + 2 + 3 + 4. Input : n = 16 Output : false It cannot be expressed as sum of two consecutive numbers. Input : n = 5 Output : true 2 + 3 = 5

There is a direct and quick method to solve this. If a number is a power of two, then it cannot be expressed as a sum of consecutive numbers otherwise Yes.

The idea is based on below two facts.

1) Sum of any two consecutive numbers is odd as one of them has to be even and other odd.

2) 2^{n} = 2^{n-1} + 2^{n-1}

If we take a closer look at 1) and 2), we can get intuition behind the fact.

Below is implementation of above idea.

## C++

`// C++ program to check if a number can ` `// be expressed as sum of consecutive numbers ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// This function returns true if n can be ` `// expressed sum of consecutive. ` `bool` `canBeSumofConsec(unsigned ` `int` `n) ` `{ ` ` ` `// We basically return true if n is a ` ` ` `// power of two ` ` ` `return` `((n&(n-1)) && n); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `unsigned ` `int` `n = 15; ` ` ` `canBeSumofConsec(n)? cout << ` `"true"` `: ` ` ` `cout << ` `"false"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check if a number can ` `// be expressed as sum of consecutive numbers ` ` ` `class` `Test ` `{ ` ` ` `// This function returns true if n can be ` ` ` `// expressed sum of consecutive. ` ` ` `static` `boolean` `canBeSumofConsec(` `int` `n) ` ` ` `{ ` ` ` `// We basically return true if n is a ` ` ` `// power of two ` ` ` `return` `(((n&(n-` `1` `))!=` `0` `) && n!=` `0` `); ` ` ` `} ` ` ` ` ` `// Driver method ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `15` `; ` ` ` `System.out.println(canBeSumofConsec(n) ? ` `"true"` `: ` `"false"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to check if a number can ` `# be expressed as sum of consecutive numbers ` ` ` ` ` `# This function returns true if n ` `# can be expressed sum of consecutive. ` `def` `canBeSumofConsec(n) : ` ` ` ` ` `# We basically return true if n is a ` ` ` `# power of two ` ` ` `return` `((n&(n` `-` `1` `)) ` `and` `n) ` ` ` ` ` `# Driver code ` `n ` `=` `15` `if` `(canBeSumofConsec(n)) : ` ` ` `print` `(` `"true"` `) ` `else` `: ` ` ` `print` `(` `"false"` `) ` ` ` `# This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check if a number can be ` `// expressed as sum of consecutive numbers ` `using` `System; ` ` ` `class` `Test ` `{ ` ` ` `// This function returns true if n ` ` ` `// can be expressed sum of consecutive. ` ` ` `static` `bool` `canBeSumofConsec(` `int` `n) ` ` ` `{ ` ` ` `// We basically return true if n is a ` ` ` `// power of two ` ` ` `return` `(((n & (n - 1)) != 0) && n != 0); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 15; ` ` ` `Console.Write(canBeSumofConsec(n) ? ` `"True"` `: ` `"False"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nitin Mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// php program to check if a number ` `// can be expressed as sum of ` `// consecutive numbers ` ` ` `// This function returns true if n ` `// can be expressed sum of consecutive. ` `function` `canBeSumofConsec(` `$n` `) ` `{ ` ` ` ` ` `// We basically return true if n is a ` ` ` `// power of two ` ` ` `return` `((` `$n` `& (` `$n` `- 1)) && ` `$n` `); ` `} ` ` ` `// Driver code ` ` ` `$n` `= 15; ` ` ` `if` `(canBeSumofConsec(` `$n` `)) ` ` ` `echo` `"true"` `; ` ` ` `else` ` ` `echo` `"false"` `; ` ` ` `// This code is contributed by ` `// nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

Output:

True

**Reference:**

http://www.cut-the-knot.org/arithmetic/UnpropertyOfPowersOf2.shtml

This article is contributed by **Sahil Chhabra(KILLER)**. 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 prime numbers that can be expressed as sum of consecutive prime numbers
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Count primes that can be expressed as sum of two consecutive primes and 1
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if a number can be expressed as sum two abundant numbers
- Check whether a number can be expressed as a product of single digit numbers
- Check if a number can be expressed as sum of two Perfect powers
- Number expressed as sum of five consecutive integers
- Check if N can be expressed as product of 3 distinct numbers
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Check if an integer can be expressed as a sum of two semi-primes
- Check if a number can be expressed as x^y (x raised to power y)
- Check if a number can be expressed as power | Set 2 (Using Log)
- Check if a number can be expressed as a^b | Set 2
- Check if a number can be expressed as 2^x + 2^y
- Check if a number can be expressed as a product of exactly K prime divisors
- Check if a number N can be expressed in base B
- Elements of Array which can be expressed as power of prime numbers
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Count of primes below N which can be expressed as the sum of two primes