Open In App

Different Methods to Check whether is the number is Square root or not in Java

In this article, we will learn different methods to check whether the number is a square root or not in Java.

Examples: 

Input: x = 4
Output: 4 is a perfect square: True

Input: x = 11
Output: 3
Explanation:  11 is a perfect square: False

Methods to Check Square Root in Java

Below are the methods by which we can check whether the number is square root or not in Java

Java Program to Check Whether is the Number is Square Root or Not

Below is the implementation of the above methods to check the number is square root or not.

Method 1: Using loop

In this method, we first repeat the number 1 up to half of the input number and check if the square of the current number is equal to the input number.

Example:

// Java program to check if a number is a perfect square using loop
import java.io.*;
public class SquareRootChecker {
    // Function to check if a number is a perfect square using looping
    public static boolean isPerfectSquareLoop(int num) {
        // Check for negative numbers
        if (num < 0) return false;
        // 0 and 1 are perfect squares
        if (num == 0 || num == 1) return true;
        
        // Loop to check squares of numbers from 1 to num/2
        for (int i = 1; i <= num / 2; i++) {
            if (i * i == num) { // If square equals num, it's a perfect square
                return true;
            }
        }
        
        // If no perfect square found
        return false;
    }
    
    // Driver code
    public static void main(String[] args) {
        int num = 36;  
      System.out.println(num + " is a perfect square: " + isPerfectSquareLoop(num));
    }
}

Output
36 is a perfect square: true

Complexity of the above Program:

Time Complexity: O(n)
Space Complexity: O(1)

Method 2: Using Primilaity Test

In this method, we use the fact that every equal square has a different prime multiplier to check if the square of the square root is equal to the input number.

Example:

// Java program to check if a number is a perfect square using primality test
import java.io.*;
public class SquareRootChecker {
    // Method to check if a number is a perfect square using primality test
    public static boolean isPerfectSquarePrimality(int num) 
    {
        // Check for negative numbers
        if (num < 0) return false;
        
        int sqrt = (int) Math.sqrt(num); // Calculate square root
        
        // If square of square root equals num, it's a perfect square
        return (sqrt * sqrt) == num;
    }
    
    // Driver code
    public static void main(String[] args) {
        int num = 4; 
        System.out.println(num + " is a perfect square: " + isPerfectSquarePrimality(num));
    }
}

Output
4 is a perfect square: true

Complexity of the above Program:

Time Complexity: O(1)
Space Complexity: O(1)

Method 3: Using Binary Search

In this method, we will use binary search to find the square root of the input number by reducing the search space iteratively.

Example:

 // Java program to check if a number is a perfect square using Binary search
import java.io.*;
public class SquareRootChecker {
    // Method to check if a number is a perfect square using binary search
    public static boolean isPerfectSquareBinarySearch(int num) 
    {
        // Check for negative numbers
        if (num < 0) return false;
        // 0 and 1 are perfect squares
        if (num <= 1) return true;
        
        // Initialize left and right pointers for binary search
        long left = 1, right = num / 2;
        
        // Binary search loop
        while (left <= right) {
            long mid = left + (right - left) / 2; // Calculate mid point
            long square = mid * mid; // Square of mid
            
            if (square == num) { // If square equals num, it's a perfect square
                return true;
            } else if (square < num) { // If square is less than num, search in right half
                left = mid + 1;
            } else { // If square is greater than num, search in left half
                right = mid - 1;
            }
        }
        
        // If no perfect square found
        return false;
    }
    
    //Driver code
    public static void main(String[] args) {
        int num = 81;  
        
        System.out.println(num + " is a perfect square: " + isPerfectSquareBinarySearch(num));
    }
}

Output
81 is a perfect square: true

Complexity of the above Program:

Time Complexity: O(log n)
Space Complexity: O(1)

Method 4: Using Log and Inbuilt Functions

In this method, we use the arithmetic property that if the square root of a number is an integer, the arithmetic is a perfect square.

Example:

// Java program to check if a number is a perfect square using logarithm and inbuilt functions
import java.io.*;
public class SquareRootChecker {
    // Method to check if a number is a perfect square using logarithm and inbuilt functions
    public static boolean isPerfectSquareLog(int num) {
        // Check for negative numbers
        if (num < 0) return false;
        
        double sqrt = Math.sqrt(num); // Calculate square root
        
        // If square root is an integer, it's a perfect square
        return (sqrt - Math.floor(sqrt)) == 0;
    }
    
    //Driver code
    public static void main(String[] args) {
        int num = 16;  
        System.out.println(num + " is a perfect square: " + isPerfectSquareLog(num));
    }
}

Output
16 is a perfect square: true

Complexity of the above method:

Time Complexity: O(1)
Space Complexity: O(1)

Article Tags :