Sobes.tech
Senior

public static boolean isPerfectSquare(int n){ if (n < 0) return false; int sqrt = (int) Math.sqrt(n); return sqrt * sqrt == n; }

sobes.tech AI

Answer from AI

This method checks whether the number n is a perfect square of an integer.

Algorithm:

  • If n is negative, immediately return false, as negative numbers cannot be perfect squares.
  • Calculate the integer square root sqrt using Math.sqrt(n) and casting to int.
  • Check if the square of sqrt (sqrt * sqrt) equals the original number n. If yes — the number is a perfect square.

Example usage:

System.out.println(isPerfectSquare(16)); // true
System.out.println(isPerfectSquare(20)); // false
public static boolean isPerfectSquare(int n){ if (n <… - sobes.tech