Tuesday, November 19, 2024
Google search engine
HomeData Modelling & AIFind sum of N terms of series 1, (1+4) , (1+4+4^2), (1+4+4^2+4^3),...

Find sum of N terms of series 1, (1+4) , (1+4+4^2), (1+4+4^2+4^3), …..

Given a positive integer, N. Find the sum of the first N term of the series-

1, (1+4), (1+4+42), (1+4+42+43), …., till N terms

Examples:

Input: N = 3
Output: 27

Input: N = 5
Output: 453

 

Approach:

1st term = 1

2nd term = (1 + 4)

3rd term = (1 + 4 + 4 ^ 2)

4th term = (1 + 4 + 4 ^ 2 + 4 ^ 3)

.

.

Nth term = (1 + 4 + 4 ^ 2+….+ 4 ^ (N – 2) + 4 ^(N – 1))

The sequence is formed by using the following pattern. For any value N-

S_{N}=\frac{4}{9}(4^{N}-1)-\frac{N}{3}

Derivation:

The following series of steps can be used to derive the formula to find the sum of N terms-

The series 

1, (1+4), (1+4+4^{2}), (1+4+4^{2}+4^{3})+....+N terms

can be decomposed as-

a_{1}=1

a_{2}=1+4

a_{3}=1+4+4^{2}

a_{4}=1+4+4^{2}+4^{3}

a_{N}=1+4+4^{2}+4^{3}+....+4^{N}                                           -(1)

The equation (1) is in G.P. with

First term a = 1

Common ration r = 4

The sum of N terms in G.P. for r>1 is

S_{N}=\frac{a(r^{N}-1)}{r-1}

Substituting the values of a and r in the above equation, we get-

S_{N}=\frac{1(4^{N}-1)}{4-1}

Thus, the term

a_{N}=\frac{(4^{N}-1)}{3}

The sum of the series 1, (1+4), (1+4+4^{2}), (1+4+4^{2}+4^{3})+….+N terms can be represented as-

S_{N}=\sum a^{N}

S_{N}=\sum \frac{4^{N}-1}{3}

S_{N}=\frac{1}{3}\sum 4^{N}-\frac{1}{3}\sum 1

S_{N}=\frac{1}{3}(4+4^{2}+4^{3}+....+4^{N})-\frac{N}{3}                  -(2)

The equation-

4+4^{2}+4^{3}+....+4^{N}

is in G.P. with 

First term a = 4

Common ratio r = 4

Applying the formula of sum of G.P.-

S_{N}=\frac{4(4^{N}-1)}{4-1}                                                           -(3)

Substituting equation (3) in equation (2), we get-

S_{N}=\frac{1}{3}(\frac{4(4^{N}-1)}{4-1})-\frac{N}{3}

S_{N}=\frac{4}{3}(\frac{4^{N}-1}{4-1})-\frac{N}{3}

S_{N}=\frac{4}{9}(4^{N}-1)-\frac{N}{3}

Illustration:

Input: N = 3
Output: 11
Explanation:
S_{N}=\frac{4}{9}(4^{N}-1)-\frac{N}{3}
S_{N}=\frac{4}{9}(4^{3}-1)-\frac{3}{3}
S_{N}=\frac{4}{9}(63)-1
S_{N}=27

Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the sum
// of first N term
int calcSum(int n)
{
    int a = pow(4, n);
    return (4 * (a - 1) - 3 * n) / 9;
}
 
// Driver Code
int main()
{
    // Value of N
    int N = 3;
 
    // Function call to calculate
    // sum of the series
    cout << calcSum(N);
    return 0;
}


Java




// Java code for the above approach
import java.util.*;
 
class GFG{
 
  // Function to calculate the sum
  // of first N term
  static int calcSum(int n)
  {
    int a = (int)Math.pow(4, n);
    return (4 * (a - 1) - 3 * n) / 9;
  }
 
 
  // Driver Code
  public static void main(String[] args)
  {
    // Value of N
    int N = 3;
 
    // Function call to calculate
    // sum of the series
    System.out.print(calcSum(N));
  }
}
 
// This code is contributed by code_hunt.


Python3




# Python 3 program for the above approach
 
# Function to calculate the sum
# of first N term
def calcSum(n):
    a = pow(4, n)
    return (4 * (a - 1) - 3 * n) / 9
 
 
# Driver Code
if __name__ == "__main__":
 
    # Value of N
    N = 3
     
    # Function call to calculate
    # sum of the series
    print(calcSum(N))
 
# This code is contributed by Abhishek Thakur.


C#




// C# code for the above approach
using System;
 
class GFG{
 
  // Function to calculate the sum
  // of first N term
  static int calcSum(int n)
  {
    int a = (int)Math.Pow(4, n);
    return (4 * (a - 1) - 3 * n) / 9;
  }
 
 
  // Driver Code
  public static void Main()
  {
    // Value of N
    int N = 3;
 
    // Function call to calculate
    // sum of the series
    Console.Write(calcSum(N));
  }
}
 
// This code is contributed by gfgking


Javascript




<script>
// Javascript program to implement
// the above approach
 
// Function to calculate the sum
// of first N term
function calcSum(n)
{
    let a = Math.pow(4, n)
    return (4 * (a - 1) - 3 * n) / 9
}
 
// Driver Code
 
// Value of N
let N = 3
 
// Function call to calculate
// sum of the series
document.write(calcSum(N))
 
// This code is contributed by samim2000.
</script>


Output

27

Time Complexity: O(log4n) because using inbuilt pow function
Auxiliary Space: O(1), since no extra space has been taken.

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
20 Aug, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments