给定一个数 n,找出第n 个中心五边形数。
中心五边形数表示连续的五边形(15 边多边形)层中中心的一个点和围绕它的其他点。
例子 :
Input : 2
Output : 16
Input : 8
Output : 421
中心五边形数的第 n项:-
下面是上述想法的基本实现。
C++
// C++ Program to find
// nth centered
// pentadecagonal number
#include
using namespace std;
// centered pentadecagonal function
int center_pentadecagonal_num(long int n)
{
// Formula to calculate nth
// centered pentadecagonal number
return (15 * n * n - 15 * n + 2) / 2;
}
// Driver Code
int main()
{
long int n = 3;
cout << n << "th number : "
<< center_pentadecagonal_num(n);
cout << endl;
n = 10;
cout << n << "th number : "
<< center_pentadecagonal_num(n);
return 0;
}
Java
// Java Program to find nth centered
// pentadecagonal number
import java.io.*;
class GFG {
// centered pentadecagonal function
static long center_pentadecagonal_num(long n)
{
// Formula to calculate nth
// centered pentadecagonal number
return (15 * n * n - 15 * n + 2) / 2;
}
// Driver Code
public static void main (String[] args)
{
long n = 3;
System.out.print(n + "th number : ");
System.out.println(
center_pentadecagonal_num(n));
n = 10;
System.out.print( n + "th number : ");
System.out.println(
center_pentadecagonal_num(n));
}
}
// This code is contributed by ajit.
Python3
# Program to find nth
#centered pentadecagonal number
def center_pentadecagonal_num(n) :
# Formula to calculate nth
# centered pentadecagonal number
return (15 * n * n - 15 * n + 2) // 2
# Driver Code
if __name__ == '__main__' :
n = 3
print(n,"rd number : ",
center_pentadecagonal_num(n))
n = 10
print(n,"th number : ",
center_pentadecagonal_num(n))
# This code is contributed by m_kit
C#
// C# Program to find
// nth centered
// pentadecagonal number
using System;
class GFG
{
// centered
// pentadecagonal function
static long center_pentadecagonal_num(long n)
{
// Formula to calculate
// nth centered
// pentadecagonal number
return (15 * n * n -
15 * n + 2) / 2;
}
// Driver Code
static public void Main ()
{
long n = 3;
Console.Write(n + "th number : ");
Console.WriteLine(
center_pentadecagonal_num(n));
n = 10;
Console.Write( n + "th number : ");
Console.WriteLine(
center_pentadecagonal_num(n));
}
}
// This code is contributed by ajit.
PHP
Javascript
输出 :
3th number : 46
10th number : 676
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。