Skip to content
Related Articles

Related Articles

Program to check if N is a Icosidigonal Number
  • Last Updated : 23 Mar, 2021

Given an integer N, the task is to check if it is a Icosidigonal Number or not. If the number N is an Icosidigonal Number then print “Yes” else print “No”.

Icosidigonal number
The polygon has many gons, depends on their gonal number series. In mathematics, there are a number of gonal numbers and the Icosidigonal Number is one of them and these numbers have 22 -sided polygon(icosidigon). An Icosidigonal Number belong to the class of figurative number. They have one common dots points and other dots pattern is arranged in an n-th nested Icosidigon pattern. 
The first few Icosidigonal numbers are 1, 22, 63, 124, 205, 306…

Examples:  

Input: N = 22 
Output: Yes 
Explanation: 
Second Icosidigonal number is 22.
Input: 30 
Output: No  

Approach:  



1. The Kth term of the Icosidigonal number is given as
K^{th} Term = \frac{20*K^{2} - 18*K}{2}

2. As we have to check that the given number can be expressed as an Icosidigonal Number or not. This can be checked as follows – 

=> N = \frac{20*K^{2} - 18*K}{2}
=> K = \frac{18 + \sqrt{160*N + 324}}{40}

3. If the value of K calculated using the above formula is an integer, then N is an Icosidigonal Number.

4. Else N is not an Icosidigonal 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 the number N
// is a Icosidigonal number
bool isIcosidigonal(int N)
{
    float n
        = (18 + sqrt(160 * N + 324))
          / 40;
 
    // Condition to check if the
    // number is a Icosidigonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 22;
 
    // Function call
    if (isIcosidigonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

Java




// Java program for the above approach
class GFG{
 
// Function to check if the number N
// is a icosidigonal number
static boolean isIcosidigonal(int N)
{
    float n = (float) ((18 + Math.sqrt(160 * N +
                                       324)) / 40);
 
    // Condition to check if the number
    // is a icosidigonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given number
    int N = 22;
 
    // Function call
    if (isIcosidigonal(N))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by Amit Katiyar

Python3




# Python3 program for the above approach
import numpy as np
 
# Function to check if the number N
# is a icosidigonal number
def isIcosidigonal(N):
 
    n = (18 + np.sqrt(160 * N + 324)) / 40
 
    # Condition to check if N
    # is a icosidigonal number
    return (n - int(n)) == 0
 
# Driver Code
N = 22
 
# Function call
if (isIcosidigonal(N)):
    print ("Yes")
else:
    print ("No")
 
# This code is contributed by PratikBasu

C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if the number N
// is a icosidigonal number
static bool isIcosidigonal(int N)
{
    float n = (float) ((18 + Math.Sqrt(160 * N +
                                    324)) / 40);
 
    // Condition to check if the number
    // is a icosidigonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void Main(string[] args)
{
     
    // Given number
    int N = 22;
 
    // Function call
    if (isIcosidigonal(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by rutvik_56

Javascript




<script>
 
// javascript program for the above approach
 
 
// Function to check if the number N
// is a Icosidigonal number
function isIcosidigonal( N)
{
    let n
        = (18 + Math.sqrt(160 * N + 324))
          / 40;
 
    // Condition to check if the
    // number is a Icosidigonal number
    return (n - parseInt(n)) == 0;
}
 
// Driver Code
 
    // Given Number
    let N = 22;
 
    // Function call
    if (isIcosidigonal(N)) {
         document.write( "Yes");
    }
    else {
        document.write( "No");
    }
     
 
    // This code contributed by aashish1995
 
</script>
Output: 
Yes

 

Time Complexity: O(1)

Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :