Here is a riddle someone has been asked in a job interview: How many zero digits are there in $100!$?
Well, I found the first $24$ quite fast by counting how many times five divides $100!$ ($5$ divides $20$ times and $25$ divides it $4$ times).
However, there are more zero digits in the middle of the number (these can be found by hand, by typing factorial(100)
in sage).
My question is whether there is a smart way to determine the number of zero digits in $100!$, and more generally in $n!$.
By the way, this will not affect the job interview as it was finished some time ago.
Answer
You can get a very good estimate by (a) calculating the number of powers of ten in the factorial, (b) estimating the total number of decimal digits (using Stirling's approximation), and (c) assuming all digits except the trailing zeroes are equally likely to have any value. Since there are plenty of powers of $2$ to go around, the number of trailing zeroes is equal to the number of powers of five, plus the number of powers of twenty-five, etc.
$$
T_n=\sum_{k=1}^{\infty}\left\lfloor{\frac{n}{5^{k}}}\right\rfloor.
$$
The total length as estimated by Stirling's approximation is
$$
L_n=\log_{10}n!=n\log_{10} n - \frac{n}{\ln 10}+O(\ln n).
$$
Combining these, our estimate of the total number of zeroes is
$$
Z_{n}\sim T_n + \frac{1}{10}\left(L_n - T_n\right)=\frac{9}{10}\sum_{k=1}^{\infty}\left\lfloor{\frac{n}{5^{k}}}\right\rfloor+\frac{1}{10}n\log_{10}n-\frac{n}{10\ln 10}+O(\ln n).
$$
This turns out to be pretty good. Using WolframAlpha to get the exact values:
$$
\begin{matrix}
\text{n} & \text{Estimate} & \text{Exact} & \text{Abs. Error}\\
\hline
1000 & 481 & 472 & 9\\
2000 & 1022 & 1025 & 3\\
4000 & 2166 & 2143 & 23\\
8000 & 4573 & 4645 & 72 \\
16000 & 9631 & 9560 & 71 \\
32000 & 20226 & 20227 & 1
\end{matrix}
$$
The result for $n=32000$ is fortuitously precise...
No comments:
Post a Comment