Find Nth term of series 1, 4, 15, 72, 420... - GeeksforGeeks

14 May, 2021
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
For N = 3, we know that the factorial of 3 is 6
Nth term = 6*(3+2)/2
         = 15

Input: 6
Output: 2880
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++
Java
Python3
C#
PHP
Javascript
// CPP program to find N-th term of the series:// 1, 4, 15, 72, 420...#include <iostream>using namespace std; // Function to find factorial of Nint factorial(int N){    int fact = 1;     for (int i = 1; i <= N; i++)        fact = fact * i;             // return factorial of N            return fact;} // calculate Nth term of seriesint nthTerm(int N){    return (factorial(N) * (N + 2) / 2);} // Driver Functionint main(){    int N = 6;         cout << nthTerm(N);         return 0;}
// Java program to find N-th// term of the series:// 1, 4, 15, 72, 420import java.util.*;import java.lang.*;import java.io.*; class GFG{     // Function to find factorial of Nstatic int factorial(int N){    int fact = 1;     for (int i = 1; i <= N; i++)        fact = fact * i;             // return factorial of N        return fact;} // calculate Nth term of seriesstatic int nthTerm(int N){    return (factorial(N) * (N + 2) / 2);} // Driver Codepublic static void main(String args[]){    int N = 6;         System.out.println(nthTerm(N));}} // This code is contributed by Subhadeep
# Python 3 program to find# N-th term of the series:# 1, 4, 15, 72, 420... # Function for finding# factorial of Ndef factorial(N) :    fact = 1    for i in range(1, N + 1) :        fact = fact * i     # return factorial of N    return fact # Function for calculating# Nth term of seriesdef nthTerm(N) :     # return nth term    return (factorial(N) * (N + 2) // 2) # Driver codeif __name__ == "__main__" :         N = 6     # Function Calling    print(nthTerm(N)) # This code is contributed# by ANKITRAI1
// C# program to find N-th// term of the series:// 1, 4, 15, 72, 420using System; class GFG{     // Function to find factorial of Nstatic int factorial(int N){    int fact = 1;     for (int i = 1; i <= N; i++)        fact = fact * i;             // return factorial of N        return fact;} // calculate Nth term of seriesstatic int nthTerm(int N){    return (factorial(N) * (N + 2) / 2);} // Driver Codepublic static void Main(){    int N = 6;         Console.Write(nthTerm(N));}} // This code is contributed by ChitraNayal
<?php// PHP program to find// N-th term of the series:// 1, 4, 15, 72, 420... // Function for finding// factorial of Nfunction factorial($N){    $fact = 1;    for($i = 1; $i <= $N; $i++)        $fact = $fact * $i;     // return factorial of N    return $fact;} // Function for calculating// Nth term of seriesfunction nthTerm($N){     // return nth term    return (factorial($N) *           ($N + 2) / 2);} // Driver code$N = 6; // Function Callingecho nthTerm($N); // This code is contributed// by mits?>
<script> // JavaScript program to find N-th term of the series:// 1, 4, 15, 72, 420... // Function to find factorial of Nfunction factorial( N){    let fact = 1;     for (let i = 1; i <= N; i++)        fact = fact * i;             // return factorial of N            return fact;} // calculate Nth term of seriesfunction nthTerm(N){    return (factorial(N) * (N + 2) / 2);}// Driver code     let N = 6;   document.write( nthTerm(N) ); // This code contributed by aashish1995 </script>
2880
 
Another approach :(Using recursion) 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find N-th term of the series:// 1, 4, 15, 72, 420...// Using recursion#include <iostream>using namespace std; // Function to find factorial of N// with recursionint factorial(int N){    // base condition    if( N == 0 || N == 1 )        return 1;    // use recursion    return N * factorial( N - 1 );} // calculate Nth term of seriesint nthTerm(int N){    return (factorial(N) * (N + 2) / 2);} // Driver Functionint main(){    int N = 6;         cout << nthTerm(N);         return 0;}
// Java program to find N-th// term of the series:// 1, 4, 15, 72, 420import java.util.*;import java.lang.*;import java.io.*; class GFG{     // Function to find factorial of Nstatic int factorial(int N){    // base condition    if( N == 0 || N == 1 )        return 1;    // use recursion    return N * factorial( N - 1 );} // calculate Nth term of seriesstatic int nthTerm(int N){    return (factorial(N) * (N + 2) / 2);} // Driver Codepublic static void main(String args[]){    int N = 6;         System.out.println(nthTerm(N));}}
# Python3 program to find# N-th term of the series:# 1, 4, 15, 72, 420...# Using recursion # Function to find factorial# of N with recursiondef factorial(N):     # base condition    if N == 0 or N == 1:        return 1     # use recursion    return N * factorial(N - 1) def nthTerm(N):         # calculate Nth term of series    return (factorial(N) * (N + 2) // 2) # Driver codeN = 6print(nthTerm(N)) # This code is contributed# by Shrikant13
// C# program to find N-th// term of the series:// 1, 4, 15, 72, 420using System; class GFG{     // Function to find factorial of Nstatic int factorial(int N){    // base condition    if( N == 0 || N == 1 )        return 1;    // use recursion    return N * factorial( N - 1 );} // calculate Nth term of seriesstatic int nthTerm(int N){    return (factorial(N) * (N + 2) / 2);} // Driver Codepublic static void Main(){    int N = 6;         Console.Write(nthTerm(N));}} // This code is contributed by ChitraNayal
<?php// PHP program to find// N-th term of the series:// 1, 4, 15, 72, 420... // Function to find factorial// of N with recursionfunction factorial($N){    // base condition    if($N == 0 or $N == 1)        return 1;             // use recursion    return $N * factorial($N - 1);} // calculate Nth term of seriesfunction nthTerm($N){    return (factorial($N) *           ($N + 2) / 2);} // Driver Code$N = 6; echo nthTerm($N); // This code is contributed// by Shashank?>
<script> // Javascript program to find N-th// term of the series:// 1, 4, 15, 72, 420 // Function to find factorial of Nfunction factorial(N){         // Base condition    if (N == 0 || N == 1)        return 1;             // Use recursion    return N * factorial(N - 1);} // Calculate Nth term of seriesfunction nthTerm(N){    return(factorial(N) * (N + 2) / 2);} // Driver Codelet N = 6; document.write(nthTerm(N)); // This code is contributed by avanitrachhadiya2155 </script>
2880
 
Time complexity: O(N)Note: Above code wouldn’t not work for large values of N. To find the values for large N, use the concept of Factorial for large numbers. 
ankthon
tufan_gupta2000
Mithun Kumar
ukasp
Rajput-Ji
Shashank12
shrikanth13
aashish1995
avanitrachhadiya2155
factorial
C++
Pattern Searching
School Programming
Pattern Searching
factorial
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
KMP Algorithm for Pattern Searching
Rabin-Karp Algorithm for Pattern Searching
Check if a string is substring of another
Naive algorithm for Pattern Searching
Boyer Moore Algorithm for Pattern Searching