Skip to content
Related Articles

Related Articles

Improve Article

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.

    Below is the implementation of the above approach:

    C++




    // 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;
    }

    Java




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

    Python3




    # 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

    C#




    // 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
    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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

    In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




    My Personal Notes arrow_drop_up
Recommended Articles
Page :