📜  非对角数

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

非对角数是将三角形和平方数的概念扩展到对角的图形化数字。具体而言,第n个非对角数计算n个嵌套非对角线(9个边多边形)的图案中的点数,它们全部共享一个公共角,其中该模式中的第i个非对角线具有由i个点组成的边,这些点由彼此间隔一个单位的点组成。
例子 :

Input : n = 10
       n (7n - 5) / 2
       10 * (7 * 10 - 5) / 2
       10 * 65 / 2 = 325
Output :325

Input : 15
Output :750

n非对角数由以下公式给出: n(7n – 5)/ 2

C++
// CPP Program to find
// nth nonagonal number.
#include 
using namespace std;
 
// Function to find nth
// nonagonal number.
int Nonagonal(int n)
{
    // Formula to find nth
    // nonagonal number.
    return n * (7 * n - 5) / 2;
}
 
// Driver function.
int main()
{
    int n = 10;   
    cout << Nonagonal(n);   
    return 0;
}


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


Python3
# Python 3 Program to find
# nth nonagonal number.
 
# Function to find nth
# nonagonal number.
def Nonagonal(n):
  
    # Formula to find nth
    # nonagonal number.
    return int(n * (7 * n - 5) / 2)
  
n = 10 
print(Nonagonal(n))
 
# This code is contributed bt
# Smitha Dinesh Semwal


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


PHP


Javascript


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


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


Python3
# Python 3 Program find first
# n nonagonal number.
 
# Function to find nonagonal
# number series.
def Nonagonal(n):
  
    for i in range(1, n+1): 
        print(int(i * (7 * i - 5) / 2),end=" ")
     
# driver Function    
n = 10
Nonagonal(n)
     
# This code is contributed by
# Smitha Dinesh Semwal


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


PHP


Javascript


输出 :

325

时间复杂度: O(1)

辅助空间: O(1)
给定一个数n,找到直到n个项的非对角数序列

C++

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

Java

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

Python3

# Python 3 Program find first
# n nonagonal number.
 
# Function to find nonagonal
# number series.
def Nonagonal(n):
  
    for i in range(1, n+1): 
        print(int(i * (7 * i - 5) / 2),end=" ")
     
# driver Function    
n = 10
Nonagonal(n)
     
# This code is contributed by
# Smitha Dinesh Semwal

C#

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

的PHP


Java脚本


输出 :

1 9 24 46 75 111 154 204 261 325 

时间复杂度: O(n)

辅助空间: O(1)
参考: https : //en.wikipedia.org/wiki/Nonagonal_number