📜  中心六边形数

📅  最后修改于: 2021-04-30 03:35:36             🧑  作者: Mango

给定一个数N ,任务是找到N居中的六角形数。此外,找到居中的六角形系列。
例子:

中心六边形数字–中心六边形数字是虚拟数字,形式为六边形。中心六角数与六角数不同,因为它在中心包含一个元素。
一些中央六边形数字是–

1, 7, 19, 37, 61, 91, 127, 169 ... 

例如:

The First N numbers are - 
1, 7, 19, 37, 61, 91, 127 ...

The cumulative sum of these numbers are - 
1, 1+7, 1+7+19, 1+7+19+37...

which is nothing but the sequence -
1, 8, 27, 64, 125, 216 ...

That is in the form of  -
13, 23, 33, 43, 53, 63 ....

由于中心六边形数之和等于N项,因此N 3将为N 3 。那是 –

方法:要找到中心六边形数的N项,请使用公式– 3 * N *(N – 1)+1
下面是上述方法的实现:

C++
// Program to find nth
// centered hexadecimal number.
#include 
using namespace std;
 
// Function to find centered
// hexadecimal number.
int centeredHexagonalNumber(int n)
{
    // Formula to calculate nth
    // centered hexadecimal number
    // and return it into main function.
    return 3 * n * (n - 1) + 1;
}
 
// Driver Code
int main()
{
    int n = 10;
    cout << n << "th centered hexagonal number: ";
    cout << centeredHexagonalNumber(n);
    return 0;
}


Java
// Java Program to find nth
// centered hexadecimal number
import java.io.*;
 
class GFG
{
     
    // Function to find centered
    // hexadecimal number
    static int centeredHexagonalNumber(int n)
    {
        // Formula to calculate nth
        // centered hexadecimal number
        // and return it into main function
        return 3 * n * (n - 1) + 1;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int n = 10;
        System.out.print(n + "th centered " +
                       "hexagonal number: ");
        System.out.println(centeredHexagonalNumber(n));
         
    }
}
 
// This code is contributed by Nikita Tiwari.


Python3
# Python 3 program to find nth
# centered hexagonal number
 
# Function to find
# centered hexagonal number
def centeredHexagonalNumber(n) :
     
    # Formula to calculate
    # nth centered hexagonal
    return 3 * n * (n - 1) + 1
 
 
# Driver Code
if __name__ == '__main__' :
         
    n = 10
    print(n, "th centered hexagonal number: "
                , centeredHexagonalNumber(n))
 
 
# This code is contributed
# by 'Akanshgupta'


C#
// C# Program to find nth
// centered hexadecimal number
using System;
 
class GFG
{
     
    // Function to find centered
    // hexadecimal number
    static int centeredHexagonalNumber(int n)
    {
        // Formula to calculate nth
        // centered hexadecimal number
        // and return it into main function
        return 3 * n * (n - 1) + 1;
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 10;
        Console.Write(n + "th centered "+
                   "hexagonal number: ");
        Console.Write(centeredHexagonalNumber(n));
         
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// Program to find the series
// of centered hexadecimal number
#include 
using namespace std;
 
// Function to find the
// series of centered
// hexadecimal number.
void centeredHexagonalSeries(int n)
{
    // Formula to calculate
    // nth centered hexadecimal
    // number.
    for (int i = 1; i <= n; i++)
        cout << 3 * i * (i - 1) + 1
             << " ";
}
 
// Driver Code
int main()
{
    int n = 10;
    centeredHexagonalSeries(n);
    return 0;
}


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


Python3
# Python3 program to find
# nth centered hexagonal number
 
# Function to find centered hexagonal
# series till n given numbers.
def centeredHexagonalSeries(n) :
    for i in range(1, n + 1) :
         
        # Formula to calculate nth
        # centered hexagonal series.
        print(3 * i * (i - 1) + 1, end=" ")
         
# Driver Code
if __name__ == '__main__' :
     
    n = 10
    centeredHexagonalSeries(n)
 
# This code is contributed
# by 'Akanshgupta'


C#
// C# Program to find the
// series of centered
// hexadecimal number.
using System;
 
class GFG
{
     
    // Function to find the
    // series of centered
    // hexadecimal number.
    static void centeredHexagonalSeries(int n)
    {
        // Formula to calculate nth
        // centered hexadecimal number.
        for (int i = 1; i <= n; i++)
            Console.Write( 3 * i *
                         (i - 1) + 1 + " ");
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 10;
        centeredHexagonalSeries(n);
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

10th centered hexagonal number: 271

性能分析:

  • 时间复杂度:在上述给定方法中,我们发现中心六边形数的N项需要固定的时间。因此,复杂度将为O(1)
  • 空间复杂度:在上述给定方法中,我们没有使用任何其他辅助空间进行计算。因此,空间复杂度将为O(1)

中心六角系列

给定数字N,任务是找到直到N的中心六边形级数。
方法:
使用循环变量(例如i )迭代循环,并使用公式– 3 * i *(i – 1)+ 1查找中心六边形的i
下面是上述方法的实现:

C++

// Program to find the series
// of centered hexadecimal number
#include 
using namespace std;
 
// Function to find the
// series of centered
// hexadecimal number.
void centeredHexagonalSeries(int n)
{
    // Formula to calculate
    // nth centered hexadecimal
    // number.
    for (int i = 1; i <= n; i++)
        cout << 3 * i * (i - 1) + 1
             << " ";
}
 
// Driver Code
int main()
{
    int n = 10;
    centeredHexagonalSeries(n);
    return 0;
}

Java

// Program to find the series of
// centered hexadecimal number.
import java.io.*;
 
class GFG
{
    // Function to find the series of
    // centered hexadecimal number.
    static void centeredHexagonalSeries(int n)
    {
        // Formula to calculate nth
        // centered hexadecimal number.
        for (int i = 1; i <= n; i++)
            System.out.print( 3 * i *
                            (i - 1) + 1 + " ");
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int n = 10;
        centeredHexagonalSeries(n);
    }
}
 
// This code is contributed by Nikita Tiwari.

Python3

# Python3 program to find
# nth centered hexagonal number
 
# Function to find centered hexagonal
# series till n given numbers.
def centeredHexagonalSeries(n) :
    for i in range(1, n + 1) :
         
        # Formula to calculate nth
        # centered hexagonal series.
        print(3 * i * (i - 1) + 1, end=" ")
         
# Driver Code
if __name__ == '__main__' :
     
    n = 10
    centeredHexagonalSeries(n)
 
# This code is contributed
# by 'Akanshgupta'

C#

// C# Program to find the
// series of centered
// hexadecimal number.
using System;
 
class GFG
{
     
    // Function to find the
    // series of centered
    // hexadecimal number.
    static void centeredHexagonalSeries(int n)
    {
        // Formula to calculate nth
        // centered hexadecimal number.
        for (int i = 1; i <= n; i++)
            Console.Write( 3 * i *
                         (i - 1) + 1 + " ");
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 10;
        centeredHexagonalSeries(n);
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出 :

1 7 19 37 61 91 127 169 217 271