Given an integer N, the task is to check if N is a Heptagonal Number or not. If the number N is an Heptagonal Number then print “Yes” else print “No”.
Heptagonal Number represents Heptagon and belongs to a figurative number. Heptagonal has seven angles, seven vertices, and seven-sided polygon. The first few Heptagonal Numbers are 1, 7, 18, 34, 55, 81, …
Input: N = 7
Second heptagonal number is 7.
Input: N = 30
- The Kth term of the heptagonal number is given as
- As we have to check that the given number can be expressed as a Heptagonal Number or not. This can be checked as:
- If the value of K calculated using the above formula is an integer, then N is a Heptagonal Number.
- Else N is not a Heptagonal Number.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: 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. 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 industry experts, please refer Geeks Classes Live