Given a number N, check if it is Hexagonal or not. If it is then print “Yes” otherwise output “No”.
Input: N = 6
6 is the second hexagonal number.
Input: N = 14
The second hexagonal number is 6 while the third hexagonal number is 15. Hence 14 is not a hexagonal number.
Approach: To solve the problem mentioned above we have to note that the nth hexagonal Number is given by the formula: H(n) = n * (2n – 1). The formula indicates that the n-th hexagonal number depends quadratically on n. Therefore, find the positive integral root of N = H(n) equation.
Therefore, H(n) = nth hexagonal number and N is the given Number. Hence solve the following equation:
Here, H(n) = N
=> n * (2n – 1) = N
=> 2 * n * n – n – N = 0
The positive root of this equation is:
n = (1 + sqrt(8 N + 1)) / 4
After obtaining the value for n, check if it is an integer or not where n is an integer if n – floor(n) is 0.
Below is the implementation of the above approach:
Time Complexity: O(1)
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.
- Program to check if N is a Centered Hexagonal Number
- Program to check if N is a Concentric Hexagonal Number
- Program for Hexagonal Pattern
- Hexagonal Number
- Centered hexagonal number
- Surface Area and Volume of Hexagonal Prism
- Concentric Hexagonal Numbers
- Find all numbers up to N which are both Pentagonal and Hexagonal
- Second hexagonal numbers
- Program to check whether the given number is Buzz Number or not
- Program to check whether a number is Proth number or not
- Recursive program to check if number is palindrome or not
- Python program to check whether a number is Prime or not
- Program to check if N is a Centered Pentagonal Number or not
- Program to check if N is a Tridecagonal Number or not
- C Program to check whether a number is a Perfect Cube or not
- Check if a number is a Krishnamurthy Number or not
- Check Whether a number is Duck Number or not
- Check if given number is Emirp Number or not
- Check if a number with even number of digits is palindrome or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.