How to Count the Divisors of a Number From Its Prime Factors
Add one to each exponent and multiply: learn why the rule works, how to list and add divisors, and which puzzles it solves at once.
Key takeaways
- If n is a product of distinct primes raised to exponents a, b, c and so on, then n has (a + 1)(b + 1)(c + 1) divisors.
- Each divisor corresponds to one independent choice of exponent for every prime, from zero up to the exponent in n.
- A number has an odd number of divisors exactly when it is a perfect square.
- The primes themselves do not affect the count, only their exponents do.
To count the positive divisors of a number, factor it into primes, add 1 to each exponent and multiply the results. For 360 = 2³ × 3² × 5¹ the count is (3 + 1) × (2 + 1) × (1 + 1) = 4 × 3 × 2 = 24.
You never have to list the divisors to know how many there are. The same factorization also lets you list them in order, add them up, and settle puzzles about squares, rectangles and light switches.
The rule and the reason
A divisor of 360 can contain only the primes 2, 3 and 5, and no more copies of each than 360 has. This follows from unique factorization. So every divisor has the form 2ˣ × 3ʸ × 5ᶻ with
x = 0, 1, 2 or 3 4 choices
y = 0, 1 or 2 3 choices
z = 0 or 1 2 choices The choices are independent, so there are 4 × 3 × 2 = 24 combinations, and uniqueness of factorization guarantees that different combinations give different numbers. This is the basic multiplication principle of counting.
The "+1" is there for the exponent 0, the option of leaving a prime out entirely. Choosing 0 for all three gives the divisor 1. Choosing the maximum for all three gives 360 itself.
Listing all of them
Organize the choices as a grid. Take the powers of 2 across the top and the powers of 3 down the side, which gives all divisors of 72. Then multiply the whole grid by 5.
| × | 1 | 2 | 4 | 8 |
|---|---|---|---|---|
| 1 | 1 | 2 | 4 | 8 |
| 3 | 3 | 6 | 12 | 24 |
| 9 | 9 | 18 | 36 | 72 |
Multiplying each of these twelve entries by 5 gives 5, 10, 20, 40, 15, 30, 60, 120, 45, 90, 180 and 360. That makes 24 divisors with none missed and none repeated.
Divisors also come in partners whose product is n: 1 with 360, 2 with 180, 3 with 120, and so on up to 18 with 20. To find them all by trial, you need only test up to √n, here just under 19.
A method in steps
The step that takes real work is the first. To factor a modest number, divide by 2 as often as possible, then 3, 5, 7 and onward, stopping when the square of the next prime exceeds what is left. For very large numbers factoring is believed to be hard, and no shortcut to the divisor count is known that avoids it in general.
Restricted counts
Small changes to the choices answer more specific questions about 360.
- Odd divisors: force x = 0, leaving 3 × 2 = 6.
- Even divisors: require x of at least 1, giving 3 × 3 × 2 = 18.
- Multiples of 12: since 12 = 2² × 3, require x to be 2 or 3 and y to be 1 or 2, giving 2 × 2 × 2 = 8.
- Square divisors: all exponents even, so x is 0 or 2, y is 0 or 2 and z is 0, giving 4.
- Proper divisors: every divisor except 360 itself, so 23.
Each question becomes a matter of restricting some exponent and multiplying again.
Why squares are special
The count (a + 1)(b + 1)(c + 1) is odd only if every factor is odd, which means every exponent is even. That is exactly the condition for n to be a perfect square.
The partner picture shows the same thing. Divisors pair off as d and n ÷ d. A divisor is its own partner only when d² = n. So the count is odd precisely for squares. For 36 the divisors are 1, 2, 3, 4, 6, 9, 12, 18 and 36, nine in all, with 6 standing alone.
This solves a classic puzzle. A row of 100 lockers starts closed. Person 1 toggles every locker, person 2 toggles every second one, person 3 every third, up to person 100. Locker n is toggled once for each of its divisors, so it ends open exactly when that count is odd. The open lockers are 1, 4, 9, 16, 25, 36, 49, 64, 81 and 100.
Adding the divisors
The same independence gives their sum, usually written σ(n). Expanding the product below produces every divisor exactly once.
σ(360) = (1 + 2 + 4 + 8) × (1 + 3 + 9) × (1 + 5)
= 15 × 13 × 6
= 1170 Each bracket is a geometric series, equal to (pᵃ⁺¹ − 1) ÷ (p − 1) for the prime p with exponent a. This sum decides whether a number is perfect, deficient or abundant. Since 1170 is more than 2 × 360, the number 360 is abundant.
There is also a neat product formula. Multiplying all the divisors of n gives n raised to half the divisor count. For 12, with six divisors, 1 × 2 × 3 × 4 × 6 × 12 = 1728 = 12³. The reason is that the six divisors form three pairs, each multiplying to 12.
Designing numbers with a given count
Run the rule backward. What is the smallest number with exactly 12 divisors? The count 12 can factor as 12, 6 × 2, 4 × 3 or 3 × 2 × 2, giving different exponent patterns. To keep the number small, put the largest exponent on the smallest prime.
| Pattern of exponents | Smallest example |
|---|---|
| 11 | 2¹¹ = 2048 |
| 5, 1 | 2⁵ × 3 = 96 |
| 3, 2 | 2³ × 3² = 72 |
| 2, 1, 1 | 2² × 3 × 5 = 60 |
The winner is 60. A number with more divisors than every smaller positive number is called highly composite, and the list begins 1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360. Srinivasa Ramanujan wrote a substantial study of them, published in 1915. Their wealth of divisors helps explain why numbers such as 12, 60 and 360 are convenient for dividing hours and circles, though the history of those units involves more than one cause.
Common divisors of two numbers
The rule also counts the divisors that two numbers share. A number divides both exactly when it divides their greatest common divisor, so find that first and then count its divisors.
For 360 and 84, the greatest common divisor is 12 = 2² × 3. It has (2 + 1) × (1 + 1) = 6 divisors: 1, 2, 3, 4, 6 and 12. Those are all the common divisors, and you can get the greatest common divisor without factoring by using the Euclidean algorithm.
A related count is the number of ways to write n as a product of two coprime positive factors, in order. Each prime must go wholly to one side or the other, so with r different primes there are 2ʳ ways. For 360, with three primes, there are 8 ordered pairs, or 4 if the order is ignored: 1 × 360, 8 × 45, 5 × 72 and 9 × 40.
Equations that reduce to counting
Some problems hide a divisor count. How many pairs of positive integers x and y satisfy 1/x + 1/y = 1/12?
Multiply by 12xy to get 12y + 12x = xy. Rearranging gives (x − 12)(y − 12) = 144. Since each of x and y must exceed 12, both brackets are positive. So x − 12 can be any positive divisor of 144, and y − 12 is its partner. As 144 = 2⁴ × 3², there are 5 × 3 = 15 ordered pairs.
For example, x − 12 = 9 gives x = 21 and y = 28, and 1/21 + 1/28 = 4/84 + 3/84 = 7/84 = 1/12. The same technique counts the solutions with any n in place of 12: the answer is the number of divisors of n².
To count the divisors of a square, double every exponent before adding 1. If n = 2² × 3, then n² = 2⁴ × 3², with no need to compute n² at all.
Counting rectangles and factor pairs
How many different rectangles with whole-number sides have area 360? Each corresponds to a pair of partner divisors, and turning a rectangle does not make a new one. With 24 divisors there are 12 rectangles.
For a square number the self-partnered divisor gives one extra shape. The area 36 has nine divisors and five rectangles: 1 by 36, 2 by 18, 3 by 12, 4 by 9 and 6 by 6.
How large can the count be?
The count jumps erratically from one number to the next. The number 359 is prime and has 2 divisors, its neighbor 360 has 24, and 361 = 19² has 3. On average, though, the behavior is smooth: the average number of divisors of the integers up to N grows roughly like the natural logarithm of N.
The function that counts divisors, often written τ(n) or d(n), is multiplicative: if m and n share no prime factor, then τ(mn) = τ(m) × τ(n). That fails otherwise. τ(2) × τ(4) = 2 × 3 = 6, while τ(8) = 4. Such functions are studied together in arithmetic functions.
Checking an answer without the formula
For a small number you can confirm a count by hunting partners. Test each d from 1 up to √n and record d together with n ÷ d whenever the division is exact. For 84 the successful values are 1, 2, 3, 4, 6 and 7, with partners 84, 42, 28, 21, 14 and 12. That is 12 divisors.
The formula agrees: 84 = 2² × 3 × 7 gives 3 × 2 × 2 = 12. When the two methods disagree, the usual cause is a factorization that stopped too early or a partner pair that was skipped. A program can run the same comparison over thousands of numbers, a quick way to test your own code.
Common mistakes
- Multiplying the exponents without adding 1. That would give 6 for 360 in place of 24.
- Using factors that are not prime. Writing 360 = 8 × 9 × 5 and counting each as exponent 1 gives 8, which is wrong.
- Repeating a prime in separate places, as in 2² × 2 × 3² × 5. Combine them into 2³ first.
- Forgetting that the count includes both 1 and n.
- Counting negative divisors by accident. If they are wanted, double the total.
Practice
Find the number of divisors of 2025. Since 2025 = 3⁴ × 5², the count is 5 × 3 = 15. The count is odd, so 2025 must be a square, and indeed it is 45².
Now try 2026, which equals 2 × 1013, where 1013 is prime. Its count is 2 × 2 = 4: the divisors are 1, 2, 1013 and 2026. Neighboring numbers can be built very differently.
A last one asks you to think backward. Which numbers have exactly 3 divisors? The count 3 cannot be split into smaller factors, so there is a single prime with exponent 2. The answer is the squares of primes: 4, 9, 25, 49 and so on. By the same reasoning, a number with exactly 5 divisors is the fourth power of a prime, such as 16 or 81.
Why the primes themselves drop out
The numbers 360, 600 and 1176 all have exponents 3, 2 and 1 in some order, since 600 = 2³ × 3 × 5² and 1176 = 2³ × 3 × 7². Each therefore has 24 divisors. The divisors differ, but their pattern of divisibility is identical: you could draw one diagram of which divides which and simply relabel it. That diagram is a three-dimensional grid, 4 by 3 by 2, and counting its points is all the formula does.