给定数字n,任务是找到第n个十六进制数字。
六边形数是类比数和一个完美的正方形。它有16个面的多边形,称为六边形或六边形。第n个十六进制数计数是点的十六个数,所有其他点都围绕其连续层。
例子 :
Input : 2
Output :16
Input :7
Output :301
计算十六进制数的公式:
C++
// C++ program to find Nth
// hexadecagon number
#include
using namespace std;
// Function to calculate hexadecagonal number
int hexadecagonalNum(long int n)
{
return ((14 * n * n) - 12 * n) / 2;
}
// Drivers Code
int main()
{
long int n = 5;
cout << n << "th Hexadecagonal number : ";
cout << hexadecagonalNum(n);
cout << endl;
n = 9;
cout << n << "th Hexadecagonal number : ";
cout << hexadecagonalNum(n);
return 0;
}
Java
// Java program to find Nth hexadecagon
// number
import java.io.*;
class GFG {
// Function to calculate hexadecagonal
// number
static long hexadecagonalNum(long n)
{
return ((14 * n * n) - 12 * n) / 2;
}
// Drivers Code
public static void main (String[] args)
{
long n = 5;
System.out.println( n + "th "
+ "Hexadecagonal number : "
+ hexadecagonalNum(n));
n = 9;
System.out.println( n + "th "
+ "Hexadecagonal number : "
+ hexadecagonalNum(n));
}
}
// This code contribued by anuj_67.
Python3
# Python program to find Nth
# hexadecagon number
# Function to calculate
# hexadecagonal number
def hexadecagonalNum(n):
# Formula to calculate nth
# Centered hexadecagonal number
return ((14 * n * n) - 12 * n) // 2
# Driver Code
n = 5
print("%sth Hexadecagonal number : " %n,
hexadecagonalNum(n))
n = 9
print("%sth Hexadecagonal number : " %n,
hexadecagonalNum(n))
# This code is contributed by ajit
C#
// C# program to find Nth hexadecagon
// number
using System;
class GFG {
// Function to calculate hexadecagonal
// number
static long hexadecagonalNum(long n)
{
return ((14 * n * n) - 12 * n) / 2;
}
// Drivers Code
public static void Main ()
{
long n = 5;
Console.WriteLine( n + "th "
+ "Hexadecagonal number : "
+ hexadecagonalNum(n));
n = 9;
Console.WriteLine( n + "th "
+ "Hexadecagonal number : "
+ hexadecagonalNum(n));
}
}
// This code contribued by anuj_67.
PHP
Javascript
输出 :
5th Hexadecagonal number : 145
9th Hexadecagonal number : 513
参考:https://en.wikipedia.org/wiki/Polygonal_number