📜  程序检查N是否为Myriagon编号

📅  最后修改于: 2021-05-06 19:38:38             🧑  作者: Mango

给定数字N ,任务是检查N是否为Myriagon数字。如果数字N是Myriagon数字,则打印“是”,否则打印“否”

例子:

方法:

  1. Myriagon数的第K项为:
    K^{th} Term = \frac{9998*K^{2} - 9996*K}{2}
  2. 因为我们必须检查给定的数字是否可以表示为Myriagon数。可以检查为:
  1. 如果使用上述公式计算的K值为整数,则N为Myriagon数。
  2. 其他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