给定整数N。任务是编写一个程序来查找给定系列的第N个术语:
2 + 6 + 13 + 23 + …
例子:
Input : N = 5
Output : 36
Input : N = 10
Output : 146
请参阅有关如何找到系列的第N个术语的文章,以了解在找到任何给定系列的第N个术语之后的想法。
给定级数的广义第N个项是:
下面是上述方法的实现:
C++
//CPP program to find Nth term of the series
// 2 + 6 + 13 + 23 + 36 + ...
#include
using namespace std;
// calculate Nth term of given series
int Nth_Term(int n)
{
return (3 * pow(n, 2) - n + 2) / (2);
}
// Driver code
int main()
{
int N = 5;
cout<
Java
//Java program to find Nth term of the series
// 2 + 6 + 13 + 23 + 36 + ...
import java.io.*;
class GFG {
// calculate Nth term of given series
static int Nth_Term(int n)
{
return (int)(3 * Math.pow(n, 2) - n + 2) / (2);
}
// Driver code
public static void main (String[] args) {
int N = 5;
System.out.println(Nth_Term(N));
}
}
// This code is contributed by anuj_67..
Python3
# Python program to find Nth term of the series
# 2 + 6 + 13 + 23 + 36 + ...
# calculate Nth term of given series
def Nth_Term(n):
return (3 * pow(n, 2) - n + 2) // (2)
# Driver code
N = 5
print(Nth_Term(N))
C#
// C# program to find Nth term of the series
// 2 + 6 + 13 + 23 + 36 + ...
class GFG
{
// calculate Nth term of given series
static int Nth_Term(int n)
{
return (int)(3 * System.Math.Pow(n, 2) -
n + 2) / (2);
}
// Driver code
static void Main ()
{
int N = 5;
System.Console.WriteLine(Nth_Term(N));
}
}
// This code is contributed by mits
PHP
Javascript
输出:
36