Given a number N, the task is to find the sum of the below series till 3N terms.
1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+… till 3N terms
Examples:
Input: N = 2
Output: 17
Input: N = 3
Output: 56
Naive Approach:
If we observe clearly then we can divide it into a grouping of 3 terms having N no. of groups.
1 to 3 term = 1^3 +1^2 +1 = 3
4 to 6 term = 2^3+2^2+2 = 14
7 to 9 term = 3^3+3^2+ 3 = 39
.
.
(3N-2) to 3N term = N^3+N^2+ N
Below steps can be used to solve the problem-
- For each iterative i, calculate (i^3+i^2+i).
- And add the calculated value to sum (Initially the sum will be 0).
- Return the final sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int seriesSum( int N)
{
int sum = 0;
for ( int i = 1; i <= N; i++)
{
sum += ( pow (i, 3) + pow (i, 2) + i);
}
return sum;
}
int main()
{
int N = 5;
cout << seriesSum(N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static int seriesSum( int N)
{
int sum = 0 ;
for ( int i = 1 ; i <= N; i++)
{
sum += (Math.pow(i, 3 ) + Math.pow(i, 2 ) + i);
}
return sum;
}
public static void main (String[] args)
{
int N = 5 ;
System.out.print(seriesSum(N));
}
}
|
Python3
def seriesSum(N):
sum = 0 ;
for i in range ( 1 , N + 1 ):
sum + = (i * * 3 ) + (i * * 2 ) + i;
return sum ;
N = 5 ;
print (seriesSum(N));
|
C#
using System;
class GFG {
static int seriesSum( int N)
{
int sum = 0;
for ( int i = 1; i <= N; i++)
{
sum += (( int )Math.Pow(i, 3) + ( int )Math.Pow(i, 2) + i);
}
return sum;
}
public static void Main ()
{
int N = 5;
Console.Write(seriesSum(N));
}
}
|
Javascript
<script>
function seriesSum(N) {
let sum = 0;
for (let i = 1; i <= N; i++) {
sum += (Math.pow(i, 3) + Math.pow(i, 2) + i);
}
return sum;
}
let N = 5;
document.write(seriesSum(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), since no extra space has been taken.
Efficient Approach:
From the given series, find the formula for the 3Nth term:
The given Series

This can be written as-
-(1)
The above three equations are in A.P., hence can be written as-



= N*(N+1)*(3*N^2+7*N+8)/12
So, the sum of the series till 3Nth term can be generalized as:

C++
#include <bits/stdc++.h>
using namespace std;
int seriesSum( int N)
{
return N * (N + 1) * (3 * pow (N, 2) + 7 * N + 8) / 12;
}
int main()
{
int N = 5;
cout << seriesSum(N);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int seriesSum( int N)
{
return N * (N + 1 ) * ( 3 * ( int )Math.pow(N, 2 ) + 7 * N + 8 ) / 12 ;
}
public static void main(String args[])
{
int N = 5 ;
System.out.print(seriesSum(N));
}
}
|
Python
import math
def seriesSum(N):
return math.floor(N * (N + 1 ) * ( 3 * pow (N, 2 ) + 7 * N + 8 ) / 12 )
N = 5
print (seriesSum(N))
|
C#
using System;
class GFG
{
static int seriesSum( int N)
{
return N * (N + 1) * (3 * ( int )Math.Pow(N, 2) + 7 * N + 8) / 12;
}
public static void Main()
{
int N = 5;
Console.Write(seriesSum(N));
}
}
|
Javascript
<script>
const seriesSum = (N) => parseInt(N * (N + 1) * (3 * Math.pow(N, 2) + 7 * N + 8) / 12)
let N = 5;
document.write(seriesSum(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)