Given an integer n, return true if n has exactly three positive divisors. Otherwise, return false.
An integer m is a divisor of n if there exists an integer k such that n = k * m
翻譯時間!
public class Solution {
public static boolean isThree(int n) {
int sqrt_n = (int) Math.sqrt(n);
if (sqrt_n * sqrt_n != n) {
return false;
}
// 判斷sqrt_n是否為質數
if (sqrt_n <= 1) return false;
for (int i = 2; i * i <= sqrt_n; i++) {
if (sqrt_n % i == 0) {
return false;
}
}
return true;
}
}
丟去leetcode檢查結果!
成功啦!下課!