Sub topic 10
Trailing zeros of n!
This chapter assumes you can factorise integers into primes, know what n! means, and can use the floor function ⌊x⌋ (round down to a whole number). It also assumes the previous chapter's idea that the trailing zeros of a number equal the smaller of its exponents of 2 and 5. Here we apply that idea to factorials, where it collapses to one short formula.
From zeros to factors of 5
The factorial n! is the product 1 × 2 × 3 × ... × n. Each trailing zero comes from one factor of 10 = 2 × 5, so:
zeros in n! = min(exponent of 2 in n!, exponent of 5 in n!)
In a factorial, the exponent of 2 is never smaller than the exponent of 5. Compare the two counts term by term: ⌊n/2⌋ ≥ ⌊n/5⌋, ⌊n/4⌋ ≥ ⌊n/25⌋, ⌊n/8⌋ ≥ ⌊n/125⌋, and so on. Every term of the count for 2 is at least the matching term of the count for 5. The minimum is therefore always the exponent of 5, and we only count fives.
Counting fives: the floor-sum formula
Count how many of 1, 2, ..., n contribute factors of 5:
- Every multiple of 5 contributes at least one factor of 5. There are ⌊n/5⌋ of them.
- Every multiple of 25 contributes a second factor that the first count missed. There are ⌊n/25⌋ of them.
- Every multiple of 125 contributes a third, and so on.
Adding these gives the formula:
zeros in n! = ⌊n/5⌋ + ⌊n/25⌋ + ⌊n/125⌋ + ⌊n/625⌋ + ...
Stop when the power of 5 exceeds n, because every later term is 0.
Worked example: 100!
- ⌊100/5⌋ = 20
- ⌊100/25⌋ = 4
- ⌊100/125⌋ = 0, so stop.
Total: 20 + 4 = 24 trailing zeros.
Worked example: 2026!
Divide repeatedly by 5 and keep only the whole-number part each time:
2026 → 405 → 81 → 16 → 3 → 0
Add the quotients: 405 + 81 + 16 + 3 = 505 trailing zeros.
This repeated-division form is the fastest by hand. Each quotient is the floor of the previous one divided by 5, which is the same as ⌊n/25⌋, ⌊n/125⌋, and so on.
The same method gives these results:
| n | ⌊n/5⌋ | ⌊n/25⌋ | ⌊n/125⌋ | ⌊n/625⌋ | Zeros |
|---|---|---|---|---|---|
| 24 | 4 | 0 | 0 | 0 | 4 |
| 25 | 5 | 1 | 0 | 0 | 6 |
| 100 | 20 | 4 | 0 | 0 | 24 |
| 125 | 25 | 5 | 1 | 0 | 31 |
| 625 | 125 | 25 | 5 | 1 | 156 |
| 1000 | 200 | 40 | 8 | 1 | 249 |
The count jumps by more than one at 25 (from 4 to 6) and at 125 (from 28 at 124 to 31). Those jumps matter in the reverse problems below.
Checking the formula in code
The function below follows the formula. The check compares it with the actual digits of n! for every n up to 500.
from math import factorial
def trailing_zeros_factorial(n):
count = 0
p = 5
while p <= n:
count += n // p
p *= 5
return count
def brute_force(n):
s = str(factorial(n))
return len(s) - len(s.rstrip("0"))
for n in (4, 5, 24, 25, 100, 2026):
print(n, trailing_zeros_factorial(n), brute_force(n))
assert all(trailing_zeros_factorial(n) == brute_force(n) for n in range(1, 501))
print("formula matches for n = 1 to 500")The six printed rows show the same count in both columns: 0, 1, 4, 6, 24 and 505. The assertion confirms the formula for every n from 1 to 500.
A second route: base-5 digit sum
There is an exact alternative form. If s is the digit sum of n written in base 5, then the exponent of 5 in n! equals (n − s) / 4.
For n = 100, the base-5 form is 400, so s = 4 and (100 − 4) / 4 = 24. For n = 2026, the base-5 form is 31101, so s = 3 + 1 + 1 + 0 + 1 = 6 and (2026 − 6) / 4 = 505. Both agree with the floor-sum.
This route is rarely faster in an exam. It is useful as an independent check, and it explains the fact used below: the count is always less than n/4.
Reverse problems: finding n for a given number of zeros
CAT often asks the opposite question: for which n does n! end in exactly k zeros? Use these facts:
- The count is non-decreasing in n and changes only when n passes a multiple of 5.
- The count is always below n/5 + n/25 + ... = n/4, so n must be greater than 4k.
- Because the count changes only at multiples of 5, a value of k that is reachable is reached by exactly five consecutive values of n, starting at a multiple of 5.
Worked example: smallest n with exactly 50 zeros
We need n > 200, and the smallest candidate is a multiple of 5.
- n = 200: 40 + 8 + 1 = 49 zeros.
- n = 205: 41 + 8 + 1 = 50 zeros.
So the smallest n is 205. The values 205, 206, 207, 208 and 209 all give 50 zeros, and 210 gives 51.
Some counts are impossible
At n = 125 the count jumps over a value. Since 124! has 28 zeros and 125! has 31, no factorial ends in exactly 29 or 30 zeros. The same happens at every multiple of 25, so the answer to such a question can be "no such n exists".
Edge cases and common errors
- Small n. For n = 1, 2, 3, 4, the count is 0, because there is no multiple of 5 yet. By convention 0! = 1, which also has no zeros.
- Stopping after ⌊n/5⌋. This is the most common slip. For n = 100 it gives 20 instead of 24. Always continue to 25, 125, 625.
- Dividing by 10. The formula uses powers of 5, not 10. Counting ⌊n/10⌋ misses all the odd multiples of 5.
- Decimals in place of floors. Use n // 5 style whole-number division at every step.
- Counting zeros anywhere in the digits. The question asks for trailing zeros only. The number 100! has other zeros inside it, which do not count.
- Quotients and differences of factorials. The rule "count only fives" depends on the 2s being at least as plentiful. This holds in n! but not automatically for 200!/100!, because dividing removes factors of both primes.
Worked example: zeros in 200!/100!
First count the fives:
- Exponent of 5 in 200!: 40 + 8 + 1 = 49
- Exponent of 5 in 100!: 20 + 4 = 24
- Difference: 25
Now check the twos, since the quotient is not a factorial:
- Exponent of 2 in 200!: 100 + 50 + 25 + 12 + 6 + 3 + 1 = 197
- Exponent of 2 in 100!: 50 + 25 + 12 + 6 + 3 + 1 = 97
- Difference: 100
The quotient has 25 factors of 5 and 100 factors of 2, so it ends in min(100, 25) = 25 zeros. The twos are still far more plentiful, so the answer is 25. When you subtract exponents, always compare both primes before settling on the smaller one.
Practice
Exercise
How many trailing zeros does 350! have?
Show answer
Divide repeatedly by 5, keeping whole parts: 350 → 70 → 14 → 2 → 0. The sum is 70 + 14 + 2 = 86 trailing zeros.
Exercise
Is there a positive integer n such that n! ends in exactly 30 zeros? If so, what is the smallest one?
Show answer
No. For n = 124 the count is 24 + 4 = 28. For n = 125 it is 25 + 5 + 1 = 31. The count skips over 29 and 30, so no factorial ends in exactly 30 zeros.
Exercise
Find the smallest n for which n! ends in at least 100 zeros.
Show answer
The count is below n/4, so n > 400. Try 400: 80 + 16 + 3 = 99 zeros. Try 405: 81 + 16 + 3 = 100 zeros. The smallest n is 405.
Exercise
How many trailing zeros does 500!/250! have?
Show answer
Fives: 500! has 100 + 20 + 4 = 124, and 250! has 50 + 10 + 2 = 62, so the quotient has 62. Twos: 500! has 250 + 125 + 62 + 31 + 15 + 7 + 3 + 1 = 494, and 250! has 125 + 62 + 31 + 15 + 7 + 3 + 1 = 244, so the quotient has 250. The smaller exponent is 62, so there are 62 trailing zeros.