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

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 the other odd.

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

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

Below is the implementation of the 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;` `}` |

## 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"` `);` ` ` `}` `}` |

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

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

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

## Javascript

`<script>` ` ` `// Javascript 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))!=0) && n!=0);` ` ` `}` ` ` ` ` `// function call` ` ` ` ` `let n = 15;` ` ` `document.write(canBeSumofConsec(n) ? ` `"true"` `: ` `"false"` `);` ` ` `</script>` |

**Output: **

True

Time Complexity: O(1)

Auxiliary Space: O(1)

**Another Approach :**

Let number chosen to represent N as a sum of consecutive numbers be** X + 1, X + 2, X + 3 …. Y **

Sum of these chosen numbers = **Sum of first Y natural numbers – Sum of first X natural number **

Sum of first Y natural number =

Sum of first X natural number =

We know that,N = Sum of first Y natural number – Sum of first X natural number

Let Y – X = a, Y + X + 1 = b

Y + X + 1 > Y – X, b > a

,2N = a * b

It means that, we know thata and b are factor of 2Nso,X and Y are integers

1.b – a – 1 => multiple of 2 (Even number)

2.b + a + 1 => multiple of 2 (Even number)

Both conditions must be satisfied

From 1 and 2 we can say that **either one of them (a, b) should be Odd and another one Even**

So if the number** (2N) **has only

**odd factors (can’t be possible as it is an even number (2N not N) )**or

**only even factors we can’t represent it as a sum of any consecutive natural numbers**

So now, we have to now **only check whether it has an odd factor or not**

1. If the number ** (2N not N)** does

**not have any odd factor**(contains only

**even factor**means can be

**represented as**) then

__we can’t represent it as a sum of consecutive number__2. If the number **(2N not N)** has an** odd factor** then __we can represent it as a sum of a consecutive number__

After this we have to only check whether we can

represent(2N asor not)

- if
Yesthen answer isfalse or 0- if
Nothen answer istrue or 1

Below is the implementation of the above idea :

## C++14

`#include <bits/stdc++.h>` `using` `namespace` `std;` ` ` `long` `long` `int` `canBeSumofConsec(` `long` `long` `int` `n) ` `{ ` ` ` `// Updating n with 2n` ` ` `n = 2 * n;` ` ` `// (n & (n - 1)) => Checking whether we can write 2n as 2^k` ` ` `// if yes (can't represent 2n as 2^k) then answer 1` ` ` `// if no (can represent 2n as 2^k) then answer 0` ` ` `return` `((n & (n - 1)) != 0);` `}` ` ` `int` `main()` `{` ` ` `long` `long` `int` `n = 10;` ` ` `cout<<canBeSumofConsec(n)<<` `"\n"` `;` `}` |

## C

`#include <stdio.h>` ` ` `long` `long` `int` `canBeSumofConsec(` `long` `long` `int` `n) ` `{ ` ` ` `// Updating n with 2n` ` ` `n = 2 * n;` ` ` `// (n & (n - 1)) => Checking whether we can write 2n as 2^k` ` ` `// if yes (can't represent 2n as 2^k) then answer 1` ` ` `// if no (can represent 2n as 2^k) then answer 0` ` ` `return` `((n & (n - 1)) != 0);` `}` ` ` `int` `main()` `{` ` ` `long` `long` `int` `n = 10;` ` ` `printf` `(` `"%lld"` `, canBeSumofConsec(n));` `}` |

## Java

`import` `java.util.*;` `class` `GFG{` ` ` ` ` `static` `int` `canBeSumofConsec( ` `int` `n) ` `{ ` ` ` `// Updating n with 2n` ` ` `n = ` `2` `* n;` ` ` ` ` `// (n & (n - 1)) => Checking whether we can write 2n as 2^k` ` ` `// if yes (can't represent 2n as 2^k) then answer 1` ` ` `// if no (can represent 2n as 2^k) then answer 0` ` ` `return` `((n & (n - ` `1` `)) != ` `0` `)?` `1` `:` `0` `;` `}` ` ` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `n = ` `10` `;` ` ` `System.out.print(canBeSumofConsec(n)+` `"\n"` `);` `}` `}` ` ` `// This code is contributed by umadevi9616 ` |

## Python3

`def` `canBeSumofConsec(n):` ` ` ` ` `# Updating n with 2n` ` ` `n ` `=` `2` `*` `n;` ` ` ` ` `# (n & (n - 1)) => Checking whether we can write 2n as 2^k` ` ` `# if yes (can't represent 2n as 2^k) then answer 1` ` ` `# if no (can represent 2n as 2^k) then answer 0` ` ` `if` `((n & (n ` `-` `1` `)) !` `=` `0` `):` ` ` `return` `1` `;` ` ` `else` `:` ` ` `return` `0` `;` ` ` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `n ` `=` `10` `;` ` ` `print` `(canBeSumofConsec(n));` ` ` `# This code is contributed by umadevi9616 ` |

## C#

`using` `System;` ` ` `public` `class` `GFG {` ` ` ` ` `static` `int` `canBeSumofConsec(` `int` `n)` ` ` `{` ` ` ` ` `// Updating n with 2n` ` ` `n = 2 * n;` ` ` ` ` `// (n & (n - 1)) => Checking whether we can write 2n as 2^k` ` ` `// if yes (can't represent 2n as 2^k) then answer 1` ` ` `// if no (can represent 2n as 2^k) then answer 0` ` ` `return` `((n & (n - 1)) != 0) ? 1 : 0;` ` ` `}` ` ` ` ` `public` `static` `void` `Main(String[] args) {` ` ` `int` `n = 10;` ` ` `Console.Write(canBeSumofConsec(n) + ` `"\n"` `);` ` ` `}` `}` ` ` `// This code is contributed by umadevi9616` |

## Javascript

`<script>` ` ` ` ` `function` `canBeSumofConsec(n) {` ` ` `// Updating n with 2n` ` ` `n = 2 * n;` ` ` ` ` `// (n & (n - 1)) => Checking whether we can write 2n as 2^k` ` ` `// if yes (can't represent 2n as 2^k) then answer 1` ` ` `// if no (can represent 2n as 2^k) then answer 0` ` ` `return` `((n & (n - 1)) != 0) ? 1 : 0;` ` ` `}` ` ` ` ` `var` `n = 10;` ` ` `document.write(canBeSumofConsec(n) + ` `"\n"` `);` ` ` `// This code is contributed by umadevi9616` `</script>` |

**Output**

1

Time Complexity: O(1)

Auxiliary Space: O(1)

**Reference:**

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

This article is contributed by **Sahil Chhabra**. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.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.