给定数字N,任务是在给定系列中找到第N个项:
9, 23, 45, 75, 113, 159......
例子:
Input: 4
Output: 113
Explanation:
For N = 4
Nth term = ( 2 * N + 3 )*( 2 * N + 3 ) - 2 * N
= ( 2 * 4 + 3 )*( 2 * 4 + 3 ) - 2 * 4
= 113
Input: 10
Output: 509
方法:
给定级数的N个项可以概括为:
Nth term of the series : ( 2 * N + 3 )*( 2 * N + 3 ) - 2 * N
下面是上述问题的实现:
程序:
C++
// CPP program to find N-th term of the series:
// 9, 23, 45, 75, 113...
#include
using namespace std;
// calculate Nth term of series
int nthTerm(int N)
{
return (2 * N + 3) * (2 * N + 3) - 2 * N;
}
// Driver Function
int main()
{
// Get the value of N
int N = 4;
// Find the Nth term
// and print it
cout << nthTerm(N);
return 0;
}
Java
// Java program to find
// N-th term of the series:
// 9, 23, 45, 75, 113...
class GFG
{
// calculate Nth term of series
static int nthTerm(int N)
{
return (2 * N + 3) *
(2 * N + 3) - 2 * N;
}
// Driver code
public static void main(String[] args)
{
// Get the value of N
int N = 4;
// Find the Nth term
// and print it
System.out.println(nthTerm(N));
}
}
// This code is contributed by Bilal
Python3
# Python program to find
# N-th term of the series:
# 9, 23, 45, 75, 113...
def nthTerm(N):
# calculate Nth term of series
return ((2 * N + 3) *
(2 * N + 3) - 2 * N);
# Driver Code
# Get the value of N
n = 4
# Find the Nth term
# and print it
print(nthTerm(n))
# This code is contributed by Bilal
C#
// C# program to find
// N-th term of the series:
// 9, 23, 45, 75, 113...
using System;
class GFG
{
// calculate Nth term of series
static int nthTerm(int N)
{
return (2 * N + 3) *
(2 * N + 3) - 2 * N;
}
// Driver code
public static void Main()
{
// Get the value of N
int N = 4;
// Find the Nth term
// and print it
Console.WriteLine(nthTerm(N));
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
PHP
Javascript
输出:
113
时间复杂度: O(1)