Sub topic 9
Why trailing zeros depend on the factors 2 and 5
This chapter assumes you can factorise integers into primes, apply the laws of exponents, and divide with remainders. It builds the one idea behind every trailing-zero question in CAT Quant: a number's trailing zeros are decided by how many pairs of the prime factors 2 and 5 it contains.
What a trailing zero is
A trailing zero is a zero at the right-hand end of a number's decimal form. The number 640000 has four trailing zeros. The number 100405 has none, because it ends in 5. Zeros inside the number, such as the ones in 100405, do not count.
In the language of the first chapter, a number has at least k trailing zeros exactly when it is divisible by 10^k, that is, when its remainder mod 10^k is 0. So "how many trailing zeros" means "what is the largest k such that 10^k divides N".
Why 10 forces the primes 2 and 5
Since 10 = 2 × 5, we have 10^k = 2^k × 5^k. For 10^k to divide N, the prime factorisation of N must contain at least k twos and at least k fives. Write
N = 2^a × 5^b × m
where m has no factor of 2 or 5. Then:
- N is divisible by 10^k if and only if a ≥ k and b ≥ k.
- The largest k that satisfies both is min(a, b).
So the number of trailing zeros of N is min(a, b), the smaller of the exponent of 2 and the exponent of 5.
Why the count is exactly min(a, b)
Let t = min(a, b). Then N = 10^t × C, where C is what remains after removing t twos and t fives. Either a = t or b = t, so C has no factor of 2 or no factor of 5. In both cases C is not divisible by 10, so its last digit is not 0. Multiplying C by 10^t appends exactly t zeros to it. Therefore N has exactly t trailing zeros.
A worked example
Find the trailing zeros of N = 1200.
- Factorise: 1200 = 12 × 100 = (2^2 × 3) × (2^2 × 5^2) = 2^4 × 3 × 5^2.
- Read the exponents: a = 4, b = 2.
- Take the minimum: min(4, 2) = 2.
Check: 1200 ends in "00", so it has two trailing zeros. The two spare factors of 2 and the factor 3 form the cofactor 2^2 × 3 = 12, and 1200 = 12 × 10^2.
Which factor limits the count
Only matched pairs of one 2 and one 5 make a 10. Unmatched factors do nothing for the zero count.
| Number | Factorisation | a (twos) | b (fives) | Trailing zeros |
|---|---|---|---|---|
| 1200 | 2^4 × 3 × 5^2 | 4 | 2 | 2 |
| 625000 | 2^3 × 5^7 | 3 | 7 | 3 |
| 640000 | 2^10 × 5^4 | 10 | 4 | 4 |
| 1024 | 2^10 | 10 | 0 | 0 |
| 4500 | 2^2 × 3^2 × 5^3 | 2 | 3 | 2 |
Look at 625000 = 8 × 78125. It contains seven fives but only three twos, so the twos are the bottleneck and the number has 3 zeros. In 640000 the fives are the bottleneck. In 1024 there are no fives, so no zeros appear no matter how many twos there are.
Reading the exponents of a product
You rarely need to multiply out a product before counting zeros. The exponent of a prime in a product is the sum of its exponents in the factors. This follows from the law a^x × a^y = a^(x+y).
Find the trailing zeros of 15 × 18 × 40.
- 15 = 3 × 5, so it contributes 0 twos and 1 five.
- 18 = 2 × 3^2, so it contributes 1 two and 0 fives.
- 40 = 2^3 × 5, so it contributes 3 twos and 1 five.
The product has a = 0 + 1 + 3 = 4 and b = 1 + 0 + 1 = 2. The answer is min(4, 2) = 2.
Check: 15 × 18 × 40 = 270 × 40 = 10800, which has two trailing zeros.
Notice that the zeros of the whole product are not the sum of the zeros of each factor. Only 40 ends in a zero, yet the product has two zeros, because the 5 in 15 pairs with the 2 in 18. Always pool the primes across all factors before pairing them.
Powers
A power multiplies every exponent. If N = 2^a × 5^b × m, then N^e = 2^(ae) × 5^(be) × m^e. So the trailing zeros of N^e are min(ae, be) = e × min(a, b). The count scales with the exponent, but only if you take the minimum on the original exponents first or multiply both by e. Both routes agree.
Example: 12^5 × 25^3.
- 12^5 = (2^2 × 3)^5 = 2^10 × 3^5.
- 25^3 = (5^2)^3 = 5^6.
So a = 10, b = 6, and the answer is min(10, 6) = 6.
Checking the method in code
The function below counts how many times a prime p divides n, then takes the minimum for p = 2 and p = 5. It stops with an error for n ≤ 0, because zero is divisible by every prime power and the loop would never end.
def prime_exponent(n, p):
if n <= 0:
raise ValueError("n must be a positive integer")
count = 0
while n % p == 0:
n //= p
count += 1
return count
def trailing_zeros(n):
return min(prime_exponent(n, 2), prime_exponent(n, 5))
def trailing_zeros_by_string(n):
s = str(n)
return len(s) - len(s.rstrip("0"))
for n in (1200, 625000, 640000, 1024, 4500, 15 * 18 * 40):
print(n, trailing_zeros(n), trailing_zeros_by_string(n))
# 1200 2 2
# 625000 3 3
# 640000 4 4
# 1024 0 0
# 4500 2 2
# 10800 2 2The second function counts zeros by reading the decimal string. The two functions agree on every input, which confirms the min(a, b) rule on real numbers.
Common errors
- Counting every 0 in the number. Only the zeros at the right-hand end count. In 100400 the answer is 2, not 4.
- Counting only the factor 5. This works for factorials, where twos are always plentiful, but it fails in general. The number 625000 has seven fives and only three zeros. Use the minimum unless you can prove one prime is never the limit.
- Adding the zeros of the factors. As shown with 15 × 18 × 40, the zeros of a product come from pooled primes, not from each factor's own zeros.
- Applying the rule to sums. The exponents of 2 and 5 in A + B are not determined by those of A and B. To count the zeros of a sum, compute the sum or factor out a common power of 10 first. For example, 10^5 + 10^3 = 10^3 × (100 + 1) = 10^3 × 101, which has 3 zeros.
- Forgetting that 10 itself, 20, 50 and 100 carry both primes. 50 = 2 × 5^2 gives one 2 and two 5s, not "two zeros".
Why this idea matters later in the tutorial
Every later trailing-zero technique reduces to this chapter's rule:
- For n!, you count the twos and fives hidden in 1 × 2 × ... × n, and show the fives are the limiting prime.
- For products and powers, you sum exponents as shown above.
- For other bases such as 12 or 30, you replace 10 = 2 × 5 with the prime factorisation of the base and take a minimum over several primes.
If you are comfortable reading "zeros" as "min(a, b)", the rest of this group of chapters is bookkeeping.
Exercises
Exercise
How many trailing zeros does 2^7 × 3^4 × 5^9 × 7 have?
Show answer
The exponent of 2 is 7 and the exponent of 5 is 9, so the count is min(7, 9) = 7. The factors 3^4 and 7 do not affect it. The number is 10^7 × (3^4 × 5^2 × 7) = 10^7 × 14175, which ends in exactly seven zeros since 14175 ends in 5.
Exercise
Find the number of trailing zeros of 14 × 35 × 60 × 75.
Show answer
Factorise each term: 14 = 2 × 7, 35 = 5 × 7, 60 = 2^2 × 3 × 5, 75 = 3 × 5^2. The exponent of 2 is 1 + 0 + 2 + 0 = 3. The exponent of 5 is 0 + 1 + 1 + 2 = 4. The answer is min(3, 4) = 3. Check: 14 × 35 = 490, 490 × 60 = 29400, 29400 × 75 = 2205000, which has three trailing zeros.
Exercise
Find the number of trailing zeros of 24^8 × 125^4.
Show answer
24^8 = (2^3 × 3)^8 = 2^24 × 3^8, and 125^4 = (5^3)^4 = 5^12. So a = 24 and b = 12, and the answer is min(24, 12) = 12.
Exercise
A number N has exactly 5 trailing zeros and its prime factorisation contains 5^9. What is the least possible exponent of 2 in N, and what is the greatest?
Show answer
The count is min(a, 9) = 5. Since 9 is not 5, the minimum must come from a, so a = 5. The exponent of 2 is exactly 5, so both the least and the greatest possible value are 5.