Sub topic 11
Trailing Zeros of Products and Powers
This chapter assumes you can factorise an integer into primes, apply the laws of exponents, and already know why a trailing zero needs one factor of 2 and one factor of 5. It extends that idea from a single number to products of several numbers and to powers.
The rule for any product
A number ends in exactly as many zeros as it has complete factors of 10. Each factor of 10 is one 2 paired with one 5. So if a number is written as 2^a × 5^b × (other primes), its trailing zeros are min(a, b).
For a product of several numbers, count the factors of 2 and 5 separately across all of them, then take the minimum:
- Factorise every term into primes. You only need the exponents of 2 and 5. Other primes do not matter.
- Add the exponents of 2 over all terms. Call the total A.
- Add the exponents of 5 over all terms. Call the total B.
- The number of trailing zeros is min(A, B).
Worked example: a product of four numbers
Find the trailing zeros of 15 × 24 × 35 × 40.
- 15 = 3 × 5, so it contributes zero 2s and one 5.
- 24 = 2^3 × 3, so it contributes three 2s and zero 5s.
- 35 = 5 × 7, so it contributes zero 2s and one 5.
- 40 = 2^3 × 5, so it contributes three 2s and one 5.
The totals are A = 6 and B = 3, so the product has 3 trailing zeros. Direct multiplication gives 504000, which agrees.
Notice that none of 15, 24 or 35 ends in zero, yet the product does. Zeros appear when 2s and 5s from different terms meet. This is why you must never add up the zeros of the individual terms. That sum is only a lower bound, because the terms can also pair their leftover 2s and 5s.
Powers of a single number
Raising a number to the power k multiplies every prime exponent by k. If N has a factors of 2 and b factors of 5, then N^k has ka and kb. Since min(ka, kb) = k × min(a, b), we get a clean rule:
> The trailing zeros of N^k are k times the trailing zeros of N.
Take N = 1200 = 2^4 × 3 × 5^2. It has min(4, 2) = 2 zeros. So 1200^3 has 3 × 2 = 6 zeros, and indeed 1200^3 = 1,728,000,000.
A mixed product of powers
Find the trailing zeros of 12^5 × 25^3 × 15^2.
- 12^5 = (2^2 × 3)^5 contributes ten 2s and no 5s.
- 25^3 = (5^2)^3 contributes six 5s.
- 15^2 = (3 × 5)^2 contributes two 5s.
So A = 10 and B = 6 + 2 = 8, which gives 8 zeros. Taking the minimum of each factor's zeros would have given 0 for every term. The count only works on the pooled totals.
When the 5s are not the bottleneck
In n! the factors of 5 are always scarcer than the factors of 2, so you only count 5s. That shortcut belongs to factorials. For a general product, check both totals.
Consider 5 × 10 × 15 × ... × 100, which is 20 terms of the form 5k with k = 1 to 20.
- Every term supplies one 5, and the extra 5s come from the k values. The total is 20 + v5(20!) = 20 + 4 = 24.
- The 5 in each term supplies no 2s. All the 2s come from the k values, so the total is v2(20!) = 10 + 5 + 2 + 1 = 18.
Here A = 18 and B = 24, so the answer is 18 zeros. The 2s are the bottleneck, and counting 5s alone would give the wrong answer. Here v5 and v2 mean the exponent of 5 and of 2 in a number.
The summary table below shows the three products we have analysed.
| Product | Total 2s (A) | Total 5s (B) | Trailing zeros |
|---|---|---|---|
| 15 × 24 × 35 × 40 | 6 | 3 | 3 |
| 1200^3 | 12 | 6 | 6 |
| 5 × 10 × 15 × ... × 100 | 18 | 24 | 18 |
Series products such as 1^1 × 2^2 × ... × 10^10
CAT often gives a product where each base is raised to its own power, such as 1^1 × 2^2 × 3^3 × ... × 10^10. Only multiples of 5 can supply 5s, so look at 5^5 and 10^10 first:
- 5^5 gives five 5s.
- 10^10 gives ten 5s and ten 2s.
So B = 15. Now check that the 2s are not scarcer. The terms 2^2, 4^4, 6^6, 8^8 and 10^10 give 2 + 8 + 6 + 24 + 10 = 50 factors of 2. Since 50 > 15, the answer is 15 zeros. Always do this second check. It usually takes a few seconds, because one large power of 2 settles it.
Checking with code
This program counts the 2s and 5s of a product given as (base, exponent) pairs. It also computes the same answer by looking at the digits, so you can confirm the method.
from math import prod
def prime_exponent(n, p):
count = 0
while n % p == 0:
n //= p
count += 1
return count
def zeros_of_product(factors):
# factors is a list of (base, exponent) pairs
twos = fives = 0
for base, exp in factors:
twos += exp * prime_exponent(base, 2)
fives += exp * prime_exponent(base, 5)
return min(twos, fives), twos, fives
def zeros_by_digits(n):
s = str(n)
return len(s) - len(s.rstrip("0"))
cases = {
"12^5 * 25^3 * 15^2": [(12, 5), (25, 3), (15, 2)],
"1^1 * 2^2 * ... * 10^10": [(k, k) for k in range(1, 11)],
"5 * 10 * ... * 100": [(5 * k, 1) for k in range(1, 21)],
}
for name, factors in cases.items():
zeros, twos, fives = zeros_of_product(factors)
number = prod(base ** exp for base, exp in factors)
print(name, zeros, twos, fives, zeros_by_digits(number))
# 12^5 * 25^3 * 15^2 8 10 8 8
# 1^1 * 2^2 * ... * 10^10 15 50 15 15
# 5 * 10 * ... * 100 18 18 24 18The last column comes from the actual big integer and matches the first number in each row, which is the factor-counting result.
Edge cases and traps
- Sums are different. The factor-counting method applies to products. For a sum such as 20^6 + 20^4, pull out the common factor: 20^4 × (20^2 + 1) = 20^4 × 401. Since 401 is not divisible by 2 or 5, the zeros come only from 20^4, which has 4 × 1 = 4. Check: 64,000,000 + 160,000 = 64,160,000.
- Powers of 10 are free. A term such as 10^n contributes n 2s and n 5s, so it adds exactly n zeros without changing the balance.
- Odd or non-multiple-of-5 terms contribute nothing. A term like 7^20 has no 2s and no 5s. It does not change the zero count, but it does not rescue a product with too few 2s or 5s either.
- A single missing prime caps the count. If a product has no 5 at all, it ends in 0 zeros, however many 2s it has. For example, 2^100 × 3^50 has no trailing zeros.
- Pool before comparing. Compute A and B for the whole expression, including any numerator and denominator. For a quotient, subtract the exponents in the denominator first, then take the minimum.
Exercises
Exercise
How many trailing zeros does 18^4 × 35^6 × 50^3 have?
Show answer
18^4 = 2^4 × 3^8 gives four 2s. 35^6 = 5^6 × 7^6 gives six 5s. 50^3 = (2 × 5^2)^3 = 2^3 × 5^6 gives three 2s and six 5s. So A = 4 + 3 = 7 and B = 6 + 6 = 12. The answer is min(7, 12) = 7 trailing zeros.
Exercise
How many trailing zeros does 2400^7 have?
Show answer
2400 = 2^5 × 3 × 5^2, so it has min(5, 2) = 2 trailing zeros. Raising it to the 7th power multiplies both exponents by 7, giving 2^35 and 5^14, so the answer is 7 × 2 = 14 trailing zeros.
Exercise
How many trailing zeros does the product 6 × 12 × 18 × ... × 120 have? (There are 20 terms, each equal to 6k for k = 1 to 20.)
Show answer
Each term is 6k = 2 × 3 × k. The 5s come only from the k values, so B = v5(20!) = 4. The 2s number 20 (one per term) plus v2(20!) = 18, so A = 38. The answer is min(38, 4) = 4 trailing zeros.
Exercise
How many trailing zeros does 20^6 + 20^4 have?
Show answer
Factor out the common power: 20^6 + 20^4 = 20^4 × (20^2 + 1) = 20^4 × 401. The number 401 is odd and does not end in 0 or 5, so it adds no factors of 2 or 5. The zeros come from 20^4 = 2^8 × 5^4, which gives min(8, 4) = 4 trailing zeros. A direct check gives 64,160,000.