Skip to content
Related Articles

Related Articles

Program to check if N is a Concentric Hexagonal Number
  • Last Updated : 01 Jun, 2020

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

Concentric Hexagonal Numbers are the number sequence forms a pattern with concentric hexagons, and the numbers denote the number of points required after the N-th stage of the pattern. The first few concentric hexagonal numbers are 0, 1, 6, 13, 24, 37, 54, 73, 96, 121 …

Examples:

Input: N = 6
Output: Yes
Explanation:
Third Concentrichexagonal number is 6.

Input: N = 20
Output: No



Approach:

  1. The Kth term of the Concentric hexagonal number is given as:

    Kth Term = (3 * K * K) / 2

  2. As we have to check that the given number can be expressed as a Concentric hexagonal number or not. This can be checked as:

    Here, Kth Term = N
    => (3 * K * K) / 2 = N
    => 3 * K * K – 2 * N = 0
    The positive root of this equation is:
    K = sqrt((2 * N )/3)

  3. If the value of K calculated using the above formula is an integer, then N is a Concentric Hexagonal Number.
  4. Else the number N is not a ConcentricHexagonal Number.
  5. Below is the implementation of the above approach:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to check if N is a
    // Concentric Hexagonal Number
    class GFG{
      
    // Function to check if the
    // number is a Concentric hexagonal number
    static boolean isConcentrichexagonal(int N)
    {
        float n = (float) Math.sqrt((2 * N) / 3);
      
        // Condition to check if the
        // number is a Concentric 
        // hexagonal number
        return (n - (int)n) == 0;
    }
      
    // Driver Code
    public static void main(String[] args)
    {
        int N = 6;
      
        // Function call
        if (isConcentrichexagonal(N)) 
        {
            System.out.print("Yes");
        }
        else 
        {
            System.out.print("No");
        }
    }
    }
      
    // This code is contributed by PrinciRaj1992

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python3 program to check if N is a 
    # concentric hexagonal number 
    import math
      
    # Function to check if the number
    # is a concentric hexagonal number 
    def isConcentrichexagonal(N):
          
        n = math.sqrt((2 * N) / 3)
          
        # Condition to check if the 
        # number is a concentric 
        # hexagonal number
        return (n - int(n)) == 0
      
    # Driver code
    N = 6
      
    if isConcentrichexagonal(N):
        print("Yes")
    else:
        print("No")
      
    # This code is contributed by divyeshrabadiya07

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to check if N is a
    // concentric hexagonal number
    using System;
      
    class GFG{
      
    // Function to check if the number 
    // is a concentric hexagonal number
    static bool isConcentrichexagonal(int N)
    {
        float n = (float) Math.Sqrt((2 * N) / 3);
      
        // Condition to check if the
        // number is a concentric 
        // hexagonal number
        return (n - (int)n) == 0;
    }
      
    // Driver Code
    public static void Main()
    {
        int N = 6;
      
        // Function call
        if (isConcentrichexagonal(N)) 
        {
            Console.Write("Yes");
        }
        else
        {
            Console.Write("No");
        }
    }
    }
      
    // This code is contributed by Code_Mech

    chevron_right

    
    

    Output:

    Yes
    

    Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

    My Personal Notes arrow_drop_up
Recommended Articles
Page :