Find the sum of N terms of the series 1, 4, 13, 40, 121, ... - GeeksforGeeks

21 Jan, 2022
Given a positive integer, n. Find the sum of the first n term of the series:
1, 4, 13, 40, 121, .....
Examples:
Input: n = 5Output: 179
Input: n = 3Output: 18
Approach:
The sequence is formed by using the following pattern. For any value N-
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
  -(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
The above equation 3, 9, 27, 81, ... is a G.P. with common ratio 3 and first term 3.
Thus, we have
Since,
Therefore,
Thus, sum of n terms is
Illustration:
Input: n = 5Output: 179Explanation:       =       =       =179
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std; // Function to return sum of// first N term of the seriesint findSum(int N){    return (pow(3, N + 1) -                3 - 2 * N) / 4;} // Driver Codeint main(){    int N = 5;    cout << findSum(N);    return 0;}
// Java program for the above approachimport 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.
# Python code for the above approach # Function to return sum of# first N term of the seriesdef findSum(N):    return (3 ** (N + 1) - 3 - 2 * N) // 4; # Driver CodeN = 5;print(findSum(N)); # This code is contributed by Saurabh Jaiswal
// C# program to implement// the above approachusing System;class GFG{ // Function to return sum of// first N term of the seriesstatic 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.
<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>
  
179
 
Time Complexity: O(1)
 
Auxiliary Space: O(1)
 
lokeshpotta20
samim2000
_saurabh_jaiswal
Algo-Geek 2021
series
Algo Geek
Mathematical
Pattern Searching
Mathematical
series
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count of operation required to water all the plants
Check if the given string is valid English word or not
Lexicographically smallest string formed by concatenating any prefix and its mirrored form
Encode given String by inserting in Matrix column-wise and printing it row-wise
Minimum count of words among all the given sentences
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7