Sub topic 11
Trailing Zeros of Products and Powers
Assume you know prime factorisation, laws of exponents, and that a trailing zero in base 10 comes from a factor of 10. The goal is to count factors, not calculate the full number.
The basic rule
Since 10 = 2 × 5, every trailing zero requires one factor of 2 and one factor of 5. For a positive integer N, let:
- v₂(N) be the number of factors of 2 in N.
- v₅(N) be the number of factors of 5 in N.
Then:
trailing zeros of N = min(v₂(N), v₅(N))
For example:
- 1200 = 2⁴ × 3 × 5².
- It contains four factors of 2 and two factors of 5.
- Therefore, it contains two complete factors of 10.
- The number of trailing zeros is 2.
Usually, factors of 2 are more plentiful than factors of 5. However, for products and powers, count both so that the method remains reliable.
Products: add the prime-factor counts
For any positive integers A and B:
- v₂(AB) = v₂(A) + v₂(B)
- v₅(AB) = v₅(A) + v₅(B)
Consider 360 × 125.
- 360 = 2³ × 3² × 5, so it contributes three 2s and one 5.
- 125 = 5³, so it contributes no 2s and three 5s.
- The product has v₂ = 3 and v₅ = 4.
- Its number of trailing zeros is min(3, 4) = 3.
You do not need to multiply 360 by 125.
For several factors, add the counts from every factor.
| Expression | v₂ | v₅ | Trailing zeros |
|---|---|---|---|
| 360 × 125 | 3 | 4 | 3 |
| 12 × 25 × 40 | 5 | 3 | 3 |
| 2⁷ × 5⁴ | 7 | 4 | 4 |
Powers: multiply the counts
If N = aᵏ, every prime factor in a appears k times. Therefore:
- v₂(aᵏ) = k × v₂(a)
- v₅(aᵏ) = k × v₅(a)
So:
trailing zeros of aᵏ = min(kv₂(a), kv₅(a))
Example: Find the trailing zeros of 120⁵.
First factor 120:
120 = 2³ × 3 × 5
Therefore:
- v₂(120⁵) = 5 × 3 = 15
- v₅(120⁵) = 5 × 1 = 5
The answer is min(15, 5) = 5.
The same idea works for a product raised to a power. For example:
(24 × 125)³
The product 24 × 125 has:
- v₂ = 3, because 24 = 2³ × 3
- v₅ = 3, because 125 = 5³
After raising the product to the third power:
- v₂ = 3 × 3 = 9
- v₅ = 3 × 3 = 9
Thus, the expression ends in 9 zeros.
def prime_exponent(n, p):
count = 0
while n % p == 0:
n //= p
count += 1
return count
def trailing_zeros_power(base, exponent):
twos = exponent * prime_exponent(base, 2)
fives = exponent * prime_exponent(base, 5)
return min(twos, fives)
print(trailing_zeros_power(120, 5)) # 5
print(trailing_zeros_power(24 * 125, 3)) # 9A quick CAT check
If an expression contains an obvious excess of factors of 2, focus on counting factors of 5. For instance, 2¹⁰ × 5⁶ has six trailing zeros because only six pairs can be formed. Check that the answer cannot exceed the total number of factors of either prime.
Exercise
How many trailing zeros does (2⁴ × 5³)⁵ have?
Show answer
The expression has 20 factors of 2 and 15 factors of 5. Therefore, it has min(20, 15) = 15 trailing zeros.