All Divisors of Number
All Divisors of Number is a GFG problem (Easy). Given an integer n, return every divisor of n in ascending order.
- A divisor of
nis a whole numberdwithn % d == 0. Divisors are also called the factors of a number. ngoes up to 10⁹. To print all divisors of a number that large, trying everydfrom 1 tonwould take a billion checks. The expected time is O(√n).
- 1 <= n <= 10⁹
Intuition
Divisors come in pairs. If i divides n, so does n / i, because i × (n / i) = n. In each pair, one number is at most √n and the other is at least √n: if both were bigger, their product would be bigger than n.
So test only i = 1, 2, 3, … while i × i ≤ n. Each hit gives two divisors at once.
The order comes for free. The small divisors are found in increasing order, so their partners n / i come out in decreasing order. Reverse the partners and put them after the small ones.
Approach
Loop i while i × i ≤ n
Every divisor pair has its smaller member in this range, so no pair is missed. For n = 10⁹ that is about 31,623 checks instead of a billion. Writing the test as i <= n / i avoids computing i × i at all.
Keep both members of each pair
When n % i == 0, append i to a small list and n / i to a large list. If n is a perfect square, at i = √n the two are the same number, so add it only once.
Join the two lists
small is increasing and large is decreasing, and every number in small is at most √n while every number in large is at least √n. So small followed by large reversed is fully sorted, with no sort call needed.
Code in Python, C++ and Java
Common pitfalls
Adding the square root twice
if n % i == 0:
small.append(i)
large.append(n // i)if n % i == 0:
small.append(i)
if i != n // i:
large.append(n // i)For n = 36, i = 6 gives the pair 6 and 6, and the output becomes 1 2 3 4 6 6 9 12 18 36. Only perfect squares hit this, so tests with n = 12 or n = 20 will not catch it.
Putting both members in one list
if n % i == 0:
out.append(i)
if i != n // i:
out.append(n // i)small.append(i) large.append(n // i) ... return small + large[::-1]
Every divisor is found, but in the wrong order: n = 20 gives 1 20 2 10 4 5. The problem asks for ascending order, so the partners must go in their own list and be reversed at the end.
Edge cases
The loop runs once with i = 1, and 1 / 1 = 1 is the same number, so the answer is just [1].
Only i = 1 divides it, giving the pair 1 and n. For n = 21191 the answer is [1, 21191] after about 145 checks.
Complexity
n ≤ 10⁹. Reversing and joining costs only that much more.All Divisors of Number FAQ
How many divisors can a number up to 10⁹ have?
At most 1,344, reached by 735,134,400. Divisor counts grow far more slowly than n, which is why the output list stays small even when n is huge.
How do you count divisors without listing them?
Factor n into primes, n = p₁^a₁ × p₂^a₂ × …. The count is (a₁ + 1)(a₂ + 1)…, because each divisor picks an exponent from 0 to aᵢ for every prime. For 36 = 2² × 3² that is 3 × 3 = 9.
Is a divisor the same as a prime factor?
No. The divisors of 12 are 1, 2, 3, 4, 6 and 12; its prime factors are only 2 and 3. Every divisor is a product of prime factors, which is why counting the exponents of each prime gives the number of divisors.