给定一个数 n,任务是找到第n 个中心十三边形数。
一个居中的十三边形数字表示中心的一个点和中心点周围的其他点
在连续的十三边形(13 边多边形)层中。
例子 :
Input : 2
Output : 14
Input : 9
Output : 469
第 n 个中心十三边形数的公式:
C++
// C++ Program to find nth
// centered tridecagonal number
#include
using namespace std;
// Function to find nth centered
// tridecagonal number
int centeredTridecagonalNum(long int n)
{
// Formula to calculate nth
// centered tridecagonal number
return (13 * n * (n - 1) + 2) / 2;
}
// Drivers code
int main()
{
long int n = 3;
cout << centeredTridecagonalNum(n);
cout << endl;
n = 10;
cout << centeredTridecagonalNum(n);
return 0;
}
Java
// Java Program to find nth
// centered tridecagonal number
import java.io.*;
class GFG
{
// Function to find nth centered
// tridecagonal number
static long centeredTridecagonalNum(long n)
{
// Formula to calculate nth
// centered tridecagonal number
return (13 * n * (n - 1) + 2) / 2;
}
// Driver Code
public static void main (String[] args)
{
long n = 3;
System.out.println(centeredTridecagonalNum(n));
n = 10;
System.out.println(centeredTridecagonalNum(n));
}
}
// This code is contributed by anuj_67.
Python3
# Program to find nth centered
# tridecagonal number
# Function to find centered
# tridecagonal number
def centeredTridecagonalNum(n) :
# Formula to calculate nth
# centered tridecagonal number
return (13 * n *
(n - 1) + 2) // 2
# Driver Code
if __name__ == '__main__' :
n = 3
print(centeredTridecagonalNum(n))
n = 10
print(centeredTridecagonalNum(n))
# This code is contributed
# by akt_mit
C#
// C# Program to find nth
// centered tridecagonal number
using System;
class GFG
{
// Function to find nth centered
// tridecagonal number
static long centeredTridecagonalNum(long n)
{
// Formula to calculate nth
// centered tridecagonal number
return (13 * n * (n - 1) + 2) / 2;
}
// Driver Code
public static void Main ()
{
long n = 3;
Console.WriteLine(centeredTridecagonalNum(n));
n = 10;
Console.WriteLine(centeredTridecagonalNum(n));
}
}
// This code is contributed by anuj_67.
PHP
Javascript
输出 :
40
586
时间复杂度: O(1)
辅助空间: O(1)
参考:http://oeis.org/wiki/Figurate_numbers
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。