Check that if given number is expressed sum of consecutive or not
The given problem is to check whether a given number can be expressed as the sum of two or more consecutive numbers. For example, the number 9 can be expressed as 4 + 5 or 2 + 3 + 4, but the number 4 cannot be expressed as the sum of any two or more consecutive numbers.
Problem Statement
Given an integer 'num', we need to determine if 'num' can be expressed as the sum of two or more consecutive numbers.
Example
Let's take a few examples to better understand the problem:
-
For 'num' = 9: The consecutive sums of 9 are:
- 2 + 3 + 4 = 9
- 4 + 5 = 9
-
For 'num' = 4: The consecutive sums of 4 are:
- There are no two or more consecutive numbers that add up to 4.
Pseudocode
// Function to check whether given number is sum of consecutive two or more numbers
consecutive_sum(num):
// Check if the number is negative, if yes, return
if num < 0:
return
// Print the given number
print "Number:", num
// Check if the number is not 0 and not a power of 2
if num != 0 AND ((num AND (num - 1)) != 0):
print "Yes"
else:
print "No"
// Main function
main():
// Call the consecutive_sum function with example numbers
consecutive_sum(9)
consecutive_sum(4)
- We start by defining the function 'consecutive_sum(num)' to check if a given number can be expressed as the sum of two or more consecutive numbers.
- Within the function, we check if the 'num' is less than 0. If it is negative, we return from the function since negative numbers cannot be expressed as the sum of consecutive numbers.
- Next, we print the given number to display it in the output.
- We then check if 'num' is not equal to 0 and whether 'num' is not a power of 2 (using the bitwise AND operation and subtraction). If this condition is true, it means 'num' can be expressed as the sum of two or more consecutive numbers, so we print "Yes".
- If the condition in step 4 is false, it means 'num' cannot be expressed as the sum of two or more consecutive numbers, and we print "No".
- The main function 'main()' is called to test the 'consecutive_sum()' function with example numbers, which are 9 and 4 in this case.
Algorithm Explanation
- We begin by defining the 'consecutive_sum(num)' function.
- We handle the case where 'num' is negative, and in such cases, we return without further processing, as negative numbers cannot be expressed as the sum of two or more consecutive numbers.
- Next, we check if 'num' is not equal to 0 and whether (num & (num - 1)) is not equal to 0. This bitwise operation checks if 'num' is not a power of 2.
- If 'num' is not a power of 2, it means 'num' can be expressed as the sum of two or more consecutive numbers, and we print "Yes".
- Otherwise, if 'num' is a power of 2 or 0, it cannot be expressed as the sum of two or more consecutive numbers, and we print "No".
Code Solution
Here given code implementation process.
// C program
// Check that if given number is expressed sum of consecutive or not
#include <stdio.h>
// Check whether given number is sum of consecutive two or more numbers
void consecutive_sum(int num)
{
if (num < 0)
{
return;
}
// Given number
printf("\n Number : %d", num);
if (num != 0 && ((num & (num - 1)) != 0))
{
printf("\n Yes ");
}
else
{
printf("\n No ");
}
}
int main(int argc, char
const *argv[])
{
consecutive_sum(9);
consecutive_sum(4);
return 0;
}
Output
Number : 9
Yes
Number : 4
No
/*
Java program
Check that if given number is expressed sum of consecutive or not
*/
public class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
public void consecutiveSum(int num)
{
if (num < 0)
{
return;
}
// Given number
System.out.print("\n Number : " + num);
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num & (num - 1)) != 0))
{
// When Yes
System.out.print("\n Yes ");
}
else
{
System.out.print("\n No ");
}
}
public static void main(String[] args)
{
Consecutive task = new Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
}
}
Output
Number : 9
Yes
Number : 4
No
// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Check that if given number is expressed sum of consecutive or not
*/
class Consecutive
{
public:
// Check whether given number is sum of consecutive two or more numbers
void consecutiveSum(int num)
{
if (num < 0)
{
return;
}
// Given number
cout << "\n Number : " << num;
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num &(num - 1)) != 0))
{
// When Yes
cout << "\n Yes ";
}
else
{
cout << "\n No ";
}
}
};
int main()
{
Consecutive task = Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
return 0;
}
Output
Number : 9
Yes
Number : 4
No
// Include namespace system
using System;
/*
C# program
Check that if given number is expressed sum of consecutive or not
*/
public class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
public void consecutiveSum(int num)
{
if (num < 0)
{
return;
}
// Given number
Console.Write("\n Number : " + num);
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num & (num - 1)) != 0))
{
// When Yes
Console.Write("\n Yes ");
}
else
{
Console.Write("\n No ");
}
}
public static void Main(String[] args)
{
Consecutive task = new Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
}
}
Output
Number : 9
Yes
Number : 4
No
<?php
/*
Php program
Check that if given number is expressed sum of consecutive or not
*/
class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
public function consecutiveSum($num)
{
if ($num < 0)
{
return;
}
// Given number
echo "\n Number : ". $num;
// Check that number is constructed by two or more consecutive numbers or not
if ($num != 0 && (($num & ($num - 1)) != 0))
{
// When Yes
echo "\n Yes ";
}
else
{
echo "\n No ";
}
}
}
function main()
{
$task = new Consecutive();
// num = 9
// = 4+5 (consecutive number)
$task->consecutiveSum(9);
// num = 4
$task->consecutiveSum(4);
}
main();
Output
Number : 9
Yes
Number : 4
No
/*
Node Js program
Check that if given number is expressed sum of consecutive or not
*/
class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
consecutiveSum(num)
{
if (num < 0)
{
return;
}
// Given number
process.stdout.write("\n Number : " + num);
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num & (num - 1)) != 0))
{
// When Yes
process.stdout.write("\n Yes ");
}
else
{
process.stdout.write("\n No ");
}
}
}
function main()
{
var task = new Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
}
main();
Output
Number : 9
Yes
Number : 4
No
# Python 3 program
# Check that if given number is expressed sum of consecutive or not
class Consecutive :
# Check whether given number is sum of consecutive two or more numbers
def consecutiveSum(self, num) :
if (num < 0) :
return
# Given number
print("\n Number : ", num, end = "")
# Check that number is constructed by two or more consecutive numbers or not
if (num != 0 and((num & (num - 1)) != 0)) :
# When Yes
print("\n Yes ", end = "")
else :
print("\n No ", end = "")
def main() :
task = Consecutive()
# num = 9
# = 4+5 (consecutive number)
task.consecutiveSum(9)
# num = 4
task.consecutiveSum(4)
if __name__ == "__main__": main()
Output
Number : 9
Yes
Number : 4
No
# Ruby program
# Check that if given number is expressed sum of consecutive or not
class Consecutive
# Check whether given number is sum of consecutive two or more numbers
def consecutiveSum(num)
if (num < 0)
return
end
# Given number
print("\n Number : ", num)
# Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num & (num - 1)) != 0))
# When Yes
print("\n Yes ")
else
print("\n No ")
end
end
end
def main()
task = Consecutive.new()
# num = 9
# = 4+5 (consecutive number)
task.consecutiveSum(9)
# num = 4
task.consecutiveSum(4)
end
main()
Output
Number : 9
Yes
Number : 4
No
/*
Scala program
Check that if given number is expressed sum of consecutive or not
*/
class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
def consecutiveSum(num: Int): Unit = {
if (num < 0)
{
return;
}
// Given number
print("\n Number : " + num);
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num & (num - 1)) != 0))
{
// When Yes
print("\n Yes ");
}
else
{
print("\n No ");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Consecutive = new Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
}
}
Output
Number : 9
Yes
Number : 4
No
/*
Swift 4 program
Check that if given number is expressed sum of consecutive or not
*/
class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
func consecutiveSum(_ num: Int)
{
if (num < 0)
{
return;
}
// Given number
print("\n Number : ", num, terminator: "");
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num & (num - 1)) != 0))
{
// When Yes
print("\n Yes ", terminator: "");
}
else
{
print("\n No ", terminator: "");
}
}
}
func main()
{
let task: Consecutive = Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
}
main();
Output
Number : 9
Yes
Number : 4
No
/*
Kotlin program
Check that if given number is expressed sum of consecutive or not
*/
class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
fun consecutiveSum(num: Int): Unit
{
if (num < 0)
{
return;
}
// Given number
print("\n Number : " + num);
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num and(num - 1)) != 0))
{
// When Yes
print("\n Yes ");
}
else
{
print("\n No ");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: Consecutive = Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
}
Output
Number : 9
Yes
Number : 4
No
Explanation of Output
-
For 'num' = 9, the output is "Yes" because 9 can be expressed as the sum of two or more consecutive numbers (2 + 3 + 4 or 4 + 5).
-
For 'num' = 4, the output is "No" because 4 cannot be expressed as the sum of two or more consecutive numbers.
Time Complexity
The time complexity of the given code is O(1). This is because the 'consecutive_sum' function performs some basic operations that do not depend on the input size. The bitwise AND and subtraction operations are constant-time operations, and thus the overall time complexity remains constant.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment