📌  相关文章
📜  在系列7、15、32,…中找到第n个项

📅  最后修改于: 2021-04-27 21:45:41             🧑  作者: Mango

给定一个系列7,15,32,……找到该系列的第n个术语。
例子 :

Input : 5
Output : 138

Input : 7
Output : 568

方法:通过查看序列的模式,我们可以轻松地识别出它是一个混合序列。
S = 7、15、32…..
系列中的每个元素都乘以2,然后比前一个增加一个。
S = 7、15(2 * 7 + 1),32(2 * 15 + 2)……。
通过使用迭代,我们可以轻松地找到级数的n个项。
下面是上述方法的实现:

C++
// CPP program to find nth term
#include 
using namespace std;
 
// utility function
int findTerm(int n)
{
    if (n == 1)
        return n;
 
    else {
 
        // since first element of the
        // series is 7, we initailise
        // a variable with 7
        int term = 7;
 
        // Using iteration to find nth
        // term
        for (int i = 2; i <= n; i++)
            term = term * 2 + (i - 1);       
 
        return term;
    }
}
 
// driver function
int main()
{
    int n = 5;
    cout << findTerm(n);
    return 0;
}


Java
// Java program to find nth term
import java.lang.*;
 
class GFG {
     
// utility function
static int findTerm(int n)
{
    if (n == 1)
    return n;
 
    else {
 
    // since first element of the
    // series is 7, we initailise
    // a variable with 7
    int term = 7;
 
    // Using iteration to find nth
    // term
    for (int i = 2; i <= n; i++)
        term = term * 2 + (i - 1);
 
    return term;
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    System.out.print(findTerm(n));
}
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to find nth term
 
# utility function
def findTerm(n) :
 
    if n == 1 :
        return n
    else :
         
        # since first element of the
        # series is 7, we initailise
        # a variable with 7
        term = 7
 
        # Using iteration to find nth
        # term
        for i in range(2, n + 1) :
            term = term * 2 + (i - 1);    
 
    return term;
 
# driver function
print (findTerm(5))
 
# This code is contributed by Saloni Gupta


C#
// C# program to find nth term
using System;
 
class GFG {
 
    // utility function
    static int findTerm(int n)
    {
        if (n == 1)
            return n;
 
        else {
 
            // since first element of the
            // series is 7, we initailise
            // a variable with 7
            int term = 7;
 
            // Using iteration to find nth
            // term
            for (int i = 2; i <= n; i++)
                term = term * 2 + (i - 1);
 
            return term;
        }
    }
 
    // Driver code
    public static void Main()
    {
        int n = 5;
        Console.WriteLine(findTerm(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

138