求系列 4、11、30、85、248 的第 N 项。 .
给定一个正整数N 。任务是找到系列的第 N项:
4, 11, 30, 85, 248…
例子:
Input: N = 4
Output: 85
Input: N = 2
Output: 11
方法:
给定序列的第 N 项可以概括为:
TN = 3N + N
插图:
Input: N = 4
Output: 85
Explanation:
TN = 3N +N
= 34 + 4
= 81 +4
= 85
下面是上述问题的实现:
C++
// C++ program to find N-th term
// of the series-
// 4, 11, 30, 85, 248...
#include
using namespace std;
// Function to return Nth term
// of the series
int nthTerm(int N)
{
return pow(3, N) + N;
}
// Driver Code
int main()
{
// Get the value of N
int N = 4;
cout << nthTerm(N);
return 0;
}
Java
// Java program to find N-th term
// of the series-
// 4, 11, 30, 85, 248...
class GFG
{
// Function to return Nth term
// of the series
static int nthTerm(int N) {
return (int)Math.pow(3, N) + N;
}
// Driver Code
public static void main(String args[])
{
// Get the value of N
int N = 4;
System.out.println(nthTerm(N));
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# Python code for the above approach
# Function to return Nth term
# of the series
def nthTerm(N):
return (3 ** N) + N;
# Driver Code
# Get the value of N
N = 4;
print(nthTerm(N));
# This code is contributed by Saurabh Jaiswal
C#
// C# program to find N-th term
// of the series-
// 4, 11, 30, 85, 248...
using System;
class GFG
{
// Function to return Nth term
// of the series
static int nthTerm(int N) {
return (int)Math.Pow(3, N) + N;
}
// Driver Code
public static void Main()
{
// Get the value of N
int N = 4;
Console.Write(nthTerm(N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
85
时间复杂度: O(1)
辅助空间: O(1)