Open In App

Program to check if N is a Centered Hexagonal Number

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:  

=> 
=> 
 

Below is the implementation of the above approach:

// 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 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 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# 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

                    
<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)


Article Tags :