📌  相关文章
📜  检查数字是否为完美平方而不找到平方根

📅  最后修改于: 2021-06-26 00:49:22             🧑  作者: Mango

在没有找到平方根的情况下,检查一个数字是否是一个完美的平方。

例子:

我们讨论了一种检查数字是否为正平方的方法。

方法1:
这个想法是要运行一个从i = 1到floor(sqrt(n))的循环,然后检查平方是否等于n。

C++
// C++ program to check if a number is perfect
// square without finding square root
#include 
using namespace std;
 
bool isPerfectSquare(int n)
{
    for (int i = 1; i * i <= n; i++) {
 
        // If (i * i = n)
        if ((n % i == 0) && (n / i == i)) {
            return true;
        }
    }
    return false;
}
 
// Driver code
int main()
{
    long long int n = 36;
    if (isPerfectSquare(n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java program to check if a number is perfect
// square without finding the square root
public class GfG {
 
    static boolean isPerfectSquare(int n)
    {
        for (int i = 1; i * i <= n; i++) {
 
            // If (i * i = n)
            if ((n % i == 0) && (n / i == i)) {
                return true;
            }
        }
        return false;
    }
 
    public static void main(String[] args)
    {
 
        int n = 36;
 
        if (isPerfectSquare(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by Rituraj Jain


Python3
# Python3 program to check if a number is
# perfect square without finding square root
 
# from math import sqrt function
 
 
def isPerfectSquare(n) :
 
    i = 1
    while(i * i<= n):
         
        # If (i * i = n)
        if ((n % i == 0) and (n / i == i)):
            return True
             
        i = i + 1
    return False
 
# Driver code
if __name__ == "__main__" :
 
    n = 36
    if (isPerfectSquare(n)):
        print("Yes, it is a perfect square.")
    else :
        print("No, it is not a perfect square.")
 
    # This code is contributed by Ryuga


C#
// C# program to check if a number is perfect
// square without finding the square root
using System;
 
public class GfG {
 
    static bool isPerfectSquare(int n)
    {
        for (int i = 1; i * i <= n; i++) {
 
            // If (i * i = n)
            if ((n % i == 0) && (n / i == i)) {
                return true;
            }
        }
        return false;
    }
 
    public static void Main()
    {
 
        int n = 36;
 
        if (isPerfectSquare(n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
/*This code is contributed by Rajput-Ji*/


PHP


Javascript


C++
// C++ program for above approach
#include 
using namespace std;
 
// Program to find if x is a
// perfect square.
bool isPerfectSquare(int x)
{ 
    long long left = 1, right = x;
   
    while (left <= right)
    {
        long long mid = (left + right) / 2;
        
        // Check if mid is perfect square
        if (mid * mid == x)
        {
            return true;
        }
         
        // Mid is small -> go right to increase mid
        if (mid * mid < x)
        {
            left = mid + 1;
        }
       
        // Mid is large -> to left to decrease mid
        else
        {
            right = mid - 1;
        }
    }
    return false;
}
 
// Driver Code
int main()
{
    int x = 2500;
   
    // Function Call
    if (isPerfectSquare(x))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program for above approach
class GFG
{
     
    // Program to find if x is a
    // perfect square.
    static boolean isPerfectSquare(int num)
    {
        long left = 1, right = num;
         
        while (left <= right)
        {
            long mid = (left + right) / 2;
           
            // Check if mid is perfect square
            if (mid * mid == num)
            {
                return true;
            }
            
            // Mid is small -> go right to increase mid
            if (mid * mid < num)
            {
                left = mid + 1;
            }
           
            // Mid is large -> to left to decrease mid
            else
            {
                right = mid - 1;
            }
        }
        return false;
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int x = 2500;
         
        // Function Call
        if (isPerfectSquare(x))
            System.out.print("Yes");
        else
            System.out.print("No");
    }
}


Python3
# Python program for above approach
 
# Program to find if x is a
# perfect square.
def isPerfectSquare(x):
     
    left = 1
    right = x
     
    while (left <= right):
       
        mid = (left + right) >> 1
         
        # Check if mid is perfect square
        if ((mid * mid) == x):
            return True
         
        # Mid is small -> go right to increase mid
        if (mid * mid < x):
            left = mid + 1
           
        # Mid is large -> to left to decrease mid
        else:
            right = mid - 1
    return False
 
# Driver code
if __name__ == "__main__":
   
  x = 2500
   
  # Function Call
  if (isPerfectSquare(x)):
      print("Yes")
  else:
      print("No")


C#
// C# program for above approach
using System;
class GFG{
     
// Program to find if x is a
// perfect square.
static bool isPerfectSquare(int x)
{
  long left = 1, right = x;
 
  while (left <= right)
  {
    long mid = (left + right) / 2;
 
    // Check if mid is perfect
    // square
    if (mid * mid == x)
    {
      return true;
    }
 
    // Mid is small -> go right to
    // increase mid
    if (mid * mid < x)
    {
      left = mid + 1;
    }
 
    // Mid is large -> to left
    // to decrease mid
    else
    {
      right = mid - 1;
    }
  }
  return false;
}
      
// Driver code
public static void Main(string[] args)
{
  int x = 2500;
 
  // Function Call
  if (isPerfectSquare(x))
    Console.Write("Yes");
  else
    Console.Write("No");
}
}
 
// This code is contributed by Rutvik_56


Javascript


输出:

Yes

时间复杂度: O(sqrt(N))

方法2:
这个想法是使用二进制搜索来找到一个范围为1到n的平方等于n的数字,这样在每次迭代中,问题陈述会减少为一半[1到n / 2-1或n / 2到n]。

C++

// C++ program for above approach
#include 
using namespace std;
 
// Program to find if x is a
// perfect square.
bool isPerfectSquare(int x)
{ 
    long long left = 1, right = x;
   
    while (left <= right)
    {
        long long mid = (left + right) / 2;
        
        // Check if mid is perfect square
        if (mid * mid == x)
        {
            return true;
        }
         
        // Mid is small -> go right to increase mid
        if (mid * mid < x)
        {
            left = mid + 1;
        }
       
        // Mid is large -> to left to decrease mid
        else
        {
            right = mid - 1;
        }
    }
    return false;
}
 
// Driver Code
int main()
{
    int x = 2500;
   
    // Function Call
    if (isPerfectSquare(x))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Java

// Java program for above approach
class GFG
{
     
    // Program to find if x is a
    // perfect square.
    static boolean isPerfectSquare(int num)
    {
        long left = 1, right = num;
         
        while (left <= right)
        {
            long mid = (left + right) / 2;
           
            // Check if mid is perfect square
            if (mid * mid == num)
            {
                return true;
            }
            
            // Mid is small -> go right to increase mid
            if (mid * mid < num)
            {
                left = mid + 1;
            }
           
            // Mid is large -> to left to decrease mid
            else
            {
                right = mid - 1;
            }
        }
        return false;
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int x = 2500;
         
        // Function Call
        if (isPerfectSquare(x))
            System.out.print("Yes");
        else
            System.out.print("No");
    }
}

Python3

# Python program for above approach
 
# Program to find if x is a
# perfect square.
def isPerfectSquare(x):
     
    left = 1
    right = x
     
    while (left <= right):
       
        mid = (left + right) >> 1
         
        # Check if mid is perfect square
        if ((mid * mid) == x):
            return True
         
        # Mid is small -> go right to increase mid
        if (mid * mid < x):
            left = mid + 1
           
        # Mid is large -> to left to decrease mid
        else:
            right = mid - 1
    return False
 
# Driver code
if __name__ == "__main__":
   
  x = 2500
   
  # Function Call
  if (isPerfectSquare(x)):
      print("Yes")
  else:
      print("No")

C#

// C# program for above approach
using System;
class GFG{
     
// Program to find if x is a
// perfect square.
static bool isPerfectSquare(int x)
{
  long left = 1, right = x;
 
  while (left <= right)
  {
    long mid = (left + right) / 2;
 
    // Check if mid is perfect
    // square
    if (mid * mid == x)
    {
      return true;
    }
 
    // Mid is small -> go right to
    // increase mid
    if (mid * mid < x)
    {
      left = mid + 1;
    }
 
    // Mid is large -> to left
    // to decrease mid
    else
    {
      right = mid - 1;
    }
  }
  return false;
}
      
// Driver code
public static void Main(string[] args)
{
  int x = 2500;
 
  // Function Call
  if (isPerfectSquare(x))
    Console.Write("Yes");
  else
    Console.Write("No");
}
}
 
// This code is contributed by Rutvik_56

Java脚本


输出:

Yes

时间复杂度: O(log(N))
空间复杂度: O(1)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。