给定数字n,任务是找到第n个十进制数。
十边形数字是将三角形和正方形数字的概念扩展到十边形(十一面多边形)的图形化数字。第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
输出 :
3th Hendecagonal number: 30
10th Hendecagonal number: 415
参考:https://en.wikipedia.org/wiki/Polygonal_number