📜  居中的八边形数

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

给定数字n,找到第n个居中的八边形数字。
居中的八边形数字表示中心的一个点,其他点在连续的八边形(18面多边形)层中围绕它排列。

center_octadecagon_num

例子 :

Input :  2
Output : 19

Input : 6
Output : 271

在数学中,第n个项的居中八角形数由下式给出:

CO_{n}= 9n^2 -9n+1

以下是上述想法的基本实现:

C++
// C++ Program to find the
// nth centered octadecagonal
// number
#include 
using namespace std;
 
// centered octadecagon  function
int center_octadecagon_num(long int n)
{
    // Formula to calculate nth
    // centered octadecagonal number
    return 9 * n * n - 9 * n + 1;
}
 
// Driver Code
int main()
{
    long int n = 3;
    cout << n << "th centered octadecagonal number : "
                  << center_octadecagon_num(n);
    cout << endl;
    n = 13;
    cout << n << "th centered octadecagonal number : "
                 << center_octadecagon_num(n);
 
    return 0;
}


Java
// Java Program to find the
// nth centered octadecagonal
// number
import java.io.*;
 
class GFG
{
     
// centered octadecagon function
static int center_octadecagon_num(int n)
{
         
    // Formula to calculate nth
    // centered octadecagonal number
    return 9 * n * n - 9 * n + 1;
}
 
// Driver Code
public static void main (String[] args)
{
int n = 3;
System.out.print(n + "th centered " +
           "octadecagonal number : ");
System.out.println(center_octadecagon_num(n));
 
n = 13;
System.out.print(n + "th centered " +
           "octadecagonal number : ");
System.out.println(center_octadecagon_num(n));
}
}
 
// This code is contributed by ajit


Python3
# Program to find nth
# centered octadecagonal number
 
# Centered octadecagonal
# number function
def center_octadecagon_num(n) :
     
    # Formula to calculate
    # nth centered octadecagonal
    # number & return it
    # into main function.
    return(9 * n * n -
           9 * n + 1)
 
# Driver Code
if __name__ == '__main__' :
         
    n = 3
    print(n,"rd centered octadecagonal " +
                                "number : ",
                center_octadecagon_num(n))
 
    n = 13
    print(n,"th centered octadecagonal " +
                              "number : ",
                center_octadecagon_num(n))
             
# This code is contributed
# by akt_mit


C#
// C# Program to find the
// nth centered octadecagonal
// number
using System;
 
class GFG
{
     
// centered octadecagon function
static int center_octadecagon_num(int n)
{
         
    // Formula to calculate nth
    // centered octadecagonal number
    return 9 * n * n - 9 * n + 1;
}
 
// Driver Code
static public void Main ()
{
     
    int n = 3;
    Console.Write( n + "th centered " +
            "octadecagonal number : ");
    Console.WriteLine( center_octadecagon_num(n));
 
    n = 13;
    Console.Write( n + "th centered " +
            "octadecagonal number : ");
    Console.WriteLine(center_octadecagon_num(n));
}
}
 
// This code is contributed by aj_36.


PHP


Javascript


输出 :

3th centered octadecagonal number : 55
13th centered octadecagonal numbe : 1405