Sub topic 5
Counting the factors of a number
This chapter assumes you can write a number as a product of prime powers, such as 360 = 2³ × 3² × 5. It also assumes you know what a factor is. Here you will turn that prime form into a count of factors. You will then extend the method to constrained counts, such as odd factors, factors that are multiples of a given number, and factors of a power. Finally you will see where CAT questions try to trip you up.
The core formula
Suppose N = p₁^e₁ × p₂^e₂ × ... × pₖ^eₖ, where each pᵢ is a distinct prime. A positive integer d divides N exactly when d uses only these primes and each exponent in d is at most the matching exponent in N.
So building a factor means making one independent choice per prime. For the prime pᵢ you pick an exponent from 0, 1, ..., eᵢ, which gives eᵢ + 1 options. Independent choices multiply:
Number of factors = (e₁ + 1)(e₂ + 1) ... (eₖ + 1)
For 360 = 2³ × 3² × 5¹, the count is 4 × 3 × 2 = 24.
Here are some worked examples.
| N | Prime form | Count calculation | Factors |
|---|---|---|---|
| 1 | (empty product) | empty product = 1 | 1 |
| 97 | 97¹ | 1 + 1 | 2 |
| 64 | 2⁶ | 6 + 1 | 7 |
| 12 | 2² × 3 | 3 × 2 | 6 |
| 36 | 2² × 3² | 3 × 3 | 9 |
| 360 | 2³ × 3² × 5 | 4 × 3 × 2 | 24 |
| 1260 | 2² × 3² × 5 × 7 | 3 × 3 × 2 × 2 | 36 |
Two edge cases follow from the formula. The number 1 has exactly one factor. Every prime has exactly two factors, 1 and itself.
Checking the formula in code
This program counts factors both ways, once with the formula and once by brute force. The two must agree.
def factorize(n):
f = {}
p = 2
while p * p <= n:
while n % p == 0:
f[p] = f.get(p, 0) + 1
n //= p
p += 1
if n > 1:
f[n] = f.get(n, 0) + 1
return f
def count_factors(n):
count = 1
for e in factorize(n).values():
count *= e + 1
return count
def brute_count(n):
return sum(1 for d in range(1, n + 1) if n % d == 0)
for n in [1, 12, 36, 97, 360, 1260]:
assert count_factors(n) == brute_count(n)
print(n, count_factors(n))
# 1 1
# 12 6
# 36 9
# 97 2
# 360 24
# 1260 36Counting only some of the factors
CAT rarely asks for the plain total. It usually adds a condition. The method is the same each time: restrict the range of exponents for each prime, then multiply the number of choices.
Odd and even factors
An odd factor cannot contain the prime 2, so its exponent of 2 must be 0. For 360, the odd factors come from 3² × 5¹. That gives 3 × 2 = 6 odd factors.
An even factor needs the exponent of 2 to be at least 1. For 360 the exponent of 2 has 3 choices (1, 2 or 3), so the count is 3 × 3 × 2 = 18. You can also subtract: 24 − 6 = 18.
Factors that are multiples of a given number
To count factors of N that are multiples of m, force each prime's exponent to be at least its exponent in m. The upper limit stays at N's exponent.
Take the factors of 360 that are multiples of 6 = 2¹ × 3¹:
- The exponent of 2 runs from 1 to 3, which is 3 choices.
- The exponent of 3 runs from 1 to 2, which is 2 choices.
- The exponent of 5 runs from 0 to 1, which is 2 choices.
The count is 3 × 2 × 2 = 12. A quick check: this equals the number of factors of 360 ÷ 6 = 60 = 2² × 3 × 5, which is 3 × 2 × 2 = 12. So when m divides N, the factors of N that are multiples of m number the same as the factors of N/m.
Factors that are perfect squares
A perfect square has only even exponents. For each prime, count the even numbers from 0 up to eᵢ, which is ⌊eᵢ/2⌋ + 1 choices. For 360, the exponent of 2 gives {0, 2}, which is 2 choices. The exponent of 3 gives {0, 2}, which is 2 choices. The exponent of 5 gives {0}, which is 1 choice. The count is 2 × 2 × 1 = 4. Those factors are 1, 4, 9 and 36.
Factors of a power or a product
Do not count the factors of N² by squaring the count for N. Double the exponents first. Since 360² = 2⁶ × 3⁴ × 5², it has 7 × 5 × 3 = 105 factors, not 24² = 576.
The same care applies to products. For 12 × 18, merge the exponents before counting:
- 12 = 2² × 3
- 18 = 2 × 3²
- 12 × 18 = 2³ × 3³, so it has 4 × 4 = 16 factors.
Multiplying the separate counts, 6 × 6 = 36, gives the wrong answer. Shared primes must be merged first.
Working backward from a count
A common CAT question asks for the smallest number with a given count of factors. Write the target count as a product of integers, each at least 2. Subtract 1 from each integer to get exponents. Assign the largest exponents to the smallest primes, then compare the candidates.
For exactly 12 factors, the possible factorizations of 12 and the smallest number from each are:
| Split of 12 | Exponents | Smallest number |
|---|---|---|
| 12 | 11 | 2¹¹ = 2048 |
| 6 × 2 | 5, 1 | 2⁵ × 3 = 96 |
| 4 × 3 | 3, 2 | 2³ × 3² = 72 |
| 3 × 2 × 2 | 2, 1, 1 | 2² × 3 × 5 = 60 |
The smallest number with exactly 12 factors is 60.
Two facts help when you read a count backward:
- A count that is a prime p forces N = q^(p−1) for some prime q, because p cannot be split any further.
- An odd count means every factor of the form (e + 1) is odd, so every exponent is even. Therefore N is a perfect square.
Reading the wording carefully
The phrase "factors" usually includes 1 and N. Questions often shift the meaning, and the answer changes with it.
| Wording | Count for 360 |
|---|---|
| Factors | 24 |
| Proper factors (excluding N itself) | 23 |
| Factors other than 1 and N | 22 |
| Prime factors (distinct) | 3 |
| Composite factors | 24 − 1 − 3 = 20 |
For the composite count, remove 1, the three prime factors (2, 3 and 5), leaving the composites. The number 360 itself is composite, so it stays in.
For example, 360 has 24 factors and is not a perfect square, so it can be written as a product of two factors in 12 unordered ways. The number 36 has 9 factors and is a perfect square, so it has (9 + 1)/2 = 5 unordered pairs: 1 × 36, 2 × 18, 3 × 12, 4 × 9 and 6 × 6.
Common errors
- Forgetting the +1. The exponent 3 gives four choices, not three. The exponent 0 is a legal choice.
- Counting from a non-prime factorization. Splitting 360 as 6 × 60 or 18 × 20 does not work. Only distinct primes give independent choices.
- Treating shared primes as separate. When two numbers are multiplied, merge their exponents of the same prime before applying the formula.
- Misreading the question. Check whether 1, N, or both are excluded, and whether the question asks about factors, prime factors or proper factors.
- Squaring the count for N². Double the exponents instead.
Exercise
How many factors of 2⁴ × 3³ × 7² are multiples of 6?
Show answer
A multiple of 6 needs the exponent of 2 to be at least 1 and the exponent of 3 to be at least 1. The exponent of 2 has choices 1 to 4, which is 4 options. The exponent of 3 has choices 1 to 3, which is 3 options. The exponent of 7 has choices 0 to 2, which is 3 options. The count is 4 × 3 × 3 = 36.
Exercise
How many odd factors does 1260 have, and how many of its factors are even?
Show answer
1260 = 2² × 3² × 5 × 7. Odd factors use the exponent 0 for 2, so they come from 3² × 5 × 7, giving 3 × 2 × 2 = 12. The total is 3 × 3 × 2 × 2 = 36, so the even factors number 36 − 12 = 24.
Exercise
Find the smallest positive integer with exactly 10 factors.
Show answer
Split 10 as 10 or 5 × 2. The split 10 gives the exponent 9, so 2⁹ = 512. The split 5 × 2 gives exponents 4 and 1, so 2⁴ × 3 = 48. The smallest is 48.