Skip to content
Related Articles
Open in App
Not now

Related Articles

Find the sum of N terms of the series 12, 14, 24, 58, 164, …

Improve Article
Save Article
  • Last Updated : 16 Aug, 2022
Improve Article
Save Article

Given a positive integer, N. Find the sum of the first N term of the series 12, 14, 24, 58, 164, …..

Examples:

Input: N = 5
Output: 272

Input: N = 3
Output: 50

Approach: 

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

SN = 3N – N2 + 11 * N – 1

Illustration:

Input: N = 5
Output: 272
Explanation:
SN = 3N – N2 + 11 * N – 1
     = 35 – 52 + 11 * 5 – 1
     = 243 – 25 + 54
     = 272

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


Java




// Java program to implement
// the above approach
class GFG {
 
    // Function to return sum of
    // N term of the series
    static int findSum(int N) {
        return (int) (Math.pow(3, N) - Math.pow(N, 2) + 11 * N - 1);
    }
 
    // Driver Code
    public static void main(String args[]) {
        int N = 5;
        System.out.print(findSum(N));
    }
}
 
// This code is contributed by saurabh_jaiswal.


Python3




# Python code for the above approach
 
# Function to return sum of
# N term of the series
def findSum(N):
    return ((3 ** N) -
        (N ** 2) +
        11 * N - 1);
 
# Driver Code
 
# Get the value of N
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
    // N term of the series
    static int findSum(int N) {
        return (int) (Math.Pow(3, N) - Math.Pow(N, 2) + 11 * N - 1);
    }
 
  // 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
        // N term of the series
        function findSum(N) {
            return (Math.pow(3, N) -
                Math.pow(N, 2) +
                11 * N - 1);
        }
        // Driver Code
 
        // Get the value of N
        let N = 5;
        document.write(findSum(N));
 
  // This code is contributed by Potta Lokesh
    </script>


 
 

Output

272

Time Complexity: O(logN) since it is using pow function

Auxiliary Space: O(1), since no extra space has been taken.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!