给定一个数字n,我们需要找到第n个居中的二十面体数。
说明:居中的二十面体编号是代表二十面体的居中图形编号。
居中的二十面体数字序列的前几个是:
1,13,55,147,309,561,923,1415,2057,2869,3871,5083,6525,8217………………。
第n个中心二十面体数的数学公式:
例子 :
Input : n = 4
Output : 309
Input : n = 12
Output : 6525
下面是上面公式的实现
C++
// C++ Program to find nth
// Centered icosahedral number
#include
using namespace std;
// Function to find
// Centered icosahedral number
int centeredIcosahedralNum(int n)
{
// Formula to calculate nth
// Centered icosahedral number
// and return it into main function.
return (2 * n + 1) * (5 * n * n + 5 * n + 3) / 3;
}
// Driver Code
int main()
{
int n = 10;
cout << centeredIcosahedralNum(n) << endl;
n = 12;
cout << centeredIcosahedralNum(n) << endl;
return 0;
}
Java
// Java Program to find nth
// Centered icosahedral number
// Java Program to find nth Centered
// icosahedral number
import java.io.*;
class GFG {
// Function to find Centered
// icosahedral number
static int centeredIcosahedralNum(int n)
{
// Formula to calculate nth Centered
// icosahedral number and return it
// into main function.
return (2 * n + 1) * (5 * n * n +
5 * n + 3) / 3;
}
// Driver Code
public static void main (String[] args)
{
int n = 10;
System.out.println(
centeredIcosahedralNum(n));
n = 12;
System.out.println(
centeredIcosahedralNum(n));
}
}
// This code is contributed by anuj_67.
Python3
# Python program to find nth
# Centered icosahedral number
# Function to calculate
# Centered icosahedral number
def centeredIcosahedralNum(n):
# Formula to calculate nth
# Centered icosahedral number
return ((2 * n + 1) *
(5 * n * n + 5 * n + 3) // 3)
# Driver Code
n = 10
print(centeredIcosahedralNum(n))
n = 12
print(centeredIcosahedralNum(n))
# This code is contributed by ajit.
C#
// C# Program to find nth
// Centered icosahedral number
// Java Program to find nth Centered
// icosahedral number
using System;
class GFG {
// Function to find Centered
// icosahedral number
static int centeredIcosahedralNum(int n)
{
// Formula to calculate nth Centered
// icosahedral number and return it
// into main function.
return (2 * n + 1) * (5 * n * n +
5 * n + 3) / 3;
}
// Driver Code
public static void Main ()
{
int n = 10;
Console.WriteLine(
centeredIcosahedralNum(n));
n = 12;
Console.WriteLine(
centeredIcosahedralNum(n));
}
}
// This code is contributed by anuj_67.
PHP
Javascript
输出 :
3871
6525
参考:
https://zh.wikipedia.org/wiki/Centered_icosahedral_number