📜  居中非对角线数程序

📅  最后修改于: 2021-05-04 20:51:10             🧑  作者: Mango

中心非对角数是一个中心的图形数,它表示在一个连续的非对角层中以一个点为中心的非角点以及围绕该中心点的所有其他点。
n的中心非对角数为
中心非对角数=(3 * n – 2)*(3 * n – 1)/ 2;
https://zh.wikipedia.org/wiki/Centered_nonagonal_number
例子 :

Input : n = 10
Output : 406

Input : n = 8
Output : 253
C++
// CPP Program to find
// nth centered nonagonal number.
#include 
using namespace std;
 
// Function to find nth
// centered nonagonal number.
int centeredNonagonal(int n)
{
    // Formula to find nth centered
    // nonagonal number.
    return (3 * n - 2) * (3 * n - 1) / 2;
}
 
// Driver function.
int main()
{
    int n = 10;
    cout << centeredNonagonal(n);
    return 0;
}


Java
// Java Program to find
// nth centered nonagonal number.
import java.io.*;
 
class GFG {
     
    // Function to find nth
    // centered nonagonal number.
    static int centeredNonagonal(int n)
    {
        // Formula to find nth centered
        // nonagonal number.
        return (3 * n - 2) * (3 * n - 1) / 2;
    }
     
    // Driver function.
    public static void main(String args[])
    {
        int n = 10;
        System.out.println(centeredNonagonal(n));
    }
}
 
// This code is contributed by Nikita Tiwari.


Python3
# Python 3 Program to find
# nth centered nonagonal number.
 
# Function to find nth
# centered nonagonal number
def centeredNonagonal(n) :
     
    # Formula to find nth centered
    # nonagonal number.
    return (3 * n - 2) * (3 * n - 1) // 2
     
# Driver function.
n = 10
print(centeredNonagonal(n))
 
# This code is contributed
# by Nikita Tiwari.


C#
// C# Program to find nth
// centered nonagonal number.
using System;
 
class GFG {
     
    // Function to find nth
    // centered nonagonal number.
    static int centeredNonagonal(int n)
    {
        // Formula to find nth centered
        // nonagonal number.
        return (3 * n - 2) * (3 * n - 1) / 2;
    }
     
    // Driver function.
    public static void Main()
    {
        int n = 10;
        Console.Write(centeredNonagonal(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// CPP Program find first
// n centered nonagonal number.
#include 
using namespace std;
 
// Function to find centered
// nonagonal number series.
int centeredNonagonal(int n)
{
    for (int i = 1; i <= n; i++) {
        cout << (3 * i - 2) * (3 * i - 1) / 2;
        cout << " ";
    }
}
 
// Driver function.
int main()
{
    int n = 10;
    centeredNonagonal(n);
    return 0;
}


Java
// Java Program find first
// n centered nonagonal number.
import java.io.*;
 
class GFG {
     
    // Function to find centered
    // nonagonal number series.
    static void centeredNonagonal(int n)
    {
        for (int i = 1; i <= n; i++)
        {
            System.out.print((3 * i - 2) *
                             (3 * i - 1) / 2);
            System.out.print(" ");
        }
    }
 
    // Driver function.
    public static void main(String args[])
    {
        int n = 10;
        centeredNonagonal(n);
    }
}
 
// This code is contributed
// by Nikita Tiwari.


Python3
# Python3 Program find first
# n centered nonagonal number
 
# Function to find centered
# nonagonal number series
def centeredNonagonal(n) :
     
    for i in range(1, n + 1) :
        print( (3 * i - 2) * (3 * i - 1) // 2, end=" ")
         
 
# Driver function
n = 10
centeredNonagonal(n)
 
# This code is contributed by Nikita Tiwari


C#
// C# Program find first
// n centered nonagonal number.
using System;
 
class GFG {
     
    // Function to find centered
    // nonagonal number series.
    static void centeredNonagonal(int n)
    {
        for (int i = 1; i <= n; i++)
        {
            Console.Write((3 * i - 2) *
                            (3 * i - 1) / 2);
                             
            Console.Write(" ");
        }
    }
 
    // Driver function.
    public static void Main()
    {
        int n = 10;
        centeredNonagonal(n);
    }
}
 
// This code is contributed by
// by vt_m.


PHP


Javascript


输出:

406

给定一个数n,找到直到n的中心非对角数序列。
我们还可以找到居中的非对角数系列。居中非对角数系列包含居中非对角线上的点。
中心非对角线数:1,10,28,55,91,136,190,253,325,406,496,595,703,820,946。 。 。

C++

// CPP Program find first
// n centered nonagonal number.
#include 
using namespace std;
 
// Function to find centered
// nonagonal number series.
int centeredNonagonal(int n)
{
    for (int i = 1; i <= n; i++) {
        cout << (3 * i - 2) * (3 * i - 1) / 2;
        cout << " ";
    }
}
 
// Driver function.
int main()
{
    int n = 10;
    centeredNonagonal(n);
    return 0;
}

Java

// Java Program find first
// n centered nonagonal number.
import java.io.*;
 
class GFG {
     
    // Function to find centered
    // nonagonal number series.
    static void centeredNonagonal(int n)
    {
        for (int i = 1; i <= n; i++)
        {
            System.out.print((3 * i - 2) *
                             (3 * i - 1) / 2);
            System.out.print(" ");
        }
    }
 
    // Driver function.
    public static void main(String args[])
    {
        int n = 10;
        centeredNonagonal(n);
    }
}
 
// This code is contributed
// by Nikita Tiwari.

Python3

# Python3 Program find first
# n centered nonagonal number
 
# Function to find centered
# nonagonal number series
def centeredNonagonal(n) :
     
    for i in range(1, n + 1) :
        print( (3 * i - 2) * (3 * i - 1) // 2, end=" ")
         
 
# Driver function
n = 10
centeredNonagonal(n)
 
# This code is contributed by Nikita Tiwari

C#

// C# Program find first
// n centered nonagonal number.
using System;
 
class GFG {
     
    // Function to find centered
    // nonagonal number series.
    static void centeredNonagonal(int n)
    {
        for (int i = 1; i <= n; i++)
        {
            Console.Write((3 * i - 2) *
                            (3 * i - 1) / 2);
                             
            Console.Write(" ");
        }
    }
 
    // Driver function.
    public static void Main()
    {
        int n = 10;
        centeredNonagonal(n);
    }
}
 
// This code is contributed by
// by vt_m.

的PHP


Java脚本


输出 :

1  10  28  55  91  136  190  253  325  406