📜  中心五角形数

📅  最后修改于: 2021-05-04 11:38:06             🧑  作者: Mango

给定整数n,找到第n个中心五角形数。
中心五角形数字是一个中心的图形数字,代表一个五角形,中间有一个点,而在五角形层中依次环绕着其他点。[来源:Wiki]

中心五角形数

很少有居中的五边形数是:
1,6,16,31,51,76,106,141,181,226,276,331,391 ……………
例子 :

Input : 3
Output : 16

Input : 9
Output : 181

方法:
第n个项的中心五边形由下式给出:

Cp_{n}=(5n^2-5n+2)/2

以上方法基本实现:

C++
// Program to find nth
// Centered pentagonal number.
#include 
using namespace std;
 
// centered pentagonal number function
 
int centered_pentagonal_Num(int n)
{
    // Formula to calculate nth
    // Centered pentagonal number
    // and return it into main function.
 
    return (5 * n * n - 5 * n + 2) / 2;
}
 
// Driver Code
int main()
{
    int n = 7;
    cout << n << "th Centered pentagonal number: ";
    cout << centered_pentagonal_Num(n);
    return 0;
}


Java
// Program to find nth
// Centered pentagonal number
import java.io.*;
 
class GFG
{
     
// centered pentagonal
// number function
static int centered_pentagonal_Num(int n)
{
    // Formula to calculate
    // nth Centered pentagonal
    // number and return it
    // into main function.
 
    return (5 * n * n - 5 * n + 2) / 2;
}
 
// Driver Code
public static void main (String[] args)
{
int n = 7;
System.out.print(n + "th Centered " +
              "pentagonal number: ");
System.out.println(centered_pentagonal_Num(n));
}
}
 
// This code is contributed by anuj_67.


Python3
# Python program to find Nth
# Centered pentagonal number.
 
# Function to calculate
# Centered pentagonal number.
 
def centered_pentagonal_Num(n):
 
    # Formula to calculate nth
    # Centered pentagonal number.
     
    return (5 * n * n - 5 * n + 2) // 2
 
# Driver Code
n = 7
print("%sth Centered pentagonal number : " %n,
                    centered_pentagonal_Num(n))
                     
# This code is contributed by ajit


C#
// C# Program to find nth
// Centered pentagonal number
using System;
 
class GFG
{
     
// centered pentagonal
// number function
static int centered_pentagonal_Num(int n)
{
    // Formula to calculate
    // nth Centered pentagonal
    // number and return it
    // into main function.
 
    return (5 * n * n - 5 * n + 2) / 2;
}
 
// Driver Code
public static void Main ()
{
int n = 7;
Console.Write(n + "th Centered " +
           "pentagonal number: ");
Console.WriteLine(centered_pentagonal_Num(n));
}
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出 :

7th Centered pentagonal number: 106