Sum of the series 2^0 + 2^1 + 2^2 +…..+ 2^n
Given an integer N, the task is to find the sum of series 20 + 21 + 22 + 23 + …. + 2n.
Examples:
Input: 5
Output: 31
20 + 21 + 22 + 23 + 24
= 1 + 2+ 4 + 8 + 16
= 31
Input: 10
Output: 1023
20 + 21 + 2 2 + 23 + 2 4 + 25 + 26 + 27 + 2 8 + 29
= 1 + 2+ 4 + 8 + 16 + 32 +64 + 128 + 256 + 512
= 1023
A naive approach is to calculate the sum is to add every power of 2 from 0 to n.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int calculateSum( int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum = sum + (1 << i);
}
return sum;
}
int main()
{
int n = 10;
cout << "Sum of series of power of 2 is : "
<< calculateSum(n);
}
|
Java
class GFG {
static int calculate sum( int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
sum = sum + ( 1 << i);
}
return sum;
}
public static void main(String[] args)
{
int n = 10 ;
System.out.println( "Sum of the series : " + calculateSum(n));
}
};
|
Python3
def calculateSum(n):
sum = 0
for i in range ( 0 , n):
sum = sum + ( 1 << i)
return sum
n = 10
print ( "Sum of series " , calculateSum(n))
|
C#
using System;
class GFG
{
static int calculateSum( int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
{
sum = sum + (1 << i);
}
return sum;
}
public static void Main()
{
int n = 10;
Console.WriteLine( "Sum of the series : " +
calculateSum(n));
}
}
|
PHP
<?php
function calculateSum( $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$sum = $sum + (1 << $i );
}
return $sum ;
}
$n = 10;
echo "Sum of the series of " .
"power 2 is : " ,
calculateSum( $n );
?>
|
Javascript
<script>
function calculateSum(n)
{
let sum = 0;
for (let i = 0; i < n; i++)
{
sum = sum + (1 << i);
}
return sum;
}
let n = 10;
document.write( "Sum of the series of power 2 is : "
+ calculateSum(n))
</script>
|
Output:
Sum of series of power of 2 is : 1023
Time Complexity: O(n)
Auxiliary Space: O(1)
An efficient approach is to find the 2^(n+1) and subtract 1 from it since we know that 2^n can be written as:
2n = ( 20+21+22+23+24 +...... 2n-1) +1
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int calculateSum( int n)
{
return (1 << (n + 1)) - 1;
}
int main()
{
int n = 10;
cout << "Sum of series of power of 2 is :"
<< calculateSum(n);
}
|
Java
class GFG {
static int calculate sum( int n)
{
int sum = ( 1 << (n + 1 ));
return sum - 1 ;
}
public static void main(String[] args)
{
int n = 10 ;
System.out.println( "Sum of the series of power 2 is : "
+ calculateSum(n));
}
};
|
Python3
def calculateSum(n):
sum = ( 1 << (n + 1 ))
return sum - 1
n = 10
print ( "Sum of series " , calculateSum(n))
|
C#
using System;
class GFG
{
static int calculateSum( int n)
{
int sum = (1 << (n + 1));
return sum - 1;
}
public static void Main()
{
int n = 10;
Console.Write( "Sum of the series " +
"of power 2 is : " +
calculateSum(n));
}
}
|
PHP
<?php
function calculateSum( $n )
{
$sum = (1 << ( $n + 1));
return $sum - 1;
}
$n = 10;
echo "Sum of the series of " .
"power 2 is : " ,
calculateSum( $n );
|
Javascript
<script>
function calculateSum(n)
{
let sum = (1 << (n + 1));
return sum - 1;
}
let n = 10;
document.write( "Sum of the series of power 2 is : "
+ calculateSum(n));
</script>
|
Output:
Sum of series of power of 2 is :2047
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
08 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...