Given a positive integer N. The task is to find Nth term of the series:
1, 2, 6, 21, 88, 445, . . .
Examples:
Input: N = 3
Output: 6Input: N = 6
Output: 445
Approach:
The given sequence follows the following pattern-
1, (1 * 1 + 1 = 2), (2 * 2 + 2 = 6), (6 * 3 + 3 = 21), (21 * 4 + 4 = 88), (88 * 5 + 5 = 445), …
Below steps can be used to solve the problem-
- For each iterative i, multiplying its previous element with i (Initially the element will be 1)
- And add multiplied elements with i.
- Finally, return the Nth term of the series.
Below is the implementation of the above approach
C++
// C++ program to find N-th term // of the series- // 1, 2, 6, 21, 88, 445... #include <bits/stdc++.h> using namespace std;
// Function to return Nth term // of the series int nthTerm( int N)
{ // Initializing a variable
int term = 1;
// Loop to iterate from 1 to N-1
for ( int i = 1; i < N; i++)
{
// Multiplying and adding previous
// element with i
term = term * i + i;
}
// returning the Nth term
return term;
} // Driver Code int main()
{ // Get the value of N
int N = 6;
cout << nthTerm(N);
return 0;
} |
Java
// Java program to find N-th term // of the series- // 1, 2, 6, 21, 88, 445... class GFG
{ // Function to return Nth term
// of the series
static int nthTerm( int N)
{
// Initializing a variable
int term = 1 ;
// Loop to iterate from 1 to N-1
for ( int i = 1 ; i < N; i++)
{
// Multiplying and adding previous
// element with i
term = term * i + i;
}
// returning the Nth term
return term;
}
// Driver Code
public static void main(String args[])
{
// Get the value of N
int N = 6 ;
System.out.println(nthTerm(N));
}
} // This code is contributed by saurabh_jaiswal. |
Python3
# Python code for the above approach # Function to return Nth term # of the series def nthTerm(N):
# Initializing a variable
term = 1 ;
# Loop to iterate from 1 to N-1
for i in range ( 1 , N):
# Multiplying and adding previous
# element with i
term = term * i + i;
# returning the Nth term
return term;
# Driver Code # Get the value of N N = 6 ;
print (nthTerm(N));
# This code is contributed by Saurabh Jaiswal |
C#
// C# program to find N-th term // of the series- // 1, 2, 6, 21, 88, 445... using System;
class GFG
{ // Function to return Nth term
// of the series
static int nthTerm( int N)
{
// Initializing a variable
int term = 1;
// Loop to iterate from 1 to N-1
for ( int i = 1; i < N; i++)
{
// Multiplying and adding previous
// element with i
term = term * i + i;
}
// returning the Nth term
return term;
}
// Driver Code
public static void Main()
{
// Get the value of N
int N = 6;
Console.Write(nthTerm(N));
}
} // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript code for the above approach
// Function to return Nth term
// of the series
function nthTerm(N)
{
// Initializing a variable
let term = 1;
// Loop to iterate from 1 to N-1
for (let i = 1; i < N; i++)
{
// Multiplying and adding previous
// element with i
term = term * i + i;
}
// returning the Nth term
return term;
}
// Driver Code
// Get the value of N
let N = 6;
document.write(nthTerm(N));
// This code is contributed by Potta Lokesh
</script>
|
Output
445
Time Complexity: O(N)
Auxiliary Space: O(1)