给定一个数 n,找出第n 个中心十二面体数。
中心十二面体数是具象数的一类。它由一个中心点组成,周围是连续的十二面体(具有 12 个平面的多面体)层。
前几个中心十二面体数(其中 n = 0、1、2、3……)是:
1, 33, 155, 427, 909, 1661 ……………
例子:
Input : 5
Output : 1661
Input :1
Output :33
第 n 个中心十二面体数的数学公式由下式给出:
下面是上述想法的基本实现:
C++
// Program to find nth centered
// dodecahedral number
#include
using namespace std;
// Function to find
// centered dodecahedral number
int CenteredDodecahedral_num(long int n)
{
// Formula to calculate nth
// centered dodecahedral number
// and return it into main function.
return (2 * n + 1) * (5 * n * n + 5 * n + 1);
}
// Driver Code
int main()
{
long int n = 3;
// print result
cout << n << "th Centered Dodecahedral number : ";
cout << CenteredDodecahedral_num(n) << endl;
n = 10;
// print result
cout << n << "th Centered Dodecahedral number : ";
cout << CenteredDodecahedral_num(n);
return 0;
}
Java
// Java Program to find nth
// centered dodecahedral number
import java.io.*;
class GFG {
// Function to find centered
// dodecahedral number
static int CenteredDodecahedral_num(int n)
{
// Formula to calculate nth
// centered dodecahedral number
// and return it into main function.
return (2 * n + 1) *
(5 * n * n + 5 * n + 1);
}
// Driver Code
public static void main (String[] args)
{
int n = 3;
// print result
System.out.print( n + "th Centered "
+ "Dodecahedral number : ");
System.out.println (
CenteredDodecahedral_num(n));
n = 10;
// print result
System.out.print( n + "th Centered "
+ "Dodecahedral number : ");
System.out.println(
CenteredDodecahedral_num(n));
}
}
// This code is contributed by m_kit.
Python3
# Program to find nth centered
# dodecahedral number
# Function to find centered
# dodecahedral number
def CenteredDodecahedral_num(n) :
# Formula to calculate nth
# centered dodecahedral number
return (2 * n + 1) * (5 * n * n + 5 * n + 1)
# Driver Code
if __name__ == '__main__' :
n = 3
print(n,"rd centered dodecahedral number: ",
CenteredDodecahedral_num(n))
n = 10
print(n,"th centered dodecahedral number : ",
CenteredDodecahedral_num(n))
# This code is contributed by aj_36
C#
// C# Program to find
// nth centered
// dodecahedral number
using System;
class GFG
{
// Function to find
// nth centered
// dodecahedral number
static int CenteredDodecahedral_num(int n)
{
// Formula to calculate
// nth centered dodecahedral
// number and return it
// into main function.
return (2 * n + 1) *
(5 * n * n +
5 * n + 1);
}
// Driver Code
static public void Main ()
{
int n = 3;
// print result
Console.Write( n + "th Centered " +
"Dodecahedral number : ");
Console.WriteLine(
CenteredDodecahedral_num(n));
n = 10;
// print result
Console.Write( n + "th Centered " +
"Dodecahedral number : ");
Console.WriteLine(
CenteredDodecahedral_num(n));
}
}
// This code is contributed by ajit
PHP
Javascript
输出 :
3th Centered Dodecahedral number : 427
10th Centered Dodecahedral number : 11571
时间复杂度: O(1)
辅助空间: O(1)
参考:
https://en.wikipedia.org/wiki/Centered_dodecahedral_number
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。