📜  求系列 1、2、6、21、88、445 的第 N 项。 .

📅  最后修改于: 2022-05-13 01:56:10.674000             🧑  作者: Mango

求系列 1、2、6、21、88、445 的第 N 项。 .

给定一个正整数N 。任务是找到系列的第 N项:

例子:

方法:

给定的序列遵循以下模式-

以下步骤可用于解决问题 -

  • 对于每个迭代i ,将其前一个元素与i相乘(最初元素将为 1)
  • 并用i 添加相乘的元素。
  • 最后,返回序列的第 N项。

下面是上述方法的实现

C++
// C++ program to find N-th term
// of the series-
// 1, 2, 6, 21, 88, 445...
#include 
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


输出
445

时间复杂度: O(N)

辅助空间: O(1)