Open In App
Related Articles

Program to check if N is a Centered Hexagonal Number

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given an integer N, the task is to check if N is a Centered Hexagonal Number or not. If the number N is a Centered Hexagonal Number then print “Yes” else print “No”.

Centered hexagonal number are figurate numbers and are in the form of the Hexagon. The Centered Hexagonal number is different from Hexagonal Number because it contains one element at the center..The first few Centered hexagonal numbers are 1, 7, 19, 37, 61, 91, 127 …

Examples:  

Input: N = 7 
Output: Yes 
Explanation: 
Second Centered hexagonal number is 7.

Input: N = 20 
Output: No 
 

Approach:  

  • The Kth term of the Centered hexagonal number is given as
    K^{th} Term = {3*K^{2} - 3*K + 2}
  • As we have to check that the given number can be expressed as a Centered hexagonal number or not. This can be checked as:

=> N = {3*K^{2} - 3*K + 2}
=> K = \frac{3 + \sqrt{12*N - 3}}{6}
 

  • If the value of K calculated using the above formula is an integer, then N is a Centered Hexagonal Number.
  • Else the number N is not a Centered Hexagonal Number.

Below is the implementation of the above approach:

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check that the
// number is a Centered hexagonal number
bool isCenteredhexagonal(int N)
{
    float n
        = (3 + sqrt(12 * N - 3))
          / 6;
 
    // Condition to check if the
    // number is a Centered hexagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    int N = 7;
 
    // Function call
    if (isCenteredhexagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

                    

Java

// Java program for the above approach
class GFG{
 
// Function to check that the
// number is a Centered hexagonal number
static boolean isCenteredhexagonal(int N)
{
    float n = (float)((3 + Math.sqrt(12 * N - 3)) / 6);
 
    // Condition to check if the
    // number is a Centered hexagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 7;
 
    // Function call
    if (isCenteredhexagonal(N))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by sapnasingh4991

                    

Python3

# Python3 program for the above approach
import math
 
# Function to check that the number
# is a centered hexagonal number
def isCenteredhexagonal(N):
     
    n = (3 + math.sqrt(12 * N - 3)) / 6
     
    # Condition to check if the number
    # is a centered hexagonal number
    return (n - int(n)) == 0
 
# Driver Code
N = 7
 
if isCenteredhexagonal(N):
    print("Yes")
else :
    print("No")
 
# This code is contributed by ishayadav181

                    

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to check that the number
// is a centered hexagonal number
static bool isCenteredhexagonal(int N)
{
    float n = (float)((3 + Math.Sqrt(12 * N -
                                     3)) / 6);
 
    // Condition to check if the number
    // is a centered hexagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 7;
 
    // Function call
    if (isCenteredhexagonal(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by amal kumar choubey

                    

Javascript

<script>
 
// Javascript program for the above approach
 
// Function to check that the
// number is a Centered hexagonal number
function isCenteredhexagonal(N)
{
    let n = parseInt((3 + Math.sqrt(12 * N - 3)) / 6);
 
    // Condition to check if the
    // number is a Centered hexagonal number
    return (n - parseInt(n)) == 0;
}
 
// Driver Code
let N = 7;
 
// Function call
if (isCenteredhexagonal(N))
{
    document.write("Yes");
}
else
{
    document.write("No");
}
 
// This code is contributed by souravmahato348
 
</script>

                    

Output: 
Yes

 

Time Complexity: O(logN) because it is using inbuilt sqrt function
Auxiliary Space: O(1)



Last Updated : 18 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads