Find Nth term of the series 4, 2, 2, 3, 6, …
Given a number N, the task is to find the N-th term in series 4, 2, 2, 3, 6, …
Example:
Input: N = 2
Output: 2
Input: N = 5
Output: 6
Approach:
- Nth number of the series is obtained by
- Multiplying the previous number with the position of the previous number itself.
- Divide the obtained number by 2.
- Since the starting number of the series is 4
1st term = 4
2nd term = (4 * 1) / 2 = 2
3rd term = (2 * 2) / 2 = 2
4th term = (2 * 3) / 2 = 3
5th term = (3 * 4) / 2 = 6
And, so on....
- In general, Nth number is obtained by formula:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int nthTerm( int N)
{
int nth = 0, first_term = 4;
int pi = 1, po = 1;
int n = N;
while (n > 1) {
pi *= n - 1;
n--;
po *= 2;
}
nth = (first_term * pi) / po;
return nth;
}
int main()
{
int N = 5;
cout << nthTerm(N) << endl;
return 0;
}
|
Java
class GFG
{
static int nthTerm( int N)
{
int nth = 0 , first_term = 4 ;
int pi = 1 , po = 1 ;
int n = N;
while (n > 1 )
{
pi *= n - 1 ;
n--;
po *= 2 ;
}
nth = (first_term * pi) / po;
return nth;
}
public static void main(String[] args)
{
int N = 5 ;
System.out.print(nthTerm(N) + "\n" );
}
}
|
Python3
def nthTerm(N) :
nth = 0 ; first_term = 4 ;
pi = 1 ; po = 1 ;
n = N;
while (n > 1 ) :
pi * = n - 1 ;
n - = 1 ;
po * = 2 ;
nth = (first_term * pi) / / po;
return nth;
if __name__ = = "__main__" :
N = 5 ;
print (nthTerm(N)) ;
|
C#
using System;
class GFG
{
static int nthTerm( int N)
{
int nth = 0, first_term = 4;
int pi = 1, po = 1;
int n = N;
while (n > 1)
{
pi *= n - 1;
n--;
po *= 2;
}
nth = (first_term * pi) / po;
return nth;
}
public static void Main(String[] args)
{
int N = 5;
Console.Write(nthTerm(N) + "\n" );
}
}
|
Javascript
<script>
function nthTerm(N)
{
let nth = 0, first_term = 4;
let pi = 1, po = 1;
let n = N;
while (n > 1) {
pi *= n - 1;
n--;
po *= 2;
}
nth = (first_term * pi) / po;
return nth;
}
let N = 5;
document.write(nthTerm(N));
</script>
|
Time Complexity: O(N) for given input N
Auxiliary Space: O(1)
Last Updated :
28 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...