Open In App

Program to check if N is a Hendecagonal Number

Improve
Improve
Like Article
Like
Save
Share
Report

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
K^{th} Term = \frac{9*K^{2} - 7*K}{2}
2. As we have to check whether the given number can be expressed as a Hendecagonal Number or not. This can be checked as: 

=> N = \frac{9*K^{2} - 7*K}{2}
=> K = \frac{7 + \sqrt{72*N + 49}}{18}

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

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if N is a
// Hendecagonal Number
bool ishendecagonal(int N)
{
    float n
        = (7 + sqrt(72 * N + 49))
          / 18;
 
    // Condition to check if the
    // number is a hendecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 11;
 
    // Function call
    if (ishendecagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

                    

Java

// Java program for the above approach
import java.lang.Math;
 
class GFG{
     
// Function to check if N is a
// hendecagonal number
public static boolean ishendecagonal(int N)
{
    double n = (7 + Math.sqrt(72 * N + 49)) / 18;
     
    // Condition to check if the
    // number is a hendecagonal number
    return (n - (int)n) == 0;
}
 
// Driver code   
public static void main(String[] args)
{
         
    // Given number
    int N = 11;
     
    // Function call
    if (ishendecagonal(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by divyeshrabadiya07   

                    

Python3

# Python3 program for the above approach
import math
 
# Function to check if N is a
# Hendecagonal Number
def ishendecagonal(N):
 
    n = (7 + math.sqrt(72 * N + 49))// 18;
 
    # Condition to check if the
    # number is a hendecagonal number
    return (n - int(n)) == 0;
 
# Driver Code
 
# Given Number
N = 11;
 
# Function call
if (ishendecagonal(N)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by Nidhi_biet

                    

C#

// C# program for the above approach
using System;
class GFG{
     
// Function to check if N is a
// hendecagonal number
public static bool ishendecagonal(int N)
{
    double n = (7 + Math.Sqrt(72 * N + 49)) / 18;
     
    // Condition to check if the
    // number is a hendecagonal number
    return (n - (int)n) == 0;
}
 
// Driver code
public static void Main(string[] args)
{
         
    // Given number
    int N = 11;
     
    // Function call
    if (ishendecagonal(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No\n");
    }
}
}
 
// This code is contributed by rutvik_56

                    

Javascript

<script>
 
// javascript program for the above approach
 
 
// Function to check if N is a
// Hendecagonal Number
function ishendecagonal( N)
{
    let n
        = (7 + Math.sqrt(72 * N + 49))
          / 18;
 
    // Condition to check if the
    // number is a hendecagonal number
    return (n - parseInt(n)) == 0;
}
 
 
// Driver Code
 
    // Given Number
    let N = 11;
 
    // Function Call
    if (ishendecagonal(N)) {
        document.write( "Yes");
    }
    else {
        document.write( "No");
    }
 
// This code contributed by gauravrajput1
 
</script>

                    

Output
Yes

Time Complexity: O(log N) because sqrt function is being used
Auxiliary Space: O(1)



Last Updated : 15 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads