📌  相关文章
📜  找出系列9,45,243,1377的N个项

📅  最后修改于: 2021-04-24 20:00:53             🧑  作者: Mango

给定整数N ,任务是打印序列9、45、243、1377、8019等N
例子:

方法:让第N项为A n ,我们可以通过观察序列轻松得出第N项:

下面是上述方法的实现:

CPP
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the nth term of the given series
long nthterm(int n)
{
 
    // nth term
    int An = (pow(1, n) + pow(2, n)) * pow(3, n);
 
    return An;
}
 
// Driver code
int main()
{
    int n = 3;
 
    cout << nthterm(n);
 
    return 0;
}


Python
# Python3 implementation of the approach
  
# Function to return the nth term of the given series
def nthterm(n):
  
    # nth term
    An = (1**n + 2**n) * (3**n)
  
    return An;
  
  
# Driver code
n = 3
print(nthterm(n))


Java
// Java implementation of the approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
public class GFG {
 
    // Function to return the nth term of the given series
    static int nthTerm(int n)
    {
        int An
            = ((int)Math.pow(1, n) + (int)Math.pow(2, n))
              * (int)Math.pow(3, n);
 
        return An;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 3;
        System.out.println(nthTerm(n));
    }
}


C#
// C# implementation of the approach
using System;
public class GFG {
 
    // Function to return the nth term of the given series
    static int nthTerm(int n)
    {
 
        int An
            = ((int)Math.Pow(1, n) + (int)Math.Pow(2, n))
              * (int)Math.Pow(3, n);
 
        return An;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 3;
        Console.WriteLine(nthTerm(n));
    }
}


PHP


Javascript


输出:
243