Sub topic 10
Trailing Zeros of \(n!\)
Prerequisites and the key idea
Assume you know prime factorisation, division with remainders, laws of exponents, and the meaning of a factorial. The factorial \(n!\) is the product
\[ n! = 1 \times 2 \times 3 \times \cdots \times n. \]
A trailing zero is a zero at the right end of a number. Each trailing zero comes from one factor of \(10\). Since
\[ 10 = 2 \times 5, \]
the number of trailing zeros equals the number of complete pairs of factors \(2\) and \(5\).
In \(n!\), there are more factors of \(2\) than factors of \(5\). Therefore, the number of trailing zeros is determined by the number of factors of \(5\).
The factorial zero-count formula
The number of trailing zeros in \(n!\) is
\[ Z(n!)=\left\lfloor\frac n5\right\rfloor+ \left\lfloor\frac n{25}\right\rfloor+ \left\lfloor\frac n{125}\right\rfloor+ \left\lfloor\frac n{625}\right\rfloor+\cdots \]
Continue adding terms while the denominator is at most \(n\). The symbol \(\lfloor x\rfloor\) means the greatest integer less than or equal to \(x\).
The first term counts numbers containing at least one factor of \(5\). The second term counts additional factors of \(5\) from multiples of \(25=5^2\). The third term counts further factors from multiples of \(125=5^3\).
For example, \(100!\) has
\[ \left\lfloor\frac{100}{5}\right\rfloor+ \left\lfloor\frac{100}{25}\right\rfloor =20+4=24 \]
trailing zeros. The multiples of \(5\) contribute 20 factors of \(5\). The multiples of \(25\) contribute one extra factor each, adding 4 more.
| Term | What it counts for \(100!\) | Count |
|---|---|---|
| \(\lfloor100/5\rfloor\) | At least one factor of 5 | 20 |
| \(\lfloor100/25\rfloor\) | An extra factor of 5 | 4 |
| \(\lfloor100/125\rfloor\) | A third factor of 5 | 0 |
Why multiples of higher powers matter
Counting only multiples of \(5\) gives the wrong answer when a number contains more than one factor of \(5\). For example,
\[ 25=5^2,\qquad 50=2\times5^2,\qquad 75=3\times5^2. \]
Each of these contributes two factors of \(5\), not one. The first division by \(5\) counts one factor for each multiple of \(5\). Dividing by \(25\) counts the additional factor in each multiple of \(25\).
Similarly, \(125=5^3\) contributes three factors of \(5\). It is counted once in the \(n/5\) term, once in the \(n/25\) term, and once in the \(n/125\) term.
def trailing_zeros_factorial(n):
count = 0
power = 5
while power <= n:
count += n // power
power *= 5
return count
print(trailing_zeros_factorial(100)) # 24
print(trailing_zeros_factorial(125)) # 31For \(125!\), the calculation is
\[ 25+5+1=31. \]
The final \(1\) appears because \(125\) contains three factors of \(5\), and the \(125\) term counts its third factor.
A fast CAT method
For a question asking for the trailing zeros of \(n!\):
- Divide \(n\) by \(5\), ignoring the remainder.
- Divide \(n\) by \(25\), then by \(125\), and so on.
- Add the integer quotients.
- Stop when the next power of \(5\) exceeds \(n\).
For \(250!\),
\[ \left\lfloor\frac{250}{5}\right\rfloor+ \left\lfloor\frac{250}{25}\right\rfloor+ \left\lfloor\frac{250}{125}\right\rfloor =50+10+2=62. \]
Exercise
How many trailing zeros does 250! have?
Show answer
62, because floor(250/5) + floor(250/25) + floor(250/125) = 50 + 10 + 2.