求系列 3、5、9、17、33 的第 N 项。 .
给定一个正整数N ,任务是找到序列的第 N项-
3, 5, 9, 17, 33…till N terms
例子:
Input: N = 4
Output: 17
Input: N = 3
Output: 9
方法:
考虑下面的例子:
Lets say N = 4
The 4th term of the given series is 17, i.e. : 2 ^ 4 + 1 = 16 + 1 = 17
Similarly, lets say N = 3
The 3rd term of the given series is : 2 ^ 3 + 1 = 8 + 1 = 9 (which is correct).
因此,我们可以使用上述观察来找出序列的第 N项的关系:
1st term = 3 = 21 + 1
2nd term = 22 + 1 = 5
3rd term = 23 + 1 = 9
4th term = 24 + 1 = 17
.
.
Therefore, Nth term can be found out using following relation: 2N + 1
Upon generalising, the relation for Nth term can be represented as:
以下是上述方法的实现 -
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to return Nth
// term of the series
int findTerm(int N)
{
return pow(2, N) + 1;
}
// Driver Code
int main()
{
int N = 6;
cout << findTerm(N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG {
// Function to return Nth
// term of the series
static int findTerm(int N)
{
return (int)Math.pow(2, N) + 1;
}
// Driver Code
public static void main (String[] args)
{
int N = 6;
System.out.print(findTerm(N));
}
}
// This code is contributed by Shubham Singh
Python3
# Python program to implement
# the above approach
# Function to return Nth
# term of the series
def findTerm(N):
return (2 ** N) + 1;
# Driver Code
N = 6;
print(findTerm(N));
# This code is contributed by gfgking
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to return Nth
// term of the series
static int findTerm(int N)
{
return (int)Math.Pow(2, N) + 1;
}
// Driver Code
public static void Main()
{
int N = 6;
Console.Write(findTerm(N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
65
时间复杂度: O(1)
辅助空间: O(1)