Given a positive integer n, find whether it can be represented as the sum of two or more consecutive positive integers.
Examples:
Input: n = 10
Output: true
Explanation: 10 can be expressed as: 1 + 2 + 3 + 4 = 10.
Input: n = 8
Output: false
Explanation: 8 cannot be expressed as the sum of two or more consecutive positive integers.
Input: n = 24
Output: true
Explanation: 24 can be expressed as: 7 + 8 + 9 = 24.
Table of Content
[Naive Approach] Checking All Consecutive Sums - (n2) Time O(1) Space
The idea is to start from every number and keep adding consecutive numbers until the sum becomes equal to or greater than
n. If the sum becomes equal ton, return"true".
The sum of first n natural numbers is: n * (n + 1) / 2.
The sum of first (x + k) natural numbers is: (x + k) * (x + k + 1) / 2.
If n is the sum of k consecutive numbers, then:
n = [(x + k)(x + k + 1) - x(x + 1)] / 2
Or,
2 * n = [(x + k)(x + k + 1) - x(x + 1)]
So, we try all possible values and check whether the condition becomes true.
#include <iostream>
using namespace std;
// C++ program to check whether a number
// can be expressed as sum of consecutive numbers
bool isSumOfConsecutive(int n)
{
// Try every starting number
for (int i = 1; i < n; i++)
{
int sum = 0;
// Generate consecutive sum
for (int j = i; j < n; j++)
{
sum += j;
// If sum becomes equal to n
// and at least two numbers are used
if (sum == n && j > i)
{
return true;
}
// If sum exceeds n
if (sum > n)
{
break;
}
}
}
return false;
}
// Driver code
int main() {
int n = 10;
if (isSumOfConsecutive(n)) {
cout << "true";
}
else {
cout << "false";
}
return 0;
}
#include <stdio.h>
// C program to check whether a number
// can be expressed as sum of consecutive numbers
int isSumOfConsecutive(int n)
{
// Try every starting number
for (int i = 1; i < n; i++)
{
int sum = 0;
// Generate consecutive sum
for (int j = i; j < n; j++)
{
sum += j;
// If sum becomes equal to n
// and at least two numbers are used
if (sum == n && j > i)
{
return 1;
}
// If sum exceeds n
if (sum > n)
{
break;
}
}
}
return 0;
}
// Driver code
int main() {
int n = 10;
if (isSumOfConsecutive(n)) {
printf("true");
}
else {
printf("false");
}
return 0;
}
public class GfG {
// Java program to check whether a number
// can be expressed as sum of consecutive numbers
static boolean isSumOfConsecutive(int n)
{
// Try every starting number
for (int i = 1; i < n; i++)
{
int sum = 0;
// Generate consecutive sum
for (int j = i; j < n; j++)
{
sum += j;
// If sum becomes equal to n
// and at least two numbers are used
if (sum == n && j > i)
{
return true;
}
// If sum exceeds n
if (sum > n)
{
break;
}
}
}
return false;
}
// Driver code
public static void main(String[] args) {
int n = 10;
if (isSumOfConsecutive(n)) {
System.out.println("true");
}
else {
System.out.println("false");
}
}
}
def isSumOfConsecutive(n):
# Try every starting number
for i in range(1, n):
sum = 0
# Generate consecutive sum
for j in range(i, n):
sum += j
# If sum becomes equal to n
# and at least two numbers are used
if sum == n and j > i:
return True
# If sum exceeds n
if sum > n:
break
return False
# Driver code
if __name__ == "__main__":
n = 10
if isSumOfConsecutive(n):
print("true")
else:
print("false")
using System;
class GfG {
// C# program to check whether a number
// can be expressed as sum of consecutive numbers
static bool isSumOfConsecutive(int n)
{
// Try every starting number
for (int i = 1; i < n; i++)
{
int sum = 0;
// Generate consecutive sum
for (int j = i; j < n; j++)
{
sum += j;
// If sum becomes equal to n
// and at least two numbers are used
if (sum == n && j > i)
{
return true;
}
// If sum exceeds n
if (sum > n)
{
break;
}
}
}
return false;
}
// Driver code
static void Main(string[] args) {
int n = 10;
if (isSumOfConsecutive(n)) {
Console.WriteLine("true");
}
else {
Console.WriteLine("false");
}
}
}
function isSumOfConsecutive(n) {
// Try every starting number
for (let i = 1; i < n; i++) {
let sum = 0;
// Generate consecutive sum
for (let j = i; j < n; j++) {
sum += j;
// If sum becomes equal to n
// and at least two numbers are used
if (sum === n && j > i) {
return true;
}
// If sum exceeds n
if (sum > n) {
break;
}
}
}
return false;
}
// Driver code
let n = 10;
if (isSumOfConsecutive(n)) {
console.log('true');
} else {
console.log('false');
}
Output
true
[Expected Approach] Using Power of 2 Property - O(1) Time O(1) Space
The idea is that a number can be written as the sum of consecutive positive numbers if it is not a power of
2. So, we simply check whethernis a power of2using bit manipulation.
Why a Power of 2 cannot be expressed?
The idea is based on the fact that every sum of consecutive integers must have at least one odd factor greater than 1. Let us see the proof.
Sum of k consecutive numbers 𝑘(2𝑎 + k - 1) / 2 = n
The above equation can be written as k(2a + k - 1) = 2n.
If k is even, then (k-1) is odd. Adding 2a (Which is even) to an odd number results in an odd number. Thus, one of these factors must be odd and the other must be even.
Why all other numbers can be expressed?
Every odd number n can be written as (2m + 1) which can be written as m + (m+1) which is sum of two consecutive.
#include <iostream>
using namespace std;
// C++ program to check whether a number
// can be expressed as sum of consecutive numbers
bool isSumOfConsecutive(int n)
{
// 1 cannot be represented
if (n == 1)
{
return false;
}
// Check if n is power of 2
if ((n & (n - 1)) == 0)
{
return false;
}
return true;
}
// Driver code
int main()
{
int n = 10;
if (isSumOfConsecutive(n))
cout << "true";
else
cout << "false";
return 0;
}
#include <stdio.h>
// C program to check whether a number
// can be expressed as sum of consecutive numbers
int isSumOfConsecutive(int n)
{
// 1 cannot be represented
if (n == 1)
{
return 0;
}
// Check if n is power of 2
if ((n & (n - 1)) == 0)
{
return 0;
}
return 1;
}
// Driver code
int main()
{
int n = 10;
if (isSumOfConsecutive(n))
printf("true");
else
printf("false");
return 0;
}
public class GfG {
// Java program to check whether a number
// can be expressed as sum of consecutive numbers
static boolean isSumOfConsecutive(int n) {
// 1 cannot be represented
if (n == 1) {
return false;
}
// Check if n is power of 2
if ((n & (n - 1)) == 0) {
return false;
}
return true;
}
// Driver code
public static void main(String[] args) {
int n = 10;
if (isSumOfConsecutive(n)) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
# Python3 program to check whether a number
# can be expressed as sum of consecutive numbers
def isSumOfConsecutive(n):
# 1 cannot be represented
if n == 1:
return False
# Check if n is power of 2
if (n & (n - 1)) == 0:
return False
return True
# Driver code
if __name__ == "__main__":
n = 10
if isSumOfConsecutive(n):
print("true")
else:
print("false")
using System;
// C# program to check whether a number
// can be expressed as sum of consecutive numbers
public class GfG
{
public static bool isSumOfConsecutive(int n)
{
// 1 cannot be represented
if (n == 1)
{
return false;
}
// Check if n is power of 2
if ((n & (n - 1)) == 0)
{
return false;
}
return true;
}
// Driver code
public static void Main()
{
int n = 10;
if (isSumOfConsecutive(n))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
// JavaScript program to check whether a number
// can be expressed as sum of consecutive numbers
function isSumOfConsecutive(n) {
// 1 cannot be represented
if (n === 1) {
return false;
}
// Check if n is power of 2
if ((n & (n - 1)) === 0) {
return false;
}
return true;
}
// Driver code
let n = 10;
if (isSumOfConsecutive(n)) {
console.log("true");
} else {
console.log("false");
}
Output
true