📜  找到系列 5、10、20、40 的第 N 项…

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

找到系列 5、10、20、40 的第 N 项…

给定一个正整数N ,任务是找到序列的第 N

例子

方法:

给定系列的第 N 项可以概括为-

可以按照以下步骤推导出公式-

插图:

以下是上述方法的实现 -

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to calculate nth term
int nTerm(int a, int r, int n)
{
    return a * pow(r, n - 1);
}
 
// Driver code
int main()
{
    // Value of N
    int N = 5;
 
    // First term of the series
    int a = 5;
 
    // Common ratio
    int r = 2;
 
    cout << nTerm(a, r, N);
    return 0;
}


C
// C program to implement
// the above approach
#include 
#include 
 
// Function to calculate nth term
int nTerm(int a, int r, int n)
{
    return a * pow(r, n - 1);
}
 
// Driver code
int main()
{
    // Value of N
    int N = 5;
 
    // First term
    int a = 5;
 
    // Common ratio
    int r = 2;
 
    printf("%d", nTerm(a, r, n));
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG {
    // Driver code
    public static void main(String[] args)
    {
        // Value of N
        int N = 5;
 
        // First term
        int a = 5;
 
        // Common ratio
        int r = 2;
        System.out.println(nTerm(a, r, N));
    }
 
    // Function to calculate nth term
    public static int nTerm(int a, int r, int n)
    {
        return a * ((int)Math.pow(r, n - 1));
    }
}


Python3
# python3 program to implement
# the above approach
 
# Function to calculate nth term
def nTerm(a, r, n):
 
    return a * pow(r, n - 1)
 
# Driver code
if __name__ == "__main__":
 
    # Value of N
    N = 5
 
    # First term of the series
    a = 5
 
    # Common ratio
    r = 2
 
    print(nTerm(a, r, N))
 
# This code is contributed by rakeshsahni


C#
using System;
 
public class GFG
{
   
    // Function to calculate nth term
    public static int nTerm(int a, int r, int n)
    {
        return a * ((int)Math.Pow(r, n - 1));
    }
    static public void Main()
    {
 
        // Code
        // Value of N
        int N = 5;
 
        // First term
        int a = 5;
 
        // Common ratio
        int r = 2;
        Console.Write(nTerm(a, r, N));
    }
}
 
// This code is contributed by Potta Lokesh


Javascript


输出
80

时间复杂度: O(1)

辅助空间: O(1)