Algorithm to calculate the number of divisors of a given number
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Counting the divisors of an integer is a classic number-theory problem with direct algorithmic solutions. The simplest method checks all numbers up to n, but the more useful approaches either exploit divisor pairs up to sqrt(n) or use prime factorization to compute the divisor count directly.
The Pairing Insight
If d divides n, then n / d also divides n. Divisors therefore come in pairs around the square root.
For example, divisors of 36 include:
- '
1and36' - '
2and18' - '
3and12' - '
4and9' - '
6and6'
This means you only need to test candidates up to sqrt(n), not all the way up to n.
Square-Root Counting Algorithm
A direct optimized algorithm is:
- loop
ifrom1tosqrt(n) - if
idividesn, count one divisor pair - if
i * i == n, count only one instead of two
This runs in O(sqrt(n)) time and is often good enough for single queries.
Prime Factorization Formula
If you need a more number-theoretic method, factor the number into primes.
If:
- '
n = p1^a1 * p2^a2 * ... * pk^ak'
then the number of positive divisors is:
- '
(a1 + 1) * (a2 + 1) * ... * (ak + 1)'
For example:
- '
36 = 2^2 * 3^2' - divisor count =
(2 + 1) * (2 + 1) = 9
That works because every divisor is formed by choosing the exponent of each prime independently from 0 through the prime’s exponent in n.
Factorization-Based Implementation
This is especially nice when you also want the prime factorization for other work.
Which Method Should You Use
A practical rule is:
- use the square-root loop for simple one-off calculations
- use factorization if you also need prime exponents or many related number-theory results
Both are vastly better than testing every number up to n.
Complexity Notes
The square-root counting method is clearly O(sqrt(n)). The factorization method is also O(sqrt(n)) in a basic implementation, though its constant factors differ and it often feels more elegant mathematically.
If you must answer divisor-count queries for many numbers, preprocessing with a sieve can be even better than factoring each number independently.
Common Pitfalls
The most common mistake is double-counting the square root for perfect squares. For 36, the divisor 6 should be counted once, not twice.
Another mistake is using the factorization formula without fully decomposing the number into prime powers. The formula applies to prime exponents, not to arbitrary factors.
A third issue is ignoring edge cases such as n = 1. The number 1 has exactly one positive divisor: itself.
Summary
- Divisors come in pairs around
sqrt(n). - A simple optimized loop counts divisors in
O(sqrt(n))time. - Prime factorization gives a direct divisor-count formula using exponents.
- Perfect squares need special handling so the square root is not double-counted.
- Choose the method based on whether you need just the count or also the factorization.

