Find Nth term of the series 1, 8, 54, 384…
Given a number N. The task is to write a program to find the Nth term in the below series:
1, 8, 54, 384...
Examples:
Input : 3
Output : 54
For N = 3
Nth term = ( 3*3) * 3!
= 54
Input : 2
Output : 8
On observing carefully, the Nth term in the above series can be generalized as:
Nth term = ( N*N ) * ( N! )
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int fact( int N)
{
int i, product = 1;
for (i = 1; i <= N; i++)
product = product * i;
return product;
}
int nthTerm( int N)
{
return (N * N) * fact(N);
}
int main()
{
int N = 4;
cout << nthTerm(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int fact( int N)
{
int i, product = 1 ;
for (i = 1 ; i <= N; i++)
product = product * i;
return product;
}
public static int nthTerm( int N)
{
return (N * N) * fact(N);
}
public static void main(String[] args)
{
int N = 4 ;
System.out.println(nthTerm(N));
}
}
|
Python 3
def fact(N):
product = 1
for i in range ( 1 , N + 1 ):
product = product * i
return product
def nthTerm(N):
return (N * N) * fact(N)
if __name__ = = "__main__" :
N = 4
print (nthTerm(N))
|
C#
using System;
class GFG
{
public static int fact( int N)
{
int i, product = 1;
for (i = 1; i <= N; i++)
product = product * i;
return product;
}
public static int nthTerm( int N)
{
return (N * N) * fact(N);
}
public static void Main(String[] args)
{
int N = 4;
Console.WriteLine(nthTerm(N));
}
}
|
PHP
<?php
function fact( $N )
{
$product = 1;
for ( $i = 1; $i <= $N ; $i ++)
$product = $product * $i ;
return $product ;
}
function nthTerm( $N )
{
return ( $N * $N ) * fact( $N );
}
$N = 4;
echo nthTerm( $N );
?>
|
Javascript
<script>
function fact( N)
{
let i, product = 1;
for (i = 1; i <= N; i++)
product = product * i;
return product;
}
function nthTerm( N)
{
return (N * N) * fact(N);
}
let N = 4;
document.write(nthTerm(N));
</script>
|
Time Complexity: O(N)
Space Complexity: O(1) because using constant variables
Last Updated :
28 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...