Algorithm to find all the exact divisors of a given integer
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding all exact divisors of an integer n means identifying every integer that divides n with zero remainder. The naive approach checks every number from 1 to n, but a much faster method only iterates up to the square root of n, collecting divisor pairs. This reduces the time complexity from O(n) to O(sqrt(n)). Divisor algorithms are foundational in number theory and appear in problems involving GCD, LCM, prime factorization, and cryptography.
The Square Root Algorithm
For any divisor i of n, the complementary divisor n / i is also a divisor. By iterating only up to sqrt(n), you find both divisors in each step:
For n = 36, sqrt(36) = 6. The loop checks 1 through 6:
i=1: 36 % 1 == 0, pair (1, 36)i=2: 36 % 2 == 0, pair (2, 18)i=3: 36 % 3 == 0, pair (3, 12)i=4: 36 % 4 == 0, pair (4, 9)i=5: 36 % 5 != 0, skipi=6: 36 % 6 == 0, pair (6, 6) — same number, add only once
Implementations in Other Languages
Java
C++
JavaScript
Counting Divisors
If you only need the count rather than the list:
Divisors via Prime Factorization
For very large numbers, find the prime factorization first, then compute divisors from the exponents:
The number of divisors of n equals the product of (exponent + 1) for each prime factor. For 36 = 2^2 * 3^2, the divisor count is (2+1) * (2+1) = 9.
Special Cases
Finding Divisors of All Numbers in a Range (Sieve)
When you need divisors for every number up to N, a sieve is more efficient than calling get_divisors N times:
This runs in O(N log N) time, which is faster than O(N * sqrt(N)) for computing divisors of all numbers from 1 to N.
Common Pitfalls
- Iterating up to n instead of sqrt(n): Checking all numbers from 1 to n is O(n). Using the square root optimization reduces this to O(sqrt(n)), which matters for large inputs like n = 10^12.
- Duplicate divisor for perfect squares: When n is a perfect square,
iandn/iare the same ati = sqrt(n). Forgetting thei != n // icheck adds a duplicate. - Integer overflow with large n: In languages like C++ or Java,
i * ican overflow for large n. Use(long)i * i <= nor comparei <= n / iinstead. - Not handling n = 1: The number 1 has exactly one divisor (itself). The algorithm handles this correctly, but some applications assume at least two divisors.
- Returning unsorted results: The square root method collects small divisors and large divisors out of order. Sort the result if order matters, or use two lists (small and large) and merge them.
Summary
- Iterate from 1 to sqrt(n), checking
n % i == 0to find divisor pairs (i, n/i) - Time complexity is O(sqrt(n)), which is optimal for a single number
- Check
i != n // ito avoid duplicate entries for perfect squares - For many numbers in a range, use a sieve approach in O(N log N)
- The divisor count equals the product of (exponent + 1) for each prime factor

