📌  相关文章
📜  找到系列2、15、41、80、132的N个项。

📅  最后修改于: 2021-05-05 01:28:01             🧑  作者: Mango

给定数字N ,任务是找到序列2、15、41、80、132 …N个项。
例子:

方法:从给定的系列中,第N个项的公式可以发现为:

1st term = 2
2nd term = 2 + 1 * 13 = 15
3rd term = 15 + 2 * 13 = 41
4th term = 41 + 3 * 13 = 80
.
.
Nth term = (N - 1)th term
         + (N - 1) * 13

因此,该系列的第N个项
(N - 1)^{th} \text{ term} + (N - 1) * 13
以下是使用递归查找第N个术语的步骤:
从值N递归地迭代:

  • 基本情况:如果递归调用的值为1,则它是序列的第一项。因此,从函数返回2。
if(N == 1) 
  return 2;
  • 递归调用:如果不满足基本情况,则根据系列的第N个术语从函数递归进行迭代:
(N - 1) * 13 + func(N - 1);
  • 返回语句:在每个递归调用(基本情况除外)中,返回递归函数以进行下一次迭代。
return ((13 * (N - 1))
       + func(N, i + 1));

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Recursive function to find Nth term
int nthTerm(int N)
{
    // Base Case
    if (N == 1) {
        return 2;
    }
 
    // Recursive Call according to
    // Nth term of the series
    return ((N - 1) * 13)
           + nthTerm(N - 1);
}
 
// Driver Code
int main()
{
    // Input Nth term
    int N = 17;
 
    // Function call
    cout << nthTerm(N) << endl;
    return 0;
}


Java
// Java program for the above approach
 
class GFG{
 
// Recursive function to find Nth term
static int nthTerm(int N)
{
    // Base Case
    if (N == 1)
    {
        return 2;
    }
 
    // Recursive Call according to
    // Nth term of the series
    return ((N - 1) * 13) +
            nthTerm(N - 1);
}
 
// Driver Code
public static void main(String[] args)
{
    // Input Nth term
    int N = 17;
 
    // Function call
    System.out.print(nthTerm(N) + "\n");
}
}
 
// This code is contributed by 29AjayKumar


Python 3
# Python 3 program for the above approach
 
# Recursive function to find Nth term
def nthTerm(N):
     
    # Base Case
    if (N == 1):
        return 2
 
    # Recursive Call according to
    # Nth term of the series
    return ((N - 1) * 13) + nthTerm(N - 1)
 
# Driver Code
if __name__ == '__main__':
     
    # Input Nth term
    N = 17
 
    # Function call
    print(nthTerm(N))
 
# This code is contributed by Bhupendra_Singh


C#
// C# program for the above approach
using System;
 
public class GFG{
 
// Recursive function to find Nth term
static public int nthTerm(int N)
{
    // Base Case
    if (N == 1)
    {
        return 2;
    }
     
    // Recursive Call according to
    // Nth term of the series
    return ((N - 1) * 13) + nthTerm(N - 1);
}
     
// Driver Code
static public void Main ()
{
    // Input Nth term
    int N = 17;
     
    // Function call
    Console.WriteLine(nthTerm(N));
}
}
 
//This code is contributed by shubhamsingh10


Javascript


输出:
1770