Sum of the series 2^0 + 2^1 + 2^2 +.....+ 2^n

03 Sep, 2021
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 = 31Input: 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 form 0 to n.Below is the implementation of above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find sum#include <bits/stdc++.h>using namespace std; // function to calculate sum of seriesint calculateSum(int n){    // initialize sum as 0    int sum = 0;     // loop to calculate sum of series    for (int i = 0; i < n; i++) {         // calculate 2^i        // and add it to sum         sum = sum + (1 << i);    }    return sum;} // Driver codeint main(){    int n = 10;    cout << "Sum of series of power of 2 is : "         << calculateSum(n);}
// Java program to find sumclass GFG {    // function to calculate sum of series    static int calculate sum(int n)    {        // initialize sum as 0        int sum = 0;         // loop to calculate sum of series        for (int i = 0; i < n; i++) {             // calculate 2^i            // and add it to sum             sum = sum + (1 << i);        }        return sum;    }    // Main function    public static void main(String[] args)    {         int n = 10;        System.out.println("Sum of the series : " + calculateSum(n));    }};
# Python3 program to calculate# sum of series of power of 2 # function to calculate sum of seriesdef calculateSum(n):     sum = 0      # loop to calculate sum of series     for i in range (0, n):          # calculate 2 ^ i         sum = sum+ (1 << i)         return sum # Driver coden = 10print("Sum of series ", calculateSum(n))
// C# program to find sumusing System; class GFG{    // function to calculate    // sum of series    static int calculateSum(int n)    {        // initialize sum as 0        int sum = 0;         // loop to calculate        // sum of series        for (int i = 0; i < n; i++)        {             // calculate 2^i            // and add it to sum             sum = sum + (1 << i);        }        return sum;    }         // Driver code    public static void Main()    {        int n = 10;        Console.WriteLine("Sum of the series : " +                                 calculateSum(n));    }} // This code is contributed// by Akanksha Rai(Abby_akku)
<?php// PHP program to find sum of the// series 2^0 + 2^1 + 2^2 +.....+ 2^n // function to calculate// sum of seriesfunction calculateSum($n){    // initialize sum as 0    $sum = 0;     // loop to calculate    // sum of series    for ($i = 0; $i < $n; $i++)    {         // calculate 2^i        // and add it to sum         $sum = $sum + (1 << $i);    }    return $sum;} // Driver code$n = 10;echo "Sum of the series of " .              "power 2 is : ",             calculateSum($n); // This code is contributed// by Smitha?>
<script> // Javascript  program to find sum of the// series 2^0 + 2^1 + 2^2 +.....+ 2^n // function to calculate// sum of seriesfunction calculateSum(n){    // initialize sum as 0    let sum = 0;     // loop to calculate    // sum of series    for (let i = 0; i < n; i++)    {         // calculate 2^i        // and add it to sum         sum = sum + (1 << i);    }    return sum;} // Driver codelet n = 10;document.write("Sum of the series of power 2 is : "                           + calculateSum(n)) //This code is contributed by sravan kumar</script>
Sum of series of power of 2 is : 1023
 
Time Complexity: O(n) 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++
Java
Python3
C#
PHP
Javascript
// C++ program to find sum#include <bits/stdc++.h>using namespace std; int calculateSum(int n){    // calculate  and return 2^(n+1) -1    return (1 << (n + 1)) - 1;} int main(){    int n = 10;    cout << "Sum of series of power of 2 is :"         << calculateSum(n);}
// Java program to calculate// sum of series of power of 2 class GFG {     // function to calculate sum of series    static int calculate sum(int n)    {         // calculate 2^(n+1)        int sum = (1 << (n + 1));        return sum - 1;    }     // Driver code    public static void main(String[] args)    {         int n = 10;        System.out.println("Sum of the series of power 2 is : "                           + calculateSum(n));    }};
# Python3 program to calculate# sum of series of 2's power # function to calculate sum of seriesdef calculateSum(n):       # calculate 2^(n + 1)      sum = (1 << (n + 1))      return sum-1 # Driver coden = 10print("Sum of series ", calculateSum(n))
// C# program to calculate// sum of series of power of 2using System;class GFG{     // function to calculate    // sum of series    static int calculateSum(int n)    {         // calculate 2^(n+1)        int sum = (1 << (n + 1));        return sum - 1;    }     // Driver code    public static void Main()    {        int n = 10;        Console.Write("Sum of the series " +                        "of power 2 is : " +                           calculateSum(n));    }     // This code is contributed// by Smitha}
<?php// PHP program to calculate// sum of series of power of 2 // function to calculate// sum of seriesfunction calculateSum($n){     // calculate 2^(n+1)    $sum = (1 << ($n + 1));    return $sum - 1;} // Driver code$n = 10;echo "Sum of the series of " .              "power 2 is : ",             calculateSum($n); // This code is contributed// by Smitha
<script> // Javascript program to calculate// sum of series of power of 2 // function to calculate// sum of seriesfunction calculateSum(n){     // calculate 2^(n+1)    let sum = (1 << (n + 1));    return sum - 1;} // Driver codelet n = 10;document.write("Sum of the series of power 2 is : "                           + calculateSum(n)); // This code is contributed by sravan kumar </script>
Sum of series of power of 2 is :2047
 
Time Complexity: O(1) 
Smitha Dinesh Semwal
Akanksha_Rai
litedeveloper24
florida_man
sravankumar8128
akshaysingh98088
surindertarika1234
school-programming
series-sum
Bit Magic
School Programming
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bits manipulation (Important tactics)
Josephus problem | Set 1 (A O(n) Solution)
Divide two integers without using multiplication, division and mod operator
Bit Fields in C
Find the element that appears once
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java