Better solution for finding numbers with exactly 3 divisors
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Numbers with exactly three positive divisors have a very specific structure. Once you know that structure, you can stop checking divisors one number at a time and replace a slow brute-force search with a much faster prime-based solution.
The Key Number Theory Fact
A number has exactly three divisors if and only if it is the square of a prime.
Why? If n = p^2 and p is prime, then the divisors are:
- '
1' - '
p' - '
p^2'
There are exactly three.
If a number is not the square of a prime, it cannot have exactly three divisors. For example:
- '
16 = 4^2, but4is not prime, and16has more than three divisors' - '
18is not a square, so its divisors come in more complicated factor pairs'
That observation turns the problem from "count divisors for every number" into "find primes, then square them."
Brute Force Versus Better Approach
A naive algorithm checks every number and counts its divisors:
This is correct but extremely inefficient.
A better approach is:
- generate primes up to
sqrt(limit) - square each prime
- keep the squares that are within the range
Efficient Solution With a Sieve
Output:
Those are exactly the numbers up to 100 with three divisors.
Counting Instead of Listing
Sometimes the problem asks only for the count. Then you do not even need to build the squares explicitly. You only need the number of primes less than or equal to sqrt(limit):
This is especially useful when the range is large and you only care about the answer size.
Querying a Single Number
If the question is "does this one number have exactly three divisors?", use the same theorem:
This is much faster than counting all divisors of n.
Why This Is Better
The improvement comes from using structure. Instead of examining every possible divisor for every number, you reduce the problem to primality.
That gives you:
- simpler reasoning
- better performance
- a solution that scales cleanly
This is a good example of how number theory often replaces brute force with one strong observation.
Common Pitfalls
The most common mistake is forgetting the "prime" part and assuming every perfect square has exactly three divisors. Only squares of primes qualify.
Another mistake is using floating-point square roots for large values and then trusting equality checks. Integer square root functions are safer.
A third issue is writing a divisor-counting loop that is technically correct but far too slow for large input sizes.
Summary
- A number has exactly three divisors if and only if it is the square of a prime.
- That fact lets you avoid brute-force divisor counting.
- For ranges, generate primes up to
sqrt(limit)and square them. - For a single number, check whether it is a perfect square whose root is prime.
- The efficient solution comes from number theory, not from micro-optimizing the brute-force loop.
Related reading
- Big-O analysis of permutation algorithm
- Binary Space Partitioning Data Structure For Donut 2D Space
- Bitwise shift to generate all possible permutations in C
- Bomb dropping algorithm
- Bowyer-Watson algorithm how to fill holes left by removing triangles with super triangle vertices
- Bron-Kerbosch algorithm for clique finding
- Brute-force, single-threaded prime factorization
- Building bridges problem - how to apply longest increasing subsequence?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.