Quant Memo
Core

Counting Divisors From a Prime Factorisation

A standard interview trick — once a number is broken into its prime factors, the count of its divisors falls out of a single formula, no listing required.

"How many divisors does 360 have?" is a common interview warm-up, and listing them by hand is slow and error-prone once the number gets past a couple dozen. The clean approach is to first write the number as a product of prime powers, then use a formula that converts that factorisation directly into a divisor count.

If a number NN factors as p1a1×p2a2××pkakp_1^{a_1} \times p_2^{a_2} \times \cdots \times p_k^{a_k}, where each pip_i is a distinct prime, then the number of positive divisors of NN is (a1+1)(a2+1)(ak+1)(a_1+1)(a_2+1)\cdots(a_k+1). The reasoning is that any divisor of NN is built by picking, independently for each prime, one of ai+1a_i + 1 possible exponents (0 through aia_i), and multiplying the choices together — the count of divisors is just the count of these independent choices multiplied out.

For N=360N = 360: factorise it as 23×32×512^3 \times 3^2 \times 5^1. The exponents are 3, 2, and 1, so the divisor count is (3+1)(2+1)(1+1)=4×3×2=24(3+1)(2+1)(1+1) = 4 \times 3 \times 2 = 24. Indeed, 360 has exactly 24 positive divisors — a fact that would take real patience to verify by listing them one at a time.

The same trick extends naturally: interviewers sometimes ask for the number of divisors that are themselves perfect squares, or the sum of all divisors (a related but different formula, piai+11pi1\prod \frac{p_i^{a_i+1}-1}{p_i-1}), so recognising that the whole family of divisor questions reduces to "factorise first" is the transferable skill, not memorising one specific formula.

Once a number is written as a product of prime powers p1a1pkakp_1^{a_1} \cdots p_k^{a_k}, its total number of divisors is simply (a1+1)(a2+1)(ak+1)(a_1+1)(a_2+1)\cdots(a_k+1) — turning a tedious listing exercise into one multiplication.

Practice in interviews

Further reading

  • Standard quant interview puzzle collections
ShareTwitterLinkedIn