Find the sum of N terms of the series 12, 14, 24, 58, 164, …
Last Updated :
16 Aug, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int findSum( int N)
{
return ( pow (3, N) -
pow (N, 2) +
11 * N - 1);
}
int main()
{
int N = 5;
cout << findSum(N);
return 0;
}
|
Java
class GFG {
static int findSum( int N) {
return ( int ) (Math.pow( 3 , N) - Math.pow(N, 2 ) + 11 * N - 1 );
}
public static void main(String args[]) {
int N = 5 ;
System.out.print(findSum(N));
}
}
|
Python3
def findSum(N):
return (( 3 * * N) -
(N * * 2 ) +
11 * N - 1 );
N = 5 ;
print (findSum(N));
|
C#
using System;
class GFG
{
static int findSum( int N) {
return ( int ) (Math.Pow(3, N) - Math.Pow(N, 2) + 11 * N - 1);
}
public static void Main()
{
int N = 5;
Console.Write(findSum(N));
}
}
|
Javascript
<script>
function findSum(N) {
return (Math.pow(3, N) -
Math.pow(N, 2) +
11 * N - 1);
}
let N = 5;
document.write(findSum(N));
</script>
|
Time Complexity: O(logN) since it is using pow function
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...