Open In App

Find the sum of N terms of the series 1, 4, 13, 40, 121, …

Improve
Improve
Like Article
Like
Save
Share
Report

Given a positive integer, n. Find the sum of the first n term of the series:

1, 4, 13, 40, 121, …..

Examples:

Input: n = 5
Output: 179

Input: n = 3
Output: 18

Approach:

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

S_{n}=(\frac{3^{n+1}-3-2*n}{4})

The above solution can be derived following the series of steps-

Let Tn be the nth term and Sn be the sum to n terms of the given series.

Thus, we have

S_{n}=1+4+13+40+121+...+T_{n-1}+T_{n}              -(1)

Equation (1) can be written as-

S_{n}=1+4+13+40+121+…+T_{n-1}+T_{n}  -(2)

Subtracting equation (2) from equation (1), we get

0=1+[3+9+27+81+...+(T_{n}-T_{n-1})-T_{n}]

The above equation 3, 9, 27, 81, … is a G.P. with common ratio 3 and first term 3.

Thus, we have

1+[\frac{3*(3^{n-1}-1)}{3-1}]-T_{n}=0

1+[\frac{3*(3^{n-1}-1)}{2}]-T_{n}=0

(\frac{3^{n}}{2}-\frac{1}{2})-T_{n}=0

(\frac{3^{n}}{2}-\frac{1}{2})=T_{n}

Since,

S_{n}=\sum_{k=1}^{n}T_{k}

Therefore,

S_{n}=\sum_{k=1}^{n}(\frac{3^{k}}{2}-\frac{1}{2})

S_{n}=\frac{1}{2}\sum_{k=1}^{n}3^{k}-\frac{1}{2}\sum_{k=1}^{n}1

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

S_{n}=\frac{1}{2}[\frac{3*(3^{n}-1)}{2}]-\frac{n}{2}

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

Thus, sum of n terms is

S_{n}=(\frac{3^{n+1}-3-2*n}{4})

Illustration:

Input: n = 5
Output: 179
Explanation:
S_{n}=(\frac{3^{n+1}-3-2*n}{4})
       =(\frac{3^{5+1}-3-2*5}{4})
       =(\frac{716}{4})
       =179

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 return sum of
// first N term of the series
int findSum(int N)
{
    return (pow(3, N + 1) -
                3 - 2 * N) / 4;
}
 
// Driver Code
int main()
{
    int N = 5;
    cout << findSum(N);
    return 0;
}

                    

Java

// Java program for the above approach
import java.util.*;
public class GFG
{
 
  // Function to return sum of
  // first N term of the series
  static int findSum(int N)
  {
    return (int)(Math.pow(3, N + 1) -
                 3 - 2 * N) / 4;
  }
 
  public static void main(String args[])
  {
    int N = 5;
    System.out.print(findSum(N));
  }
}
// This code is contributed by Samim Hossain Mondal.

                    

Python3

# Python code for the above approach
 
# Function to return sum of
# first N term of the series
def findSum(N):
    return (3 ** (N + 1) - 3 - 2 * N) // 4;
 
# Driver Code
N = 5;
print(findSum(N));
 
# This code is contributed by Saurabh Jaiswal

                    

C#

// C# program to implement
// the above approach
using System;
class GFG
{
 
// Function to return sum of
// first N term of the series
static int findSum(int N)
{
    return (int)(Math.Pow(3, N + 1) -
                3 - 2 * N) / 4;
}
 
  // Driver Code
  public static void Main()
  {
    int N = 5;
    Console.Write(findSum(N));
 
  }
}
 
// This code is contributed by Samim Hossain Mondal.

                    

Javascript

<script>
      // JavaScript code for the above approach
 
      // Function to return sum of
      // first N term of the series
      function findSum(N) {
          return Math.floor((Math.pow(3, N + 1) -
              3 - 2 * N) / 4);
      }
 
      // Driver Code
      let N = 5;
      document.write(findSum(N));
 
// This code is contributed by Potta Lokesh
  </script>

                    

 
 


Output
179


Time Complexity: O(log3N) because using inbuilt pow function

Auxiliary Space: O(1)


 



Last Updated : 16 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads