Is there a good upper ground to the number of digits of a factorial n! when I only know the number of digits of $n$

factorial

I was coding a function for calculating the factorial of a big number in $C$. Since I'm using a structure where I don´t know directly the value of the number, I need to find the number of digits of the result (or a decent upper bound) only knowing the number of digits of my original number. Any help is appreciated, thanks.

Best Answer

Given a natural number $n$, the number of decimal digits is equal to $d(n)=\lfloor \log_{10}(n)\rfloor +1$. If you only know the value of $d(n)$, then $n$ could be as large as the number consisting of $d(n)$ nines $999...9$, or $10^{d(n)}-1$. This means that the best upper bound we can get on the number of digits of $n!$ given only the number of digits of $n$ is equal to $$d(n!)\le d\big((10^{d(n)}-1)!\big)$$ and this is a tight bound. Let’s see if we can simplify this a bit to get an upper bound that is easier to calculate but a little bit less tight. First of all, note that $d(n) < \log_{10}(n)+1$, so we have $$d(n!)\le \log_{10}\big((10^{d(n)}-1)!\big)+1$$ Now, a companion to Stirling’s Formula tells us that $N!$ is less than or equal to $N^{N+1/2}e^{1-N}$ for all natural numbers $N$. Thus, we have from the above inequality that $$d(n!)\le \frac{10^{d(n)}-1/2}{\ln(10)}\ln(10^{d(n)}-1)+\frac{2-10^{d(n)}}{\ln(10)}+1$$ This is decent, but it’s still a bit ugly. Let’s cut out some unnecessary terms (while preserving inequality) to get the following nicer (but still less tight) upper bound: $$d(n!)\le d(n)\cdot 10^{d(n)}-\frac{10^{d(n)}}{\ln(10)}$$ This formula should work for $n\ge 4$, say.