Handle negative input in perfect square checks (#7207)

This commit is contained in:
asmitha-16
2026-01-14 03:09:41 +05:30
committed by GitHub
parent 1644db2a34
commit bdda4fa6b4

View File

@@ -15,6 +15,9 @@ public final class PerfectSquare {
* <tt>false</tt>
*/
public static boolean isPerfectSquare(final int number) {
if (number < 0) {
return false;
}
final int sqrt = (int) Math.sqrt(number);
return sqrt * sqrt == number;
}
@@ -27,6 +30,9 @@ public final class PerfectSquare {
* {@code false}
*/
public static boolean isPerfectSquareUsingPow(long number) {
if (number < 0) {
return false;
}
long a = (long) Math.pow(number, 1.0 / 2);
return a * a == number;
}