Program to check if N is a Hendecagonal Number
Given an integer N, the task is to check if N is a Hendecagonal Number or not. If the number N is a Hendecagonal Number then print “Yes” else print “No”
Hendecagonal Number is a figurate number that extends the concept of triangular and square numbers to the decagon(11-sided polygon). The nth hendecagonal number counts the number of dots in a pattern of n nested decagons, all sharing a common corner, where the ith hendecagon in the pattern has sides made of i dots spaced one unit apart from each other. The first few hendecagonal numbers are 1, 11, 30, 58, 95, 141…
Examples:
Input: N = 11
Output: Yes
Explanation:
Second hendecagonal number is 11.
Input: N = 40
Output: No
Approach:
1. The Kth term of the Hendecagonal Number is given as
2. As we have to check whether the given number can be expressed as a Hendecagonal Number or not. This can be checked as:
=>
=>
3. If the value of K calculated using the above formula is an integer, then N is a Hendecagonal Number.
4. Else N is not a Hendecagonal Number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool ishendecagonal( int N)
{
float n
= (7 + sqrt (72 * N + 49))
/ 18;
return (n - ( int )n) == 0;
}
int main()
{
int N = 11;
if (ishendecagonal(N)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.lang.Math;
class GFG{
public static boolean ishendecagonal( int N)
{
double n = ( 7 + Math.sqrt( 72 * N + 49 )) / 18 ;
return (n - ( int )n) == 0 ;
}
public static void main(String[] args)
{
int N = 11 ;
if (ishendecagonal(N))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import math
def ishendecagonal(N):
n = ( 7 + math.sqrt( 72 * N + 49 )) / / 18 ;
return (n - int (n)) = = 0 ;
N = 11 ;
if (ishendecagonal(N)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
public static bool ishendecagonal( int N)
{
double n = (7 + Math.Sqrt(72 * N + 49)) / 18;
return (n - ( int )n) == 0;
}
public static void Main( string [] args)
{
int N = 11;
if (ishendecagonal(N))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No\n" );
}
}
}
|
Javascript
<script>
function ishendecagonal( N)
{
let n
= (7 + Math.sqrt(72 * N + 49))
/ 18;
return (n - parseInt(n)) == 0;
}
let N = 11;
if (ishendecagonal(N)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(log N) because sqrt function is being used
Auxiliary Space: O(1)
Last Updated :
15 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...