第二个十进制数系列可以表示为
7, 22, 45, 76, 115, 162, 217, 280,,…..
第N个学期
给定整数N。任务是找到给定级数的第N个项。
例子:
Input: N = 1
Output: 7
Input: N = 4
Output: 76
方法:想法是找到第二个十进制数的通用术语。以下是第二个十进制数的通用项的计算:
1st Term = 1 * (4*1 + 3) = 7
2nd term = 2 * (4*2 + 3) = 22
3rd term = 3 * (4*3 + 3) = 45
4th term = 4 * (4*4 + 3) = 76
.
.
.
Nth term = n * (4 * n + 3)
Therefore, the Nth term of the series is given as
下面是上述方法的实现:
C++
// C++ implementation to
// find N-th term in the series
#include
#include
using namespace std;
// Function to find N-th term
// in the series
void findNthTerm(int n)
{
cout << n * (4 * n + 3) << endl;
}
// Driver Code
int main()
{
int N = 4;
findNthTerm(N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find N-th term
// in the series
static void findNthTerm(int n)
{
System.out.println(n * (4 * n + 3));
}
// Driver code
public static void main(String[] args)
{
int N = 4;
findNthTerm(N);
}
}
// This code is contributed by Pratima Pandey
Python3
# Python3 implementation to
# find N-th term in the series
# Function to find N-th term
# in the series
def findNthTerm(n):
print(n * (4 * n + 3))
# Driver Code
N = 4;
findNthTerm(N);
# This code is contributed by Code_Mech
C#
// C# program for the above approach
using System;
class GFG{
// Function to find N-th term
// in the series
static void findNthTerm(int n)
{
Console.WriteLine(n * (4 * n + 3));
}
// Driver code
public static void Main(String[] args)
{
int N = 4;
findNthTerm(N);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
76
时间复杂度: O(1)
辅助空间: O(1)
参考: OEIS