What is the best way to get all the divisors of a number?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you need all divisors of one positive integer, the best practical method is to scan only up to the square root and collect divisor pairs. That gives the same result as checking every number from 1 to n, but it cuts the number of divisibility tests from linear time down to square-root time.
Use Divisor Pairs Around the Square Root
Divisors naturally come in pairs. If d divides n, then n // d also divides n. That means once you have checked every candidate up to sqrt(n), you have found every divisor pair already.
For 36, the pairs are 1 and 36, 2 and 18, 3 and 12, 4 and 9, and 6 and 6. After the square root, every new divisor would just mirror one you already found.
Baseline and Efficient Implementation
The naive method is easy to understand, but it does too much work.
A better implementation uses the divisor-pair idea directly.
This returns sorted divisors and avoids duplicating the square root for perfect squares.
Why This Is Faster
The naive solution performs n modulus operations. The square-root method performs about sqrt(n) checks. For a number around one million, that means roughly one thousand checks instead of one million.
The memory cost is modest because you store only the divisors you actually find. For typical values, the divisor count is tiny compared with the size of the number itself.
Counting Divisors Instead of Listing Them
Sometimes you need only the count, not the list. In that case, prime factorization is often the right tool. If
n = p1^a1 * p2^a2 * ...
then the number of positive divisors is
(a1 + 1) * (a2 + 1) * ...
Here is a simple Python implementation.
That is useful when a problem asks how many divisors exist rather than what they are.
Handle Edge Cases Explicitly
You should decide what your function means for special inputs. 1 has one positive divisor, which is 1. 0 is usually rejected in programming utilities because every nonzero integer divides 0, so the divisor set is not finite. Negative numbers are domain-specific. Some programs return divisors of the absolute value, while others reject negatives entirely.
Clear input rules matter more than clever code here.
Common Pitfalls
- Checking every number up to
nwhen scanning tosqrt(n)is enough. - Forgetting to append the paired divisor
n // dafter findingd. - Duplicating the square-root divisor for perfect squares.
- Returning divisors in an unexpected order because the large partners were not reversed.
- Failing to define behavior for
0,1, or negative input values.
Summary
- The best general method for one number is scanning up to the square root.
- Divisors come in pairs, so each successful check yields one or two results.
- This reduces time from
O(n)toO(sqrt(n)). - Prime factorization is useful when you need divisor counts.
- Explicit edge-case handling makes the function predictable and reusable.

