📜  找到系列 5、13、37、109、325 的第 N 项。 . .

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

找到系列 5、13、37、109、325 的第 N 项。 . .

给定一个正整数N 。任务是找到序列5, 13, 37, 109, 325, ....第 N项。

例子

方法:使用以下模式形成的序列。对于任何值 N

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to return Nth term
// of the series
int calcNum(int N)
{
  return 4 * pow(3, N - 1) + 1;
}
 
// Driver Code
int main()
{
    int N = 5;
 
    cout << calcNum(N);
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
public class GFG
{
 
  // Function to return Nth term
  // of the series
  static int calcNum(int N)
  {
    return 4 * (int)Math.pow(3, N - 1) + 1;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int N = 5;
    System.out.println(calcNum(N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python 3 program for the above approach
 
# Function to return Nth term
# of the series
def calcNum(N):
     
    return 4 * pow(3, N - 1) + 1
 
# Driver Code
if __name__ == "__main__":
 
    N=5
 
    print(calcNum(N))
 
# This code is contributed by Abhishek Thakur.


C#
// C# program to implement
// the above approach
using System;
 
public class GFG{
 
  // Function to return Nth term
  // of the series
  static int calcNum(int N)
  {
    return 4 * (int)Math.Pow(3, N - 1) + 1;
  }
 
  // Driver Code
  static public void Main ()
  {
    int N = 5;
    Console.Write(calcNum(N));
  }
}
 
 
// This code is contributed by SHubham Singh


Javascript



输出
325

时间复杂度:O(1)
辅助空间:O(1)