📌  相关文章
📜  检查给定的数字是否为完美的平方

📅  最后修改于: 2021-05-31 23:36:33             🧑  作者: Mango

给定一个数字,检查它是否是一个完美的正方形。

例子 :

Input : 2500
Output : Yes
Explanation:
2500 is a perfect square.
50 * 50 = 2500

Input  : 2555
Output : No

方法:

  1. 取数字的平方根。
  2. 将平方根乘以两次
  3. 使用布尔等于运算符来验证平方根的乘积是否等于给定的数字。
C++
// CPP program to find if x is a
// perfect square.
#include 
using namespace std;
 
bool isPerfectSquare(long double x)
{
    // Find floating point value of
    // square root of x.
    if (x >= 0) {
 
        long long sr = sqrt(x);
         
        // if product of square root
        //is equal, then
        // return T/F
        return (sr * sr == x);
    }
    // else return false if n<0
    return false;
}
 
int main()
{
    long long x = 2500;
    if (isPerfectSquare(x))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program to find if x is a
// perfect square.
class GFG {
 
    static boolean isPerfectSquare(double x)
    {
        if (x >= 0) {
           
            // Find floating point value of
            // square root of x.
            double sr = Math.sqrt(x);
           
            // if product of square root
            // is equal, then
            // return T/F
 
            return ((sr * sr) == x);
        }
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        double x = 2500;
 
        if (isPerfectSquare(x))
            System.out.print("Yes");
        else
            System.out.print("No");
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to find if x is a
# perfect square.
 
import math
 
 
def isPerfectSquare(x):
 
    #if x >= 0,
    if(x >= 0):
        sr = math.sqrt(x)
        # sqrt function returns floating value so we have to convert it into integer
        #return boolean T/F
        return (int(sr*sr) == x)
    return false
 
# Driver code
 
 
x = 2500
if (isPerfectSquare(x)):
    print("Yes")
else:
    print("No")
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to find if x is a
// perfect square.
using System;
class GFG {
 
    static bool isPerfectSquare(double x)
    {
 
        // Find floating point value of
        // square root of x.
        if (x >= 0) {
 
            double sr = Math.Sqrt(x);
           
            // if product of square root
            // is equal, then
            // return T/F
            return (sr * sr == x);
        }
        // else return false if n<0
        return false;
    }
 
    // Driver code
    public static void Main()
    {
        double x = 2500;
 
        if (isPerfectSquare(x))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// C++ program for the above approach
#include 
#include 
using namespace std;
 
void checkperfectsquare(int n)
{
     
    // If ceil and floor are equal
    // the number is a perfect
    // square
    if (ceil((double)sqrt(n)) == floor((double)sqrt(n))) {
        cout << "perfect square";
    }
    else {
        cout << "not a perfect square";
    }
}
 
// Driver Code
int main()
{
 
    int n = 49;
    checkperfectsquare(n);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
static void checkperfectsquare(int n)
{
     
    // If ceil and floor are equal
    // the number is a perfect
    // square
    if (Math.ceil((double)Math.sqrt(n)) ==
        Math.floor((double)Math.sqrt(n)))
    {
        System.out.print("perfect square");
    }
    else
    {
        System.out.print("not a perfect square");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 49;
     
    checkperfectsquare(n);
}
}
 
// This code is contributed by subhammahato348


C#
// C# program for the above approach
using System;
 
class GFG{
 
static void checkperfectsquare(int n)
{
     
    // If ceil and floor are equal
    // the number is a perfect
    // square
    if (Math.Ceiling((double)Math.Sqrt(n)) ==
        Math.Floor((double)Math.Sqrt(n)))
    {
        Console.Write("perfect square");
    }
    else
    {
        Console.Write("not a perfect square");
    }
}
 
// Driver Code
public static void Main()
{
    int n = 49;
 
    checkperfectsquare(n);
}
}
 
// This code is contributed by subhammahato348


Javascript


输出
Yes

要了解有关内置sqrt函数的更多信息,请参考此Stackoverflow和此StackExchange线程。

另一种方法:

  1. 使用floor和ceil函数。
  2. 如果它们相等,则表示数字是一个完美的平方。

C++

// C++ program for the above approach
#include 
#include 
using namespace std;
 
void checkperfectsquare(int n)
{
     
    // If ceil and floor are equal
    // the number is a perfect
    // square
    if (ceil((double)sqrt(n)) == floor((double)sqrt(n))) {
        cout << "perfect square";
    }
    else {
        cout << "not a perfect square";
    }
}
 
// Driver Code
int main()
{
 
    int n = 49;
    checkperfectsquare(n);
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
 
class GFG{
 
static void checkperfectsquare(int n)
{
     
    // If ceil and floor are equal
    // the number is a perfect
    // square
    if (Math.ceil((double)Math.sqrt(n)) ==
        Math.floor((double)Math.sqrt(n)))
    {
        System.out.print("perfect square");
    }
    else
    {
        System.out.print("not a perfect square");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 49;
     
    checkperfectsquare(n);
}
}
 
// This code is contributed by subhammahato348

C#

// C# program for the above approach
using System;
 
class GFG{
 
static void checkperfectsquare(int n)
{
     
    // If ceil and floor are equal
    // the number is a perfect
    // square
    if (Math.Ceiling((double)Math.Sqrt(n)) ==
        Math.Floor((double)Math.Sqrt(n)))
    {
        Console.Write("perfect square");
    }
    else
    {
        Console.Write("not a perfect square");
    }
}
 
// Driver Code
public static void Main()
{
    int n = 49;
 
    checkperfectsquare(n);
}
}
 
// This code is contributed by subhammahato348

Java脚本


输出
perfect square