Find Nth term of the series 0, 2, 6, 12, 20, 30, 42…
Last Updated :
19 Apr, 2023
Given a positive integer N, the task is to find the Nth term of the series
0, 2, 6, 12, 20…till N terms
Examples:
Input: N = 7
Output: 42
Input: N = 10
Output: 90
Approach:
From the given series, find the formula for Nth term-
1st term = 1 * (1 – 1) = 0
2nd term = 2 * (2 – 1) = 2
3rd term = 3 * (3 – 1) = 6
4th term = 4 * (4 – 1) = 12
.
.
Nth term = N * (N – 1)
The Nth term of the given series can be generalized as-
TN = N * (N – 1)
Illustration:
Input: N = 7
Output: 42
Explanation:
TN = N * (N – 1)
= 7 * (7 – 1)
= 7 * 6
= 42
Below is the implementation of the above approach-
C++
#include <iostream>
using namespace std;
int nthTerm( int n)
{
return n * n - n;
}
int main()
{
int N = 7;
cout << nthTerm(N) << endl;
return 0;
}
|
C
#include <stdio.h>
int nthTerm( int n)
{
return n * n - n;
}
int main()
{
int N = 7;
printf ( "%d" , nthTerm(N));
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int N = 7 ;
System.out.println(nthTerm(N));
}
public static int nthTerm( int n)
{
return n * n - n;
}
}
|
Python3
def nthTerm(n):
return n * n - n;
N = 7 ;
print (nthTerm(N));
|
C#
using System;
public class GFG
{
public static int nthTerm( int n)
{
return n * n - n;
}
static public void Main()
{
int N = 7;
Console.Write(nthTerm(N));
}
}
|
Javascript
<script>
function nthTerm(n) {
return n * n - n;
}
let N = 7;
document.write(nthTerm(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
Share your thoughts in the comments
Please Login to comment...