Three Divisors - Problem

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.

Input & Output

Example 1 — Perfect Square of Prime
$ Input: n = 4
Output: true
💡 Note: 4 = 2², and 2 is prime. The divisors of 4 are: 1, 2, 4 (exactly 3 divisors)
Example 2 — Not a Square of Prime
$ Input: n = 6
Output: false
💡 Note: The divisors of 6 are: 1, 2, 3, 6 (4 divisors, not 3)
Example 3 — Another Square of Prime
$ Input: n = 9
Output: true
💡 Note: 9 = 3², and 3 is prime. The divisors of 9 are: 1, 3, 9 (exactly 3 divisors)

Constraints

  • 1 ≤ n ≤ 104

Visualization

Tap to expand
Three Divisors Problem Overviewn = 4InputDivisors:1, 2, 4Count: 3Check:3 = 3 ✓trueResultKey Pattern: Numbers with exactly 3 divisors are squares of primesExamples: 4=2², 9=3², 25=5², 49=7²
Understanding the Visualization
1
Input
Given integer n to analyze
2
Count Divisors
Find all positive divisors of n
3
Check Count
Return true if exactly 3 divisors found
Key Takeaway
🎯 Key Insight: Only perfect squares of prime numbers have exactly 3 divisors: 1, the prime, and its square
Asked in
Google 15 Amazon 12 Microsoft 8
25.0K Views
Medium Frequency
~15 min Avg. Time
890 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