Three Divisors - Problem
Three Divisors is a fascinating number theory problem that asks you to identify special numbers with exactly three positive divisors.

Given an integer n, return true if n has exactly three positive divisors, otherwise return false.

A divisor of n is any positive integer m where n = k × m for some integer k. For example, the divisors of 12 are: 1, 2, 3, 4, 6, and 12.

Key Insight: Numbers with exactly three divisors have a very special property - they are squares of prime numbers! This is because a prime squared (p²) has divisors: 1, p, and p².

Input & Output

example_1.py — Basic Case
$ Input: n = 9
Output: true
💡 Note: The divisors of 9 are: 1, 3, and 9. Since there are exactly 3 divisors, return true. Note that 9 = 3², which is the square of prime 3.
example_2.py — Non-prime Square
$ Input: n = 12
Output: false
💡 Note: The divisors of 12 are: 1, 2, 3, 4, 6, and 12. Since there are 6 divisors (not 3), return false.
example_3.py — Edge Case
$ Input: n = 1
Output: false
💡 Note: The only divisor of 1 is 1 itself. Since there is only 1 divisor (not 3), return false.

Visualization

Tap to expand
Mathematical Pattern: Prime SquaresPrime: 3Divisors: 1, 3Count: 23Square: 3² = 9Divisors: 1, 3, 9Count: 3 ✓Composite: 4² = 16Divisors: 1,2,4,8,16Count: 5 ✗4 is not primeSquare itKey InsightOnly squares of prime numbershave exactly 3 divisors!
Understanding the Visualization
1
Prime Property
A prime p has exactly 2 divisors: 1 and p
2
Square of Prime
p² has divisors: 1, p, and p² (exactly 3)
3
Non-prime Squares
If n = ab where a,b > 1, then n² has more divisors
4
Verification
Check if n is perfect square and √n is prime
Key Takeaway
🎯 Key Insight: Numbers with exactly three divisors are always squares of prime numbers (p²), which have divisors 1, p, and p².

Time & Space Complexity

Time Complexity
⏱️
O(√n)

We only check divisors up to the square root of n

n
2n
Linear Growth
Space Complexity
O(1)

Only using a counter variable and loop variables

n
2n
Linear Space

Constraints

  • 1 ≤ n ≤ 104
  • n is a positive integer
  • The solution should handle edge cases like n = 1
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
24.7K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen