Find sum of N terms of the series 3^3 – 2^3, 5^3 – 4^3, 7^3 – 6^3, …
Last Updated :
16 Aug, 2022
Given a positive integer N, the task is to find the sum upto Nth term of the series:
33 – 23, 53 – 43, 73 – 63, …., till N terms
Examples:
Input: N = 10
Output: 4960
Input: N = 1
Output: 19
Naive Approach:
- Initialize two int variables odd and even. Odd with value 3 and even with value 2.
- Now Iterate the for loop n times each time will calculate the current term and add it to the sum.
- In each iteration increase odd and even value with 2.
- Return the resultant sum
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int N)
{
int Odd = 3;
int Even = 2;
int Sum = 0;
for ( int i = 0; i < N; i++) {
Sum += ( pow (Odd, 3)
- pow (Even, 3));
Odd += 2;
Even += 2;
}
return Sum;
}
int main()
{
int N = 10;
cout << findSum(N);
}
|
Java
import java.util.*;
class GFG
{
public static int findSum( int N)
{
int Odd = 3 ;
int Even = 2 ;
int Sum = 0 ;
for ( int i = 0 ; i < N; i++) {
Sum += (Math.pow(Odd, 3 ) - Math.pow(Even, 3 ));
Odd += 2 ;
Even += 2 ;
}
return Sum;
}
public static void main(String[] args)
{
int N = 10 ;
System.out.print(findSum(N));
}
}
|
Python3
def findSum(N):
Odd = 3
Even = 2
Sum = 0
for i in range (N):
Sum + = ( pow (Odd, 3 ) - pow (Even, 3 ))
Odd + = 2
Even + = 2
return Sum
if __name__ = = "__main__" :
N = 10
print (findSum(N))
|
C#
using System;
class GFG
{
public static int findSum( int N)
{
int Odd = 3;
int Even = 2;
int Sum = 0;
for ( int i = 0; i < N; i++) {
Sum += ( int )(Math. Pow(Odd, 3) - Math.Pow(Even, 3));
Odd += 2;
Even += 2;
}
return Sum;
}
public static void Main()
{
int N = 10;
Console.Write(findSum(N));
}
}
|
Javascript
<script>
function findSum(N)
{
let Odd = 3;
let Even = 2;
let Sum = 0;
for (let i = 0; i < N; i++) {
Sum += (Math.pow(Odd, 3)
- Math.pow(Even, 3));
Odd += 2;
Even += 2;
}
return Sum;
}
let N = 10;
document.write(findSum(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), since no extra space has been taken.
Efficient Approach:
The sequence is formed by using the following pattern.
For any value N the generalise form of the given sequence is-
SN = 4*N3 + 9*N2 + 6*N
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int N)
{
return 4 * pow (N, 3) + 9 * pow (N, 2) + 6 * N;
}
int main()
{
int N = 10;
cout << findSum(N);
}
|
Java
import java.util.*;
class GFG
{
static int findSum( int N)
{
return ( int ) ( 4 * Math.pow(N, 3 ) + 9 * Math.pow(N, 2 ) + 6 * N);
}
public static void main(String[] args)
{
int N = 10 ;
System.out.print(findSum(N));
}
}
|
Python3
def findSum(N):
return 4 * pow (N, 3 ) + 9 * pow (N, 2 ) + 6 * N
if __name__ = = "__main__" :
N = 10
print (findSum(N))
|
C#
using System;
class GFG
{
static int findSum( int N)
{
return 4 * ( int )Math.Pow(N, 3)
+ 9 * ( int )Math.Pow(N, 2) + 6 * N;
}
public static void Main()
{
int N = 10;
Console.Write(findSum(N));
}
}
|
Javascript
<script>
function findSum(N) {
return 4 * Math.pow(N, 3) + 9 * Math.pow(N, 2) + 6 * N;
}
let N = 10;
document.write(findSum(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...