Sum of first n term of Series 3, 5, 9, 17, 33....

27 Apr, 2021
Given n, we need to find sum of first n terms of the series represented as Sn = 3 + 5 + 9 + 17 + 33 ... upto nExamples:  
Input : 2
Output : 8
3 + 5 = 8

Input : 5
Output : 67
3 + 5 + 9 + 17 + 33 = 67
 
Let, the nth term be denoted by tn. This problem can easily be solved by splitting each term as follows : 
Sn = 3 + 5 + 9 + 17 + 33...... Sn = (2+1) + (4+1) + (8+1) + (16+1) +...... Sn = (2+1) + (2*2+1) + (2*2*2+1) + (2*2*2*2+1) +......+ ((2*2*2..unto n times) + 1)
We observed that the nth term can be written in terms of powers of 2 and 1. Hence, the sum of first n terms is given as follows: 
Sn = (2+1) + (4+1) + (8+1) + (16+1) +......+ upto n terms Sn = (1 + 1 + 1 + 1 + ...unto n terms) + (2 + 4 + 8 + 16 + ...upto nth power of 2) In above formula, 2 + 4 + 8 + 16.... is a G.P. It’s sum of first n terms is given by 2*(2^n-1)/(2-1) = 2^(n+1) – 2 (using G.P. formula) Sn = n + 2*(2^n – 1) Sn = 2^(n+1) + n -2
 
C++
Java
Python
C#
PHP
Javascript
// C++ program to find sum of first n terms#include <bits/stdc++.h>using namespace std; int calculateSum(int n){    // Sn = n*(4*n*n + 6*n - 1)/3    return (pow(2, n + 1) + n - 2);} // Driver codeint main(){    // number of terms to be included in sum    int n = 4;     // find the Sn    cout << "Sum = " << calculateSum(n);     return 0;}
// Java program to find// sum of first n termsimport java.util.*; class GFG{static int calculateSum(int n){    // Sn = n*(4*n*n + 6*n - 1)/3    return ((int)Math.pow(2, n + 1) +                             n - 2);} // Driver Codepublic static void main(String args[]){    // number of terms to    // be included in sum    int n = 4;     // find the Sn    System.out.println("Sum = " +                calculateSum(n));}} // This code is contributed// by Kirti_Mangal
# Python program to find sum# of n terms of the seriesdef calculateSum(n):     return (2**(n + 1) + n - 2) # Driver Code # number of terms for the sumn = 4 # find the Snprint("Sum =", calculateSum(n)) # This code is contributed# by Surendra_Gangwar
//C# program to find// sum of first n termsusing System; class GFG{static int calculateSum(int n){    // Sn = n*(4*n*n + 6*n - 1)/3    return ((int)Math.Pow(2, n + 1) +                            n - 2);} // Driver Codepublic static void Main(){    // number of terms to    // be included in sum    int n = 4;     // find the Sn    Console.WriteLine("Sum = " +                calculateSum(n));}} // This code is contributed// by inder_verma..
<?php// PHP program to find sum// of first n termsfunction calculateSum( $n){    // Sn = n*(4*n*n + 6*n - 1)/3    return (pow(2, $n + 1) + $n - 2);} // Driver code // number of terms to be// included in sum$n = 4; // find the Snecho "Sum = " , calculateSum($n); // This code is contributed// by inder_verma..?>
<script>// Java script program to find// sum of first n terms function calculateSum( n){    // Sn = n*(4*n*n + 6*n - 1)/3    return (Math.pow(2, n + 1) +                             n - 2);} // Driver Code     // number of terms to    // be included in sum    let n = 4;     // find the Sn    document.write("Sum = " +                calculateSum(n)); // This code is contributed by mohan pavan </script>
Sum = 34
 
Kirti_Mangal
SURENDRA_GANGWAR
inderDuMCA
shubham_singh
pulamolusaimohan
series
series-sum
C++ Programs
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.