Find Nth term of series 1, 4, 15, 72, 420…
Given a number N. The task is to write a program to find the Nth term in the below series:
1, 4, 15, 72, 420…
Examples:
Input: 3
Output: 15
Explanation: For N = 3, we know that the factorial of 3 is 6 Nth term = 6*(3+2)/2
Input: 6
Output: 2880
Explanation: For N = 6, we know that the factorial of 6 is 720 Nth term = 620*(6+2)/2 = 2880
The idea is to first find the factorial of the given number N, that is N!. Now the N-th term in the above series will be:
N-th term = N! * (N + 2)/2
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int factorial( int N)
{
int fact = 1;
for ( int i = 1; i <= N; i++)
fact = fact * i;
return fact;
}
int nthTerm( int N)
{
return (factorial(N) * (N + 2) / 2);
}
int main()
{
int N = 6;
cout << nthTerm(N);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static int factorial( int N)
{
int fact = 1 ;
for ( int i = 1 ; i <= N; i++)
fact = fact * i;
return fact;
}
static int nthTerm( int N)
{
return (factorial(N) * (N + 2 ) / 2 );
}
public static void main(String args[])
{
int N = 6 ;
System.out.println(nthTerm(N));
}
}
|
Python3
def factorial(N) :
fact = 1
for i in range ( 1 , N + 1 ) :
fact = fact * i
return fact
def nthTerm(N) :
return (factorial(N) * (N + 2 ) / / 2 )
if __name__ = = "__main__" :
N = 6
print (nthTerm(N))
|
C#
using System;
class GFG
{
static int factorial( int N)
{
int fact = 1;
for ( int i = 1; i <= N; i++)
fact = fact * i;
return fact;
}
static int nthTerm( int N)
{
return (factorial(N) * (N + 2) / 2);
}
public static void Main()
{
int N = 6;
Console.Write(nthTerm(N));
}
}
|
PHP
<?php
function factorial( $N )
{
$fact = 1;
for ( $i = 1; $i <= $N ; $i ++)
$fact = $fact * $i ;
return $fact ;
}
function nthTerm( $N )
{
return (factorial( $N ) *
( $N + 2) / 2);
}
$N = 6;
echo nthTerm( $N );
?>
|
Javascript
<script>
function factorial( N)
{
let fact = 1;
for (let i = 1; i <= N; i++)
fact = fact * i;
return fact;
}
function nthTerm(N)
{
return (factorial(N) * (N + 2) / 2);
}
let N = 6;
document.write( nthTerm(N) );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Another approach :(Using recursion)
C++
#include <iostream>
using namespace std;
int factorial( int N)
{
if ( N == 0 || N == 1 )
return 1;
return N * factorial( N - 1 );
}
int nthTerm( int N)
{
return (factorial(N) * (N + 2) / 2);
}
int main()
{
int N = 6;
cout << nthTerm(N);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static int factorial( int N)
{
if ( N == 0 || N == 1 )
return 1 ;
return N * factorial( N - 1 );
}
static int nthTerm( int N)
{
return (factorial(N) * (N + 2 ) / 2 );
}
public static void main(String args[])
{
int N = 6 ;
System.out.println(nthTerm(N));
}
}
|
Python3
def factorial(N):
if N = = 0 or N = = 1 :
return 1
return N * factorial(N - 1 )
def nthTerm(N):
return (factorial(N) * (N + 2 ) / / 2 )
N = 6
print (nthTerm(N))
|
C#
using System;
class GFG
{
static int factorial( int N)
{
if ( N == 0 || N == 1 )
return 1;
return N * factorial( N - 1 );
}
static int nthTerm( int N)
{
return (factorial(N) * (N + 2) / 2);
}
public static void Main()
{
int N = 6;
Console.Write(nthTerm(N));
}
}
|
PHP
<?php
function factorial( $N )
{
if ( $N == 0 or $N == 1)
return 1;
return $N * factorial( $N - 1);
}
function nthTerm( $N )
{
return (factorial( $N ) *
( $N + 2) / 2);
}
$N = 6;
echo nthTerm( $N );
?>
|
Javascript
<script>
function factorial(N)
{
if (N == 0 || N == 1)
return 1;
return N * factorial(N - 1);
}
function nthTerm(N)
{
return (factorial(N) * (N + 2) / 2);
}
let N = 6;
document.write(nthTerm(N));
</script>
|
Time complexity: O(N)
Auxiliary Space: O(N), for recursion call stack.
Note: Above code wouldn’t work for large values of N. To find the values for large N, use the concept of Factorial for large numbers.
Last Updated :
11 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...