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 …
Input: N = 6
Third Concentrichexagonal number is 6.
Input: N = 20
- The Kth term of the Concentric hexagonal number is given as:
Kth Term = (3 * K * K) / 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)
- If the value of K calculated using the above formula is an integer, then N is a Concentric Hexagonal Number.
- Else the number N is not a ConcentricHexagonal Number.
Below is the implementation of the above approach:
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.