Program to check if N is a Myriagon Number
Last Updated :
19 Sep, 2022
Given a number N, the task is to check if N is a Myriagon Number or not. If the number N is an Myriagon Number then print “Yes” else print “No”.
Myriagon Number is a polygon with 10000 sides. The first few Myriagon numbers are 1, 10000, 29997, 59992, 99985, 149976 …
Examples:
Input: N = 10000
Output: Yes
Explanation:
Second Myriagon number is 10000.
Input: N = 300
Output: No
Approach:
- The Kth term of the Myriagon number is given as:
- As we have to check that the given number can be expressed as a Myriagon Number or not. This can be checked as:
=>
=>
- If the value of K calculated using the above formula is an integer, then N is a Myriagon Number.
- Else N is not a Myriagon Number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isMyriagon( int N)
{
float n
= (9996 + sqrt (79984 * N + 99920016))
/ 19996;
return (n - ( int )n) == 0;
}
int main()
{
int N = 10000;
if (isMyriagon(N)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isMyriagon( int N)
{
double n = ( 9996 + Math.sqrt( 79984 * N +
99920016 )) / 19996 ;
return (n - ( int )n) == 0 ;
}
public static void main (String[] args)
{
int N = 10000 ;
if (isMyriagon(N))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import math
def isMyriagon(N):
n = ( 9996 + math.sqrt( 79984 * N +
99920016 )) / 19996
return (n - int (n)) = = 0
n = 10000
if (isMyriagon(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isMyriagon( int N)
{
double n = (9996 + Math.Sqrt(79984 * N +
99920016)) / 19996;
return (n - ( int )n) == 0;
}
static public void Main ()
{
int N = 10000;
if (isMyriagon(N))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function isMyriagon(N)
{
n = (9996 + Math.sqrt(79984 * N + 99920016))
/ 19996;
return (n - parseInt(n)) == 0;
}
N = 10000;
if (isMyriagon(N)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(logN) because inbuilt sqrt function is being used
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...