Program to check if N is a Chiliagon Number
Last Updated :
30 Dec, 2022
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:
- The Kth term of the Chiliagon Number is given as
- As we have to check that the given number can be expressed as a Chiliagon Number or not. This can be checked as follows:
=>
=>
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++
#include <bits/stdc++.h>
using namespace std;
bool is_Chiliagon( int N)
{
float n
= (996 + sqrt (7984 * N + 992016))
/ 1996;
return (n - ( int )n) == 0;
}
int main()
{
int N = 1000;
if (is_Chiliagon(N)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
class GFG{
static boolean is_Chiliagon( int N)
{
float n = ( float )( 996 + Math.sqrt( 7984 * N +
992016 )) / 1996 ;
return (n - ( int ) n) == 0 ;
}
public static void main(String s[])
{
int N = 1000 ;
if (is_Chiliagon(N))
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
import math;
def is_Chiliagon(N):
n = ( 996 + math.sqrt( 7984 * N +
992016 )) / / 1996 ;
return (n - int (n)) = = 0 ;
N = 1000 ;
if (is_Chiliagon(N)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
static bool is_Chiliagon( int N)
{
float n = ( float )(996 + Math.Sqrt(7984 * N +
992016)) / 1996;
return (n - ( int ) n) == 0;
}
public static void Main()
{
int N = 1000;
if (is_Chiliagon(N))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function is_Chiliagon(N)
{
let n
= (996 + Math.sqrt(7984 * N + 992016))
/ 1996;
return (n - Math.floor(n)) == 0;
}
let N = 1000;
if (is_Chiliagon(N)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(logN)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...