第二个七边形数字系列可以表示为
4, 13, 27, 46, 70, 99, 133, 172, 216, …..
第N个学期
给定整数N。任务是找到给定级数的第N个项。
例子:
Input: N = 1
Output: 4
Input: N = 4
Output: 46
方法:想法是找到第二个七边形数字的总称。下面是第二个七边形数字的通用术语的计算:
Series = 4, 13, 27, 46, 70, 99, 133, 172, 216, …..
Difference = 13-4, 27-13, 46-27, 70-46, …………….
Difference = 9, 14, 19, 24……which is a AP
So nth term of given series
nth term = 4 + (9 + 14 + 19 + 24 …… (n-1)terms)
nth term = 4 + (n-1)/2*(2*9+(n-1-1)*5)
nth term = 4 + (n-1)/2*(18+5n-10)
nth term = 4 + (n-1)*(5n+8)/2
nth term = n*(5*n+3)/2
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 * (5 * n + 3) / 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.println(n * (5 * n + 3) / 2);
}
// Driver code
public static void main(String[] args)
{
int N = 4;
findNthTerm(N);
}
}
// This code is contributed by Ritik Bansal
Python 3
# Python implementation to
# find N-th term in the series
# Function to find N-th term
# in the series
def findNthTerm(n):
print(n * (5 * n + 3) // 2)
# Driver code
N = 4
# Function call
findNthTerm(N)
# This code is contributed by Vishal Maurya.
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 * (5 * n + 3) / 2);
}
// Driver code
public static void Main()
{
int N = 4;
findNthTerm(N);
}
}
// This code is contributed by Code_Mech
Javascript
输出:
46
时间复杂度: O(1)
辅助空间: O(1)
参考: OEIS