给定一个数 n,找出第n 个中心化的十九边形数。
一个居中的十九边形数表示连续的十九边形(19 边多边形)层中中心的一个点和围绕它的其他点。
前几个居中的十九边形数是:
1、20、58、115、191、286、400、533、685、856、1046、1255…………………………
例子 :
Input : 3
Output : 58
Input : 13
Output :1483
在数学中,第 n项的中心十九边形数由下式给出:
下面是上述想法的基本实现:
C++
// C++ Program to find
// nth centered
// nonadecagonal number
#include
using namespace std;
// centered nonadecagonal
// function
int center_nonadecagon_num( long int n )
{
// Formula to calculate nth
// centered nonadecagonal number
return (19 * n * n - 19 * n + 2) / 2;
}
// Driver Code
int main()
{
long int n = 2;
cout << n << "th centered nonadecagonal number : "
<< center_nonadecagon_num(n);
cout << endl;
n = 7;
cout << n << "th centered nonadecagonal number : "
<< center_nonadecagon_num(n);
return 0;
}
Java
// Java Program to find nth centered
// nonadecagonal number
import java.io.*;
class GFG {
// centered nonadecagonal
// function
static int center_nonadecagon_num(int n)
{
// Formula to calculate nth
// centered nonadecagonal number
return (19 * n * n - 19 * n + 2) / 2;
}
// Driver code
public static void main (String[] args)
{
int n = 2;
System.out.print ( n + "th centered "
+ "nonadecagonal number : ");
System.out.println (
center_nonadecagon_num(n));
n = 7;
System.out.print ( n + "th centered "
+ "nonadecagonal number : ");
System.out.println(
center_nonadecagon_num(n));
}
}
// This code is contributed by m_kit
Python3
# Program to find nth
# centered nonadecagonal number
def center_nonadecagon_num(n) :
# Formula to calculate
# nth centered nonadecagonal
# number & return it into
# main function.
return (19 * n * n -
19 * n + 2) // 2
# Driver Code
if __name__ == '__main__' :
n = 2
print(n,"nd centered nonadecagonal " +
"number : ",
center_nonadecagon_num(n))
n = 7
print(n,"nd centered nonadecagonal " +
"number : ",
center_nonadecagon_num(n))
# This code is contributed by ajit
C#
// C# Program to find
// nth centered
// nonadecagonal number
using System;
class GFG
{
// centered nonadecagonal
// function
static int center_nonadecagon_num(int n)
{
// Formula to calculate nth
// centered nonadecagonal number
return (19 * n * n -
19 * n + 2) / 2;
}
// Driver code
static public void Main ()
{
int n = 2;
Console.Write ( n + "th centered " +
"nonadecagonal number : ");
Console.WriteLine(
center_nonadecagon_num(n));
n = 7;
Console.Write( n + "th centered " +
"nonadecagonal number : ");
Console.WriteLine(
center_nonadecagon_num(n));
}
}
// This code is contributed by ajit
PHP
Javascript
输出 :
2th centered nonadecagonal number : 20
7th centered nonadecagonal numbe : 400
时间复杂度: O(1)
辅助空间: O(1)
参考:
http://oeis.org/A069132
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。