给定数字N ,任务是检查N是否为Myriagon数字。如果数字N是Myriagon数字,则打印“是”,否则打印“否” 。
Myriagon Number is a polygon with 10000 sides. The first few Myriagon numbers are 1, 10000, 29997, 59992, 99985, 149976 …
例子:
Input: N = 10000
Output: Yes
Explanation:
Second Myriagon number is 10000.
Input: N = 300
Output: No
方法:
- Myriagon数的第K个项为:
- 因为我们必须检查给定的数字是否可以表示为Myriagon数。可以检查为:
=>
=>
- 如果使用上述公式计算的K值为整数,则N为Myriagon数。
- 其他N不是Myriagon编号。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if N is a
// Myriagon Number
bool isMyriagon(int N)
{
float n
= (9996 + sqrt(79984 * N + 99920016))
/ 19996;
// Condition to check if the
// number is a Myriagon number
return (n - (int)n) == 0;
}
// Driver Code
int main()
{
// Given Number
int N = 10000;
// Function call
if (isMyriagon(N)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to check if N
// is a myriagon number
static boolean isMyriagon(int N)
{
double n = (9996 + Math.sqrt(79984 * N +
99920016)) / 19996;
// Condition to check if the
// number is a myriagon number
return (n - (int)n) == 0;
}
// Driver Code
public static void main (String[] args)
{
// Given Number
int N = 10000;
// Function call
if (isMyriagon(N))
{
System.out.println("Yes" );
}
else
{
System.out.println("No" );
}
}
}
// This code is contributed by ShubhamCoder
Python3
# Python3 implementation to check that
# a number is a myriagon number or not
import math
# Function to check that the
# number is a myriagon number
def isMyriagon(N):
n = (9996 + math.sqrt(79984 * N +
99920016)) / 19996
# Condition to check if the
# number is a myriagon number
return (n - int(n)) == 0
# Driver Code
n = 10000
# Function call
if (isMyriagon(n)):
print("Yes")
else:
print("No")
# This code is contributed by ShubhamCoder
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if N
// is a myriagon number
static bool isMyriagon(int N)
{
double n = (9996 + Math.Sqrt(79984 * N +
99920016)) / 19996;
// Condition to check if the
// number is a myriagon number
return (n - (int)n) == 0;
}
// Driver Code
static public void Main ()
{
// Given Number
int N = 10000;
// Function call
if (isMyriagon(N))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
// This code is contributed by ShubhamCoder
Javascript
输出:
Yes