第二个五边形数字是对象的集合,可以以规则五边形的形式排列。
五角形的第二个系列是:
2, 7, 15, 26, 40, 57, 77, 100, 126, …..
查找第二个五角形系列的第N个项
给定整数N。任务是找到第二个五边形级数的第N个项。
例子:
Input: N = 1
Output: 2
Input: N = 4
Output: 26
方法:想法是找到可以通过以下观察得出的系列的通用术语:
Series = 2, 7, 15, 26, 40, 57, 77, 100, 126, …..
Difference = 7 – 2, 15 – 7, 26 – 15, 40 – 26, …………….
= 5, 8, 11, 14……which is an AP
So nth term of given series
nth term = 2 + (5 + 8 + 11 + 14 …… (n-1)terms)
= 2 + (n-1)/2*(2*5+(n-1-1)*3)
= 2 + (n-1)/2*(10+3n-6)
= 2 + (n-1)*(3n+4)/2
= n*(3*n + 1)/2
因此,该系列的第N个项为
下面是上述方法的实现:
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 * (3 * n + 1) / 2
<< endl;
}
// Driver code
int main()
{
int N = 4;
findNthTerm(N);
return 0;
}
Java
// Java implementation to
// find N-th term in the series
class GFG{
// Function to find N-th term
// in the series
static void findNthTerm(int n)
{
System.out.print(n * (3 *
n + 1) / 2 + "\n");
}
// Driver code
public static void main(String[] args)
{
int N = 4;
findNthTerm(N);
}
}
// This code is contributed by 29AjayKumar
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 * (3 * n + 1) // 2, end = " ");
# Driver code
N = 4;
findNthTerm(N);
# This code is contributed by Code_Mech
C#
// C# implementation to
// find N-th term in the series
using System;
class GFG{
// Function to find N-th term
// in the series
static void findNthTerm(int n)
{
Console.Write(n * (3 *
n + 1) / 2 + "\n");
}
// Driver code
public static void Main()
{
int N = 4;
findNthTerm(N);
}
}
// This code is contributed by Code_Mech
Javascript
输出:
26
参考: https : //oeis.org/A005449