Find Nth term of the series 0, 6, 24, 60, 120…
Last Updated :
19 Apr, 2023
Given a positive integer N, the task is to find the Nth term of the series
0, 6, 24, 60, 120…till N terms
Examples:
Input: N = 5
Output: 120
Input: N = 10
Output: 990
Approach:
From the given series, find the formula for Nth term-
1st term = 1 ^ 3 – 1 = 0
2nd term = 2 ^ 3 – 2 = 6
3rd term = 3 ^ 3 – 3 = 24
4th term = 4 ^ 3 – 4 = 60
.
.
Nth term = N ^ 3 – N
The Nth term of the given series can be generalized as-
TN = N ^ 3 – N
Illustration:
Input: N = 10
Output: 990
Explanation:
TN = N ^ 3 – N
= 10 ^ 3 – 10
= 1000 – 10
= 990
Below is the implementation of the above approach-
C++
#include <iostream>
using namespace std;
int nth( int n)
{
return n * n * n - n;
}
int main()
{
int N = 5;
cout << nth(N) << endl;
return 0;
}
|
C
#include <stdio.h>
int nth( int n)
{
return n * n * n - n;
}
int main()
{
int N = 5;
printf ( "%d" , nth(N));
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int N = 5 ;
System.out.println(nth(N));
}
public static int nth( int n)
{
return n * n * n - n;
}
}
|
Python
def nth(n):
return n * n * n - n
N = 5
print (nth(N))
|
C#
using System;
public class GFG
{
public static int nth( int n) { return n * n * n - n; }
static public void Main()
{
int N = 5;
Console.Write(nth(N));
}
}
|
Javascript
<script>
function nth(n)
{
return n * n * n - n;
}
let N = 5;
document.write(nth(N) + '<br>' );
</script>
|
Time Complexity: O(1) // since no loop is used the algorithm takes up constant time to perform the operations
Auxiliary Space: O(1) // since no extra array is used so the space taken by the algorithm is constant
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...