📌  相关文章
📜  查找系列1、4、15、72、420的N个项。

📅  最后修改于: 2021-05-30 18:45:16             🧑  作者: Mango

给定数字N。任务是编写一个程序来查找以下系列中的N术语:

例子:

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

这个想法是首先找到给定数字N的阶乘,即N!。现在,上述系列中的第N个项将是:

下面是上述方法的实现:

C++
// CPP program to find N-th term of the series:
// 1, 4, 15, 72, 420…
#include 
using namespace std;
 
// Function to find factorial of N
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 series
int nthTerm(int N)
{
    return (factorial(N) * (N + 2) / 2);
}
 
// Driver Function
int main()
{
    int N = 6;
     
    cout << nthTerm(N);
     
    return 0;
}


Java
// Java program to find N-th
// term of the series:
// 1, 4, 15, 72, 420
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
     
// Function to find factorial of N
static 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 series
static int nthTerm(int N)
{
    return (factorial(N) * (N + 2) / 2);
}
 
// Driver Code
public static void main(String args[])
{
    int N = 6;
     
    System.out.println(nthTerm(N));
}
}
 
// This code is contributed by Subhadeep


Python3
# Python 3 program to find
# N-th term of the series:
# 1, 4, 15, 72, 420…
 
# Function for finding
# factorial of N
def 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 series
def nthTerm(N) :
 
    # return nth term
    return (factorial(N) * (N + 2) // 2)
 
# Driver code
if __name__ == "__main__" :
     
    N = 6
 
    # Function Calling
    print(nthTerm(N))
 
# This code is contributed
# by ANKITRAI1


C#
// C# program to find N-th
// term of the series:
// 1, 4, 15, 72, 420
using System;
 
class GFG
{
     
// Function to find factorial of N
static 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 series
static int nthTerm(int N)
{
    return (factorial(N) * (N + 2) / 2);
}
 
// Driver Code
public static void Main()
{
    int N = 6;
     
    Console.Write(nthTerm(N));
}
}
 
// This code is contributed by ChitraNayal


PHP


Javascript


C++
// CPP program to find N-th term of the series:
// 1, 4, 15, 72, 420…
// Using recursion
#include 
using namespace std;
 
// Function to find factorial of N
// with recursion
int factorial(int N)
{
    // base condition
    if( N == 0 || N == 1 )
        return 1;
    // use recursion
    return N * factorial( N - 1 );
}
 
// calculate Nth term of series
int nthTerm(int N)
{
    return (factorial(N) * (N + 2) / 2);
}
 
// Driver Function
int main()
{
    int N = 6;
     
    cout << nthTerm(N);
     
    return 0;
}


Java
// Java program to find N-th
// term of the series:
// 1, 4, 15, 72, 420
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
     
// Function to find factorial of N
static int factorial(int N)
{
    // base condition
    if( N == 0 || N == 1 )
        return 1;
    // use recursion
    return N * factorial( N - 1 );
}
 
// calculate Nth term of series
static int nthTerm(int N)
{
    return (factorial(N) * (N + 2) / 2);
}
 
// Driver Code
public static void main(String args[])
{
    int N = 6;
     
    System.out.println(nthTerm(N));
}
}


Python3
# Python3 program to find
# N-th term of the series:
# 1, 4, 15, 72, 420…
# Using recursion
 
# Function to find factorial
# of N with recursion
def 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 code
N = 6
print(nthTerm(N))
 
# This code is contributed
# by Shrikant13


C#
// C# program to find N-th
// term of the series:
// 1, 4, 15, 72, 420
using System;
 
class GFG
{
     
// Function to find factorial of N
static int factorial(int N)
{
    // base condition
    if( N == 0 || N == 1 )
        return 1;
    // use recursion
    return N * factorial( N - 1 );
}
 
// calculate Nth term of series
static int nthTerm(int N)
{
    return (factorial(N) * (N + 2) / 2);
}
 
// Driver Code
public static void Main()
{
    int N = 6;
     
    Console.Write(nthTerm(N));
}
}
 
// This code is contributed by ChitraNayal


PHP


输出:
2880

另一种方法:(使用递归)

C++

// CPP program to find N-th term of the series:
// 1, 4, 15, 72, 420…
// Using recursion
#include 
using namespace std;
 
// Function to find factorial of N
// with recursion
int factorial(int N)
{
    // base condition
    if( N == 0 || N == 1 )
        return 1;
    // use recursion
    return N * factorial( N - 1 );
}
 
// calculate Nth term of series
int nthTerm(int N)
{
    return (factorial(N) * (N + 2) / 2);
}
 
// Driver Function
int main()
{
    int N = 6;
     
    cout << nthTerm(N);
     
    return 0;
}

Java

// Java program to find N-th
// term of the series:
// 1, 4, 15, 72, 420
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
     
// Function to find factorial of N
static int factorial(int N)
{
    // base condition
    if( N == 0 || N == 1 )
        return 1;
    // use recursion
    return N * factorial( N - 1 );
}
 
// calculate Nth term of series
static int nthTerm(int N)
{
    return (factorial(N) * (N + 2) / 2);
}
 
// Driver Code
public static void main(String args[])
{
    int N = 6;
     
    System.out.println(nthTerm(N));
}
}

Python3

# Python3 program to find
# N-th term of the series:
# 1, 4, 15, 72, 420…
# Using recursion
 
# Function to find factorial
# of N with recursion
def 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 code
N = 6
print(nthTerm(N))
 
# This code is contributed
# by Shrikant13

C#

// C# program to find N-th
// term of the series:
// 1, 4, 15, 72, 420
using System;
 
class GFG
{
     
// Function to find factorial of N
static int factorial(int N)
{
    // base condition
    if( N == 0 || N == 1 )
        return 1;
    // use recursion
    return N * factorial( N - 1 );
}
 
// calculate Nth term of series
static int nthTerm(int N)
{
    return (factorial(N) * (N + 2) / 2);
}
 
// Driver Code
public static void Main()
{
    int N = 6;
     
    Console.Write(nthTerm(N));
}
}
 
// This code is contributed by ChitraNayal

的PHP


输出:
2880

时间复杂度:O(N)
注意:上面的代码不适用于大的N值。要查找大的N的值,请对大数使用阶乘的概念。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”