7

How many digits are there in 100 factorial?

How does one calculate the number of digits?

  • 5
    Welcome to MSE. To get useful answers please provide some context to your question, including what you tried so far and where you got stuck. – Ittay Weiss Dec 20 '14 at 10:48

4 Answers4

9

Suppose that $x$ is a positive, $n$-digit integer. Note that, $$n-1=\log_{10}(10^{n-1}) \leq \log_{10}(x) < \log_{10}(10^n) = n.$$ Thus, you might compute, the floor of $\log_{10}(100!)$ and add $1$. But \begin{align} \log_{10}(100!) &= \sum_{k=1}^{100} \log_{10}(k) \approx \int_{1}^{100}\log_{10}(t)dt \\ &= \left.\frac{t\ln(t)-t}{\ln(10)}\right|_1^{100} \approx 157.005 \end{align} Thus, the answer ought to be 158.


The reason this can be expected to work is that the integral can be approximated via Riemann sums. In fact, $$\sum_{i=1}^{n} \log_{10}(i) < \int_1^n \log_{10}(t)dt < \sum_{i=2}^n\log(10,i) = \sum_{i=1}^n\log(10,i),$$ as illustrated for $n=20$ in the following picture:

enter image description here

Thus, $$\sum_{i=1}^{n} \log_{10}(i) - \sum_{i=1}^n\log(10,i) < \int_1^n \log_{10}(t)dt - \sum_{i=1}^n\log(10,i) < 0.$$ But, the term on the left is just $\log_{10}(100)=2$. So, the integral is a lower bound for the sum that cannot be more than two off of the actual value. Accounting for the fact that the integral is nearly mid-way between the sums, the error should less than one, which is why we hit he answer exactly.

Mark McClure
  • 30,510
  • Isn't it easier to use the known Stirling formula ? –  Dec 20 '14 at 13:23
  • 1
    Yes, probably so. But, then, I didn't learn Stirling's formula until well after I had learned about Riemann sums. And, as your answer nicely illustrates, it's nice to think about it from a relatively elementary perspective. I think I'll go upvote it! :) – Mark McClure Dec 20 '14 at 13:42
  • The $n\log n$ approach is extremely efficient, quasi $O(1)$. –  Dec 20 '14 at 17:11
  • Wow! That's actually the best of all the answers! Thanks. – Tanuj Jul 27 '17 at 15:33
5

Stirling's formula gives a good approximation: $$n!\approx\sqrt{2\pi n}\left(\frac{n}{e}\right)^n$$
That is messier, but it helps because I can take the logarithm: $$\log(n!)\approx \log(\sqrt{2\pi n})+n\log\left(\frac{n}{e}\right)$$ That was the common base-ten logarithm.
The number of digits in $n!$ equals the next integer above $\log(n!)$.

Empy2
  • 50,853
5

If I were writing a computer program to do it:

$$\left\lfloor\log_{10}(100!)\right\rfloor+1=\left\lfloor\sum_{i=1}^{100}\log_{10}(i)\right\rfloor+1 = 158$$

$\lfloor\log_b(x)\rfloor+1$ calculates the smallest integer power of $b$ which is greater than x. i.e. $$b^{\lfloor\log_b(x)\rfloor} \le x < b^{\lfloor\log_b(x)\rfloor+1}.$$ This is the same as the number of digits in a base-$b$ representation of $x$.

David P
  • 12,320
  • 1
    $\log_{10}(10) = 1$ so $\lceil\log_{10}(10)\rceil = 1$ which is not the number of digits in $10$. You want $1+\lfloor\log_{10}(x)\rfloor$. – Henrik supports the community Dec 20 '14 at 11:16
  • You also need to edit the ceilings to floors in the line below the first equation. – Henrik supports the community Dec 20 '14 at 11:19
  • Direct computation of the product with rescaling (no logarithms) can be more efficient. –  Dec 20 '14 at 12:12
  • +1 Though, if you're going use a computer program, then why not just Length[IntegerDigits[100!]] in Mathematica or WolframAlpha? My answer is similar in that it uses the same sum but then estimates that sum with an integral. – Mark McClure Dec 20 '14 at 13:28
  • Very true, My brain defaults to C++'s math.h whenever I think about calculation – David P Dec 20 '14 at 14:23
  • @MarkMcClure: a rather inefficient method, possibly more than quadratic in $n$. There is no need to compute all digits. The truly interesting question would be to establish the number of significant digits sufficient to avoid being off by one (or so) because of truncation. Of course, just for $100!$ it doesn't matter. –  Dec 20 '14 at 17:08
  • @YvesDaoust I just think it's neat that discrete sums can be estimated using integrals. So often, it's the other way around. – Mark McClure Dec 20 '14 at 19:02
2

Using a four-operations calculator, you can work as follows:

  • start from $2$,
  • multiply by increasing integers,
  • every time the product exceeds $10$, shift the comma (divide by $10$) and count the shift.

The number of digits will be the number of shifts plus one.

$$\begin{align} &2&0\\ &6&0\\ &2.4&1\\ &1.20&2\\ &7.20&2\\ &5.040&3\\ &4.0320&4\\ &\dots&\dots\\ &9.3326215\dots&157 \end{align}$$

Actually, you are computing $100!$ in the scientific notation.

  • Something is iffy, based on https://math.stackexchange.com/a/1946244/6400 it should be 158. http://www.wolframalpha.com/input/?i=floor(lngamma(101)%2Flog(10))%2B1; – chx Jun 20 '17 at 08:29
  • 1
    @chx: I guess you didn't read all, $157$ is the number of shifts, i.e. one less than the number of digits. ($2$ doesn't have $0$ digits, does it ?) –  Jun 20 '17 at 08:36