GeeksforGeeks Easy

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 n is a whole number d with n % d == 0. Divisors are also called the factors of a number.
  • n goes up to 10⁹. To print all divisors of a number that large, trying every d from 1 to n would take a billion checks. The expected time is O(√n).
Constraints
  • 1 <= n <= 10⁹
mathsdivisorssqrt
Open on GeeksforGeeks ↗
02

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.

03

Approach

1

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.

2

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.

3

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def getDivisors(self, n):
▶3 small, large = [], []
▶4 i = 1
▶5 while i * i <= n:
▶6 if n % i == 0:
▶7 small.append(i)
▶8 if i != n // i:
▶9 large.append(n // i)
▶10 i += 1
▶11 return small + large[::-1]
test36÷1=36remainder 0smallpartner136sorted36 ÷ 1 = 36 exactly → keep 1 and 36
i1divides 36
small1
partners36
1 divides 36, and so does its partner 36, since 1 × 36 = 36. One check finds both divisors. 1 goes in the small list, 36 in the partner list.
test36÷2=18remainder 0smallpartner136218sorted36 ÷ 2 = 18 exactly → keep 2 and 18
i2divides 36
small1, 2
partners36, 18
2 divides 36, and so does its partner 18, since 2 × 18 = 36. One check finds both divisors. 2 goes in the small list, 18 in the partner list.
test36÷3=12remainder 0smallpartner136218312sorted36 ÷ 3 = 12 exactly → keep 3 and 12
i3divides 36
small1, 2, 3
partners36, 18, 12
3 divides 36, and so does its partner 12, since 3 × 12 = 36. One check finds both divisors. 3 goes in the small list, 12 in the partner list.
test36÷4=9remainder 0smallpartner13621831249sorted36 ÷ 4 = 9 exactly → keep 4 and 9
i4divides 36
small1, 2, 3, 4
partners36, 18, 12, 9
4 divides 36, and so does its partner 9, since 4 × 9 = 36. One check finds both divisors. 4 goes in the small list, 9 in the partner list.
test36÷5=7remainder 1smallpartner13621831249sorted36 ÷ 5 leaves 1 → 5 is not a divisor
i55 × 5 = 25 ≤ 36
remainder1
36 ÷ 5 leaves a remainder of 1, so 5 is not a divisor, and neither is any partner it would have had. Move on to 6.
test36÷6=6remainder 0smallpartner136218312496samesorted6 × 6 = 36 → keep 6 only once
i6divides 36
small1, 2, 3, 4, 6
partners36, 18, 12, 9
6 divides 36 and its partner is 6 itself, because 36 is a perfect square. Adding it to both lists would print 6 twice, so it goes in once.
test7×7=49more than 36smallpartner136218312496samesorted7 × 7 = 49 is past 36 → stop
i77 × 7 = 49
checks made6not 36
Any divisor bigger than 6 would need a partner smaller than 7, and all of those have been tested already. So the loop stops after only 6 checks.
test36smallpartner136218312496samesorted123469121836small list, then partners reversed → ascending
small1, 2, 3, 4, 6increasing
partners36, 18, 12, 9decreasing
answer1, 2, 3, 4, 6, 9, 12, 18, 36
The small divisors were found in increasing order, so their partners came out decreasing. Reversing the partners and putting them after the small ones gives 1, 2, 3, 4, 6, 9, 12, 18, 36, already sorted.
05

Common pitfalls

Adding the square root twice

✗ Wrong
if n % i == 0:
    small.append(i)
    large.append(n // i)
✓ Right
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

✗ Wrong
if n % i == 0:
    out.append(i)
    if i != n // i:
        out.append(n // i)
✓ Right
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.

06

Edge cases

n = 1

The loop runs once with i = 1, and 1 / 1 = 1 is the same number, so the answer is just [1].

n is prime

Only i = 1 divides it, giving the pair 1 and n. For n = 21191 the answer is [1, 21191] after about 145 checks.

07

Complexity

Time
O(√n)
Space
O(√n)
The loop runs about √n times, and the two lists together hold the divisors, at most 1,344 of them for n ≤ 10⁹. Reversing and joining costs only that much more.
08

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.