class Solution:
def isThree(self, n: int) -> bool:
# A number has exactly 3 divisors if and only if it is a perfect square of a prime
import math
root = int(math.sqrt(n))
if root * root != n:
return False
# Check if root is prime
if root < 2:
return False
for i in range(2, int(root ** 0.5) + 1):
if root % i == 0:
return False
return True
class Solution {
public:
bool isThree(int n) {
int root = (int)sqrt(n);
if (root * root != n) return false;
if (root < 2) return false;
for (int i = 2; i * i <= root; ++i) {
if (root % i == 0) return false;
}
return true;
}
};
class Solution {
public boolean isThree(int n) {
int root = (int)Math.sqrt(n);
if (root * root != n) return false;
if (root < 2) return false;
for (int i = 2; i * i <= root; ++i) {
if (root % i == 0) return false;
}
return true;
}
}
var isThree = function(n) {
let root = Math.floor(Math.sqrt(n));
if (root * root !== n) return false;
if (root < 2) return false;
for (let i = 2; i * i <= root; ++i) {
if (root % i === 0) return false;
}
return true;
};
Given a positive integer n, determine whether n has exactly three positive divisors. Return true if it does, and false otherwise.
nn has exactly three positive divisorsConstraints:
n ≤ 104
Let's start by understanding the problem: for a given integer n, we need to check if it has exactly three positive divisors. A brute-force approach would be to count all the divisors of n by iterating from 1 to n and checking which numbers divide n evenly. If the count is exactly 3, return true; otherwise, return false.
However, this brute-force approach is inefficient, especially for large values of n. Is there a mathematical property that characterizes numbers with exactly three divisors? Upon reflection, we notice that:
This leads to the insight: a number has exactly three positive divisors if and only if it is the square of a prime number. The divisors are 1, the prime, and the square itself.
Based on the mathematical insight, we can design an efficient algorithm:
n. Let's call it root.root * root == n. If not, n is not a perfect square, so it can't have exactly three divisors.root is a prime number. If it is, then n is the square of a prime, and thus has exactly three divisors.root is not prime, return false.
To check if root is prime, we can use trial division up to the square root of root.
n ≤ 10,000, root ≤ 100).n directly.
Consider n = 9:
root = 3.3 * 3 == 9: True, so 9 is a perfect square.true.
Now, consider n = 8:
root = 2.2 * 2 == 8: False (2 * 2 = 4 ≠ 8).false.Brute-force approach:
n to count divisors.root is prime by testing divisibility up to √root.
Since n ≤ 104, root ≤ 100, so the primality check is extremely fast.
The key insight for this problem is recognizing that only perfect squares of primes have exactly three divisors. This allows us to avoid brute-force divisor counting, and instead check two simple conditions: whether n is a perfect square, and whether its square root is prime. This leads to an efficient and elegant solution that works within the problem's constraints.