Sum of first n term of Series 3, 5, 9, 17, 33….
Last Updated :
17 Jan, 2024
Given n, we need to find sum of first n terms of the series represented as Sn = 3 + 5 + 9 + 17 + 33 … upto n
Examples:
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++
#include <bits/stdc++.h>
using namespace std;
int calculateSum( int n)
{
return ( pow (2, n + 1) + n - 2);
}
int main()
{
int n = 4;
cout << "Sum = " << calculateSum(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int calculateSum( int n)
{
return (( int )Math.pow( 2 , n + 1 ) +
n - 2 );
}
public static void main(String args[])
{
int n = 4 ;
System.out.println( "Sum = " +
calculateSum(n));
}
}
|
Python
def calculateSum(n):
return ( 2 * * (n + 1 ) + n - 2 )
n = 4
print ( "Sum =" , calculateSum(n))
|
C#
using System;
class GFG
{
static int calculateSum( int n)
{
return (( int )Math.Pow(2, n + 1) +
n - 2);
}
public static void Main()
{
int n = 4;
Console.WriteLine( "Sum = " +
calculateSum(n));
}
}
|
Javascript
<script>
function calculateSum( n)
{
return (Math.pow(2, n + 1) +
n - 2);
}
let n = 4;
document.write( "Sum = " +
calculateSum(n));
</script>
|
PHP
<?php
function calculateSum( $n )
{
return (pow(2, $n + 1) + $n - 2);
}
$n = 4;
echo "Sum = " , calculateSum( $n );
?>
|
Time Complexity: O(log n)
Auxiliary Space: O(log n)
Another Approach: Using recursion
In this approach, we can use a recursive function to generate the series and sum the first n terms of the series.
Steps that were to follow the above approach:
- Initialize the sum variable to 0.
- Define a recursive function that takes the current term, the sum, and the remaining number of terms as arguments.
- If the remaining number of terms is 0, return the sum.
- Add the current term to the sum.
- Multiply the current term by 2 and subtract 1.
- Call the recursive function with the updated current term, sum, and remaining number of terms.
- Return the result of the recursive function.
Below is the code to implement the above steps:
C++
#include <iostream>
using namespace std;
int sum_of_terms( int current_term, int sum, int n) {
if (n == 0) {
return sum;
}
sum += current_term;
current_term = current_term * 2 - 1;
return sum_of_terms(current_term, sum, n - 1);
}
int main() {
int n = 4;
int sum = sum_of_terms(3, 0, n);
cout << "Sum =" << sum << endl;
return 0;
}
|
Java
import java.io.*;
public class Main {
public static int sum_of_terms( int current_term, int sum, int n) {
if (n == 0 ) {
return sum;
}
sum += current_term;
current_term = current_term * 2 - 1 ;
return sum_of_terms(current_term, sum, n - 1 );
}
public static void main(String[] args) {
int n = 4 ;
int sum = sum_of_terms( 3 , 0 , n);
System.out.println( "Sum =" + sum);
}
}
|
Python3
def sum_of_terms(current_term, _sum, n):
if n = = 0 :
return _sum
_sum + = current_term
current_term = current_term * 2 - 1
return sum_of_terms(current_term, _sum, n - 1 )
def main():
n = 4
_sum = sum_of_terms( 3 , 0 , n)
print ( "Sum =" , _sum)
if __name__ = = "__main__" :
main()
|
C#
using System;
class Program
{
static int SumOfTerms( int currentTerm, int sum, int n)
{
if (n == 0)
{
return sum;
}
sum += currentTerm;
currentTerm = currentTerm * 2 - 1;
return SumOfTerms(currentTerm, sum, n - 1);
}
static void Main()
{
int n = 4;
int initialTerm = 3;
int sum = SumOfTerms(initialTerm, 0, n);
Console.WriteLine( "Sum = " + sum);
}
}
|
Javascript
function sumOfTerms(currentTerm, sum, n) {
if (n === 0) {
return sum;
}
sum += currentTerm;
currentTerm = currentTerm * 2 - 1;
return sumOfTerms(currentTerm, sum, n - 1);
}
function main() {
let n = 4;
let sum = sumOfTerms(3, 0, n);
console.log( "Sum =" , sum);
}
main();
|
Time complexity: O(n)
Space complexity: O(n) (due to the recursion depth)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...