In the seventies when I got my first TI pocket calculator in school I developed a certain fascination for the factorial function. It was basically the most dangerous function around because if you entered any integer larger than 69 it would blow up with an error because it could not calculate numbers with more than two digits in the base-10 exponent and for most of them it could of course only show the first few most significant digits. So it took me almost 40 years and the mind-boggling capacity of WL to handle arbitrary-sized integers (well almost) to ask the following question: What is the number of trailing zeros in n factorial? As a short reminder n! is the mathematical notation for the product of all positive integers less or equal to n. As you can easily see from an example there are plenty of trailing zeros in the factorial of reasonably-sized integers:
Well to answer the question above we reformulate it as the number of sought zeros is equal to how many times n factorial can be divided (meaning integer division) by ten without getting a remainder. But for getting a factor of 10 there needs to be a corresponding factor of 5 and one factor of 2 in the prime factorization of n factorial. Since every other number contains at least a factor of 2 in its prime factorization we can safely count only the factors of 5 in the factorization of the first n integers so we get a first (very functional and “pattern matchy” way to calculate the trailing zero function TZF:
All well it seems, of course not everything worked out right at first trial, but now that all three seem to coincide, it seems pretty watertight right? So then let’s see which form might be most efficient to calculate the TZF function:
So you see functional is not always faster unfortunately, but it is also surprising to see that taking the long detour to calculate the whole factorial does equally well roughly as the first variant which only factorizes rather small numbers. But back to the original question we can see that we answer the original question pretty accurately by calculating the TZF function divided by n as a geometric sum:
So the trailing number of zeros of n factorial is approximately n/4 for large n (to be more accurate take one less). So if you get the question at the next party “how many trailing zeros are there in 10000 factorial?” just say “2499 of course”!
Let’s see if these agree to iron out obvious mistakes by feeding random samples. But beware, the factorial is still dangerous and will exhaust your physical memory if you throw in too big numbers or too small radices.
In order to get asymptotic values of the trailing zeros you now need to divide n by (p-1) k. You have to do this only with the prime factor that results in the largest value of (p-1) k of course. Then you round down again and you will be mostly very accurate. So let’s do an example: