Skip to content
Related Articles

Related Articles

Program to check if N is a Chiliagon Number

View Discussion
Improve Article
Save Article
  • Last Updated : 27 Jun, 2021

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

Chiliagon Number is class of figurate number. It has 1000 – sided polygon called Chiliagon. The N-th Chiliagon Number counts the 1000 number of dots and all others dots are surrounding with a common sharing corner and make a pattern. The first few Chiliagon Numbers are 1, 1000, 2997, 5992, … 
 

Examples: 
 

Input: N = 1000 
Output: Yes 
Explanation: 
Second chiliagon number is 1000
Input: 35 
Output: No 
 

 

Approach: 
 

  1. The Kth term of the Chiliagon Number is given as 
    K^{th} Term = \frac{998*K^{2} - 996*K}{2}
     
  2. As we have to check that the given number can be expressed as a Chiliagon Number or not. This can be checked as follows: 
     

=> N = \frac{998*K^{2} - 996*K}{2}
=> K = \frac{996 + \sqrt{7984*N + 992016}}{1996}
 
 

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

4. Else N is not a Chiliagon Number.

Below is the implementation of the above approach:
 

C++




// C++ for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check that if N is
// Chiliagon Number or not
bool is_Chiliagon(int N)
{
    float n
        = (996 + sqrt(7984 * N + 992016))
          / 1996;
 
    // Condition to check if N is a
    // Chiliagon Number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 1000;
 
    // Function call
    if (is_Chiliagon(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

Java




// Java program for the above approach
class GFG{
     
// Function to check that if N is
// Chiliagon Number or not
static boolean is_Chiliagon(int N)
{
    float n = (float)(996 + Math.sqrt(7984 * N +
                                      992016)) / 1996;
 
    // Condition to check if N is a
    // Chiliagon Number
    return (n - (int) n) == 0;
}
 
// Driver Code
public static void main(String s[])
{
    // Given Number
    int N = 1000;
 
    // Function call
    if (is_Chiliagon(N))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by rutvik_56

Python3




# Python3 for the above approach
import math;
 
# Function to check that if N is
# Chiliagon Number or not
def is_Chiliagon(N):
 
    n = (996 + math.sqrt(7984 * N +
                         992016)) // 1996;
 
    # Condition to check if N is a
    # Chiliagon Number
    return (n - int(n)) == 0;
 
# Driver Code
 
# Given Number
N = 1000;
 
# Function call
if (is_Chiliagon(N)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by Code_Mech

C#




// C# program for the above approach
using System;
class GFG{
     
// Function to check that if N is
// Chiliagon Number or not
static bool is_Chiliagon(int N)
{
    float n = (float)(996 + Math.Sqrt(7984 * N +
                                      992016)) / 1996;
 
    // Condition to check if N is a
    // Chiliagon Number
    return (n - (int) n) == 0;
}
 
// Driver Code
public static void Main()
{
    // Given Number
    int N = 1000;
 
    // Function call
    if (is_Chiliagon(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by Code_Mech

Javascript




<script>
 
// Javascript for the above approach
 
// Function to check that if N is
// Chiliagon Number or not
function is_Chiliagon(N)
{
    let n
        = (996 + Math.sqrt(7984 * N + 992016))
        / 1996;
 
    // Condition to check if N is a
    // Chiliagon Number
    return (n - Math.floor(n)) == 0;
}
 
// Driver Code
 
    // Given Number
    let N = 1000;
 
    // Function call
    if (is_Chiliagon(N)) {
        document.write("Yes");
    }
    else {
        document.write("No");
    }
 
// This code is contributed by Mayank Tyagi
 
</script>

Output: 

Yes

 

Time Complexity: O(N1/2)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!