2020-08-16 21:16:08 +08:00
|
|
|
package Maths;
|
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/** https://en.wikipedia.org/wiki/Perfect_square */
|
2020-08-16 21:16:08 +08:00
|
|
|
public class PerfectSquare {
|
2020-10-24 18:23:28 +08:00
|
|
|
public static void main(String[] args) {
|
|
|
|
assert !isPerfectSquare(-1);
|
|
|
|
assert !isPerfectSquare(3);
|
|
|
|
assert !isPerfectSquare(5);
|
|
|
|
assert isPerfectSquare(9);
|
|
|
|
assert isPerfectSquare(100);
|
|
|
|
}
|
2020-08-16 21:16:08 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Check if a number is perfect square number
|
|
|
|
*
|
|
|
|
* @param number the number to be checked
|
|
|
|
* @return <tt>true</tt> if {@code number} is perfect square, otherwise <tt>false</tt>
|
|
|
|
*/
|
|
|
|
public static boolean isPerfectSquare(int number) {
|
|
|
|
int sqrt = (int) Math.sqrt(number);
|
|
|
|
return sqrt * sqrt == number;
|
|
|
|
}
|
|
|
|
}
|