Find N-th term in the series 0, 4, 18, 48, 100 …
Given a series 0, 4, 18, 48, 100 . . . and an integer N, the task is to find the N-th term of the series.
Examples:
Input: N = 4
Output: 48
Explanation: As given in the sequence we can see that 4th term is 48
Input: N = 6
Output: 180
Approach: Consider the below observation:
For N = 2, the 2nd term is 4, which can be represented as 8 – 4, i.e. 23 – 22
For N = 3, the 3rd term is 18, which can be represented as 27 – 9, i.e. 33 – 32
For N = 4, the 4th term is 18, which can be represented as 27 – 9, i.e. 43 – 42
.
.
.
Similarly, The N-th term of this series can be represented as N3 – N2
So for any N, find the square of that N and subtract it from the cube of the number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getNthTerm( int N)
{
return (N * N * N) - (N * N);
}
int main()
{
int N = 4;
cout << getNthTerm(N);
return 0;
}
|
Java
class GFG
{
public static int getNthTerm( int N)
{
return (N * N * N) - (N * N);
}
public static void main(String args[])
{
int N = 4 ;
System.out.println(getNthTerm(N));
}
}
|
Python3
def getNthTerm(N):
return (N * N * N) - (N * N);
N = 4 ;
print (getNthTerm(N));
|
C#
using System;
class GFG {
public static int getNthTerm( int N) {
return (N * N * N) - (N * N);
}
public static void Main() {
int N = 4;
Console.Write(getNthTerm(N));
}
}
|
Javascript
<script>
function getNthTerm(N) {
return (N * N * N) - (N * N);
}
let N = 4;
document.write(getNthTerm(N));
</script>
|
Time complexity: O(1)
Auxiliary space: O(1)
Last Updated :
06 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...