Trailing Zeros of Factorials

Since I’m bored to hell in programming class, my teacher, who is also my physics teacher, gave me the task to find out the trailing zeros of 100! and then for n!. I then began searching for patterns in factorials starting from 1! to 10!. Interestingly, 5! = 120 has one trailing zero and 10! =3628800 has two trailing zeros. By induction you could determine the trailing zeros of factorials to be expressed by \lfloor \frac{n}{5} \rfloor, where \lfloor x \rfloor is the floor function.

But if you go down the pattern a bit further you find that 25! =15511210043330985984000000 which has six zeros! The heck? This contradicts our idea. Let’s investigate a bit further.

In a factorial, the amount of fives getting multiplied adds a zero. For example 10! = 1\times2\times3\times4\times5\times6\times7\times8\times9\times10. If we take a look we see that there are two 5s hidden here, 5 and 10=5\times2. But 25! contains the factor 25 which is 5^2! So in that case, we find that there are six fives, because 25=5\times5.

With this additional information we can determine the number of trailing zeros of 25! to be \lfloor \frac{25}{5}\rfloor +\lfloor \frac{25}{5^2} \rfloor = 6

But we are not finished yet, there is a pattern here and we should be able to define it for any n!.

Given a number n, the trailing zeros of n! is the sum of n divided by all of its prime factors of 5.

\big f(n) = \sum\limits_{i=1}^{k} \lfloor \frac{n}{5^i} \rfloor

where k has to be chosen such that 5^{k+1} > n.

For example let’s calculate the trailing zeros of 100!. We find 5^3 = 125 > 100 leaving us with f(100) = \lfloor \frac{100}{5}\rfloor +\lfloor \frac{100}{25}\rfloor = 24.