📜  中心七边形数

📅  最后修改于: 2021-05-04 21:15:48             🧑  作者: Mango

给定数字n ,任务是找到n中心七边形数字。
居中的七边形数字是居中的图形编号,它代表一个以点为中心的七边形,而所有其他点都以七边形的形式包围。可以使用公式(7n 2 – 7n + 2)/ 2计算N中心的七边形数。
例子 :

Input : n = 2
Output : 8

Input : n = 7
Output : 148

请参考该图的图形表示。

下面是实现:

C++
// CPP program to find n-th
// Centered heptagonal number
#include 
 
using namespace std;
 
// Function to find Centered
// heptagonal number
int centered_heptagonal_num(long int n)
{
    // Formula to calculate nth
    // Centered heptagonal number
    return (7 * n * n - 7 * n + 2) / 2;
}
 
// Driver Code
int main()
{
    long int n = 5;
    cout << n << "th Centered heptagonal number : ";
    cout << centered_heptagonal_num(n);
    return 0;
}


Java
// Java program to find n-th Centered
// heptagonal number
import java.io.*;
 
class GFG {
 
    // Function to find Centered heptagonal
    // number
    static long centered_heptagonal_num(long n)
    {
         
        // Formula to calculate nth
        // Centered heptagonal number
        return (7 * n * n - 7 * n + 2) / 2;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        long n = 5;
        System.out.println( n + "th Centered "
                      + "heptagonal number : "
                + centered_heptagonal_num(n));
    }
}
 
// This code is contributed by anuj_67.


Python3
# Python program to find nth
# Centered heptagonal number
 
# Function to find Centered
# heptagonal number
def centered_heptagonal_num(n):
 
    # Formula to calculate nth
    # Centered heptagonal number
    return (7 * n * n - 7 * n + 2) // 2
 
 
# Driver Code
n = 5
print("%sth Centered heptagonal number : " %n,
                    centered_heptagonal_num(n))


C#
//C# program to find n-th Centered
// heptagonal number
using System;
 
class GFG {
 
    // Function to find Centered heptagonal
    // number
    static long centered_heptagonal_num(long n)
    {
         
        // Formula to calculate nth
        // Centered heptagonal number
        return (7 * n * n - 7 * n + 2) / 2;
    }
     
    // Driver Code
    public static void Main ()
    {
        long n = 5;
        Console.WriteLine( n + "th Centered "
                    + "heptagonal number : "
                + centered_heptagonal_num(n));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出 :

5th Centered heptagonal number : 71