给定一个数 n,找出第n 个中心十二边形数。
中心十二边形数表示连续十二边形(12 边多边形)层中中心的一个点和围绕它的其他点。
例子 :
Input : 3
Output : 37
Input : 7
Output :253
前几个居中的十二边形数是:
1, 13, 37, 73, 121, 181, 253, 337, 433, 541, 661……………………..
第 n 个中心十二边形数的公式:
C++
// C++ Program to find
// nth centered
// Dodecagonal number
#include
using namespace std;
// Function to calculate Centered
// Dodecagonal number
int centeredDodecagonal(long int n)
{
// Formula to calculate nth
// centered Dodecagonal number
return 6 * n * (n - 1) + 1;
}
// Driver Code
int main()
{
long int n = 2;
cout << centeredDodecagonal(n);
cout << endl;
n = 9;
cout << centeredDodecagonal(n);
return 0;
}
Java
// Java Program to find nth
// centered dodecagonal number
import java.io.*;
class GFG{
// Function to calculate
// centered dodecagonal number
static long centeredDodecagonal(long n)
{
// Formula to calculate nth
// centered dodecagonal number
return 6 * n * (n - 1) + 1;
}
// Driver Code
public static void main(String[] args)
{
long n = 2;
System.out.println(centeredDodecagonal(n));
n = 9;
System.out.println(centeredDodecagonal(n));
}
}
// This code is contributed by anuj_67
Python3
# Python3 program to find nth
# centered dodecagonal number
# Function to calculate
# centered dodecagonal number
def centeredDodecagonal(n) :
# Formula to calculate nth
# centered dodecagonal number
return 6 * n * (n - 1) + 1;
# Driver code
n = 2
print(centeredDodecagonal(n));
n = 9
print(centeredDodecagonal(n));
# This code is contributed by grand_master
C#
// C# Program to find nth
// centered dodecagonal number
using System;
class GFG{
// Function to calculate
// centered dodecagonal number
static long centeredDodecagonal(long n)
{
// Formula to calculate nth
// centered dodecagonal number
return 6 * n * (n - 1) + 1;
}
// Driver Code
public static void Main(String[] args)
{
long n = 2;
Console.WriteLine(centeredDodecagonal(n));
n = 9;
Console.WriteLine(centeredDodecagonal(n));
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出 :
13
433
时间复杂度: O(1)
辅助空间: O(1)
参考
http://oeis.org/A003154
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。