In number theory, every positive integer has a set of divisors. "Three Divisors" asks you to determine if a given positive integer 'n' has exactly three positive divisors. If it does, return true; otherwise, return false. This problem requires an understanding of how the number of divisors relates to the prime factorization of a number.
This three divisors interview question is a classic math-based problem used by Microsoft and Google. It tests your ability to apply mathematical properties to simplify a search. While you could check every number from 1 to 'n' to count its divisors, an efficient solution requires recognizing the specific mathematical property of numbers with exactly three divisors: they are always squares of prime numbers.
This problem follows the Math, Number Theory, Enumeration interview pattern.
sqrt * sqrt == n.sqrt is a prime number.sqrt is prime:
sqrt < 2, return false.sqrt. If any number divides sqrt, it's not prime.n = 9.
In "Three Divisors coding problem," a common mistake is only checking if the number is a perfect square. For example, 16 is a perfect square (4^2), but its divisors are 1, 2, 4, 8, 16 (total of 5). The square root must be prime. Another error is not correctly handling the case where n=1 (divisors: {1}, total 1).
Brush up on basic prime properties. Knowing that the number of divisors is determined by the exponents in the prime factorization ( (e1+1)(e2+1)... ) is very helpful. For a number to have exactly 3 divisors, there must be only one prime factor with an exponent of 2.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Number of Common Factors | Easy | Solve | |
| Count Primes | Medium | Solve | |
| Count Symmetric Integers | Easy | Solve | |
| Smallest Even Multiple | Easy | Solve | |
| Maximize Subarray GCD Score | Hard | Solve |