The number of trailing zeros in the decimal representation of n!, the factorial of a non-negative integer n, is simply the multiplicity of the prime factor 5 in n!. This can be determined with this special case of de Polignac's formula:1
where k must be chosen such that
more precisely
and ⌊ a ⌋ {\displaystyle \lfloor a\rfloor } denotes the floor function applied to a. For n = 0, 1, 2, ... this is
For example, 53 > 32, and therefore 32! = 263130836933693530167218012160000000 ends in
zeros. If n < 5, the inequality is satisfied by k = 0; in that case the sum is empty, giving the answer 0.
The formula actually counts the number of factors 5 in n!, but since there are at least as many factors 2, this is equivalent to the number of factors 10, each of which gives one more trailing zero.
Defining
the following recurrence relation holds:
This can be used to simplify the computation of the terms of the summation, which can be stopped as soon as q i reaches zero. The condition 5k+1 > n is equivalent to q k+1 = 0.
Summarized from Factorials and Trailing Zeroes https://www.purplemath.com/modules/factzero.htm ↩