给定一个数 n,任务是找到第 n 个 Hendecagonal 数。
Hendecagonal number 是一个图形数字,它将三角形和正方形数字的概念扩展到十边形(11 边多边形)。第 n 个十边形数计算 n 个嵌套十边形图案中的点数,所有十边形都共享一个公共角,其中图案中的第 i 个十边形的边由 i 个点组成,彼此间隔一个单位。
例子:
Input : 2
Output :11
Input :6
Output :141
第 n 个十边形数的公式:
C++
// C++ program to find nth
// Hendecagonal number
#include
using namespace std;
// Function to find
// Hendecagonal number
int hendecagonal_num(int n)
{
// Formula to calculate nth
// Hendecagonal number
return (9 * n * n - 7 * n) / 2;
}
// Driver Code
int main()
{
int n = 3;
cout << n << "rd Hendecagonal number: ";
cout << hendecagonal_num(n);
cout << endl;
n = 10;
cout << n << "th Hendecagonal number: ";
cout << hendecagonal_num(n);
return 0;
}
Java
// Java program to find nth
// Hendecagonal number
import java.io.*;
class GFG
{
// Function to find
// Hendecagonal number
static int hendecagonal_num(int n)
{
// Formula to calculate nth
// Hendecagonal number
return (9 * n * n -
7 * n) / 2;
}
// Driver Code
public static void main (String[] args)
{
int n = 3;
System.out.print(n + "rd Hendecagonal " +
"number: ");
System.out.println(hendecagonal_num(n));
n = 10;
System.out.print(n + "th Hendecagonal " +
"number: ");
System.out.println(hendecagonal_num(n));
}
}
// This code is contributed by ajit
Python3
# Program to find nth
# Hendecagonal number
# Function of Hendecagonal
# number
def hendecagonal_num(n) :
# Formula to calculate nth
# Hendecagonal number &
# return it into main function.
return (9 * n * n -
7 * n) // 2
# Driver Code
if __name__ == '__main__' :
n = 3
print(n,"rd Hendecagonal number : " ,
hendecagonal_num(n))
n = 10
print(n,"th Hendecagonal number : " ,
hendecagonal_num(n))
# This code is contributed by ajit
C#
// C# program to find nth
// Hendecagonal number
using System;
class GFG
{
// Function to find
// Hendecagonal number
static int hendecagonal_num(int n)
{
// Formula to calculate nth
// Hendecagonal number
return (9 * n * n - 7 * n) / 2;
}
// Driver Code
static public void Main ()
{
int n = 3;
Console.Write(n +
"rd Hendecagonal number: ");
Console.WriteLine( hendecagonal_num(n));
n = 10;
Console.Write(n +
"th Hendecagonal number: ");
Console.WriteLine( hendecagonal_num(n));
}
}
// This code is contributed by aj_36
PHP
Javascript
输出 :
3th Hendecagonal number: 30
10th Hendecagonal number: 415
时间复杂度: O(1)
辅助空间: O(1)
参考:https://en.wikipedia.org/wiki/Polygonal_number