## Number of digits for factorial of 12345678987654321

11

4

What is the number of digits (IntegerLength) of the factorial of 12 345 678 987 654 321? The number of zeros at the end of this factorial was calculated and it is huge: exactly 3 086 419 746 913 569 zeros. The calculation of the number of digits for this factorial if done by simple multiplication, will take about 199 years in my current machine which has a I7 with 6 cores and 64GB of memory. Yes, I know, this number will not fit in my memory, even though I have a SSD with 1TB extra memory which will also not be able to store this gigantic number. So, any ideas?

PS: the program to calculate the number of zeros at the end of a factorial is:

Fnz[n_] := Module[{f, s, p, z},
p = 5;
f = IntegerDigits[n, 5];
s = Total[f];
z = (n - s)/(p - 1);
Print[n, "! has ", z, " zeros at the end."];
]


Here the result:

 Fnz[12345678987654321]


12345678987654321! has 3 086 419 746 913 569 zeros at the end.

2Thanks for the question. One of my pet peeves, however, is people describing their problems as "hard", when what they mean is "I can't work out how to do this" - which is implied, because you posted the question here! :) – Patrick Stevens – 2015-12-30T18:12:25.940

Patrick, factoring a large number n (with say 300 digits) is also a hard math problem even though everybody knows how to do it. Just try every prime until you reach the Sqrt[n], right? Same thing here. You see, I could calculate the number of zeros at the end of the number WITHOUT having it in my hand to count the zeros. That´s is what I´m talking about. – Giorgio – 2015-12-30T18:31:47.453

1Factoring is in general difficult, but factoring a factorial is not :) – Patrick Stevens – 2015-12-30T18:53:35.897

Patrick Stevens just means that it's no use saying it's a hard math problem, i) because otherwise you would not ask about it and ii) also probably because it's a very subjective opinion. No big deal anyways and welcome on the website. – anderstood – 2015-12-30T19:15:49.653

Patrick, if you don't find it hard, why don't you answer my question? How many digits there is in the factorial of 12345678987654321, including the zeros, of course? Tell me, and I would love to learn how to do it (for the number above of course). You see, just for the zeros, they wouldn't fit in my memory. I only have available 610^10 bytes and just for the the zeros we need about 310^15 bytes. – Giorgio – 2015-12-30T19:17:15.100

1@Giorgio One possibility: buy 3.10^4 - 1 additional 1To SSD drives. More seriously, did you check Chip Hurst's answer (which gives another result)? – anderstood – 2015-12-30T19:19:22.297

This is really not a mathematica question. If you are really interested in the topic you might go study algorithms for computing the log gamma function. – george2079 – 2015-12-30T19:19:33.690

Well, I want to see a Mathematica program to do it. In the program below, I tried the function Pochhammer which allows me to find the number of digits by calculating chunks of the factorial. Say we want to know the number of digits for factorial of 100. The program below will answer it: – Giorgio – 2015-12-30T19:40:13.990

Here is the program:Ndf[f_] := Module[{s, lf, i, a, b, nd = 0}, If[Head[f] == Integer, s = f; nd = IntegerDigits[f]; Goto[two]]; s = Total[f]; lf = Length[f]; i = 1; a = 1; b = f[[1]]; Label[one]; nd = nd + Log10[Pochhammer[a, b]]; a = a + b; If[i < lf, i = i + 1; b = f[[i]]; Goto[one]]; nd = 1 + nd // Floor; Label[two]; Print[s, "! has ", nd, " digits."]; ] – Giorgio – 2015-12-30T19:40:50.783

Example: Ndf[{50,50}] and the answer will be: 100! has 158 digits. In the case of the number I want, 12345678987654321 even if we divide it, the time to finish it will around 200 years. – Giorgio – 2015-12-30T19:45:35.780

1What do you mean by "program"? You can always pack the code in my answer into it's own function. Something like Ndf[x_] := Ceiling[LogGamma[N[x + 1]]/Log[10]], and now Ndf[12345678987654321] outputs 193299018111544064. – Chip Hurst – 2015-12-30T19:47:34.887

Hi Chip, thanks! Wow, that was fast. One more subject to learn. I appreciated it. – Giorgio – 2015-12-30T19:51:53.720

@Giorgio By the way, Chip's answer was why I didn't answer. My answer would have been identical. – Patrick Stevens – 2015-12-31T09:38:15.140

22

We can exploit the built in LogGamma:

x = 12345678987654321;
Ceiling[LogGamma[N[x + 1]]/Log[10]]

193299018111544064


We have naively for $n > 1$, that $n! < n^n$. Taking logs of both sides gives the (not very tight) bound $\log\Gamma(x + 1) < x \log(x)$ for $x > 1$.

This means if we want the number of digits of $n!$ for some large $n$, asking for $\log_{10}(n \log(n))$ number of digits in $\log\Gamma$ will be more than enough.

Here it is in a function.

Ndf[x_] :=
With[{p = 20 + Max[1, Ceiling[Log10[x Log[x]]]]},
Ceiling[LogGamma[N[x + 1, p]]/Log[10]]
]

Ndf[12345678987654321]

193299018111544064

Ndf[10^30]

29565705518096748172348871081099


1Correct, but at the edge of what can be done with machine precision. – Daniel Lichtblau – 2015-12-30T19:23:44.513

Yep, I suppose that is worth mentioning. – Chip Hurst – 2015-12-30T19:24:26.010

312345678987654321+1 can just barely be represented exactly as an IEEE double-precision float, but only because it's even! If it were odd, that last 1 bit would be lost or rounded up. There is not even the potential of a loss of precision if you just do Ceiling[LogGamma[12345678987654321 + 1]/Log[10]]. – Mark Adler – 2015-12-30T22:28:17.413

@MarkAdler That's a good point about using exact inputs, the only issue is that there is a gray area where Mathematica will try to auto evaluate LogGamma, but it would require more memory than most computers have. For example In[2]:= MemoryConstrained[LogGamma[10^10], 10^8]

Out[2]= $Aborted. It looks like it stops auto evaluating around10^14. – Chip Hurst – 2015-12-31T00:17:51.610 1I don't know what the gray area is, but this doesn't fall into it. With the Ceiling, Mathematica uses less than 4K of memory to do the exact calculation. – Mark Adler – 2015-12-31T00:22:55.677 @MarkAdler Right, but my point is if we were to use your idea in a general function that would handle any integer input, we would have to treat all inputs less than 10^14 differently than what you suggest. – Chip Hurst – 2015-12-31T00:24:15.387 Ah. Now I see where the gray area you speak of lies. – Mark Adler – 2015-12-31T00:27:19.950 13 I wondered if Chip's answer was exactly correct, given Daniel's comment about machine precision. So I did it a little differently with higher precision in a way that gives good confidence in the result. (It turns out that the machine precision answer is correct.) LogGamma can be expanded around infinity thusly: Series[LogGamma[z], {z, Infinity, 4}]  to give: $$z \left(-\log \left(\frac{1}{z}\right)-1\right)+\frac{1}{2} \left(\log \left(\frac{1}{z}\right)+\log (2 \pi )\right)+\frac{1}{12 z}-\frac{1}{360 z^3}+O\left(\left(\frac{1}{z}\right)^5\right)$$ For large$z$that should converge very rapidly. In fact, the$1\over z$terms can be dropped, since$O\left(1\over z\right)$will be way less than one digit. For really big z, like this one, this is a sufficient approximation for LogGamma to count digits (unless you happen to be right on the hairy edge of an integer): $$\left(z-{1\over 2}\right)\log z-z+{1\over 2}\log\left(2\pi\right)$$ (Simplified a little.) You can add the$1\over 12z$if you like, but it won't make any difference here. Evaluating that for this case, I indeed get: (z - 1/2) Log[z] - z + 1/2 Log[2 \[Pi]] /. z -> 12345678987654321 + 1 // Ceiling[#/Log[10]] &  193299018111544064  Looking at the digits after the decimal: (z - 1/2) Log[z] - z + 1/2 Log[2 \[Pi]] /. z -> 12345678987654321 + 1 // N[#/Log[10], 20] &  1.9329901811154406373*10^17  So it's not on the hairy edge since the ...63.73 was rounded up to ...64, and in fact adding the$1\over z$terms don't change the result. Interestingly, that formula is not the log of Stirling's formula, and the log of Stirling's formula does not give the correct answer. (It's low by eight digits.) Since the number of digits in$n!$is bounded above by$2n\log_{10}(n)$, we could always do Ceiling[LogGamma[N[x + 1, Ceiling[2x Log10[x]]]]/Log[10]] and be assured that our answer is correct. (I may edit my post with something similar later today.) – Chip Hurst – 2015-12-30T21:47:13.513 3 If you use the 2nd version of Stirling's approximation at https://en.wikipedia.org/wiki/Stirling%27s_approximation which has corrections of order$\ln(n)\$, n! ~ Sqrt[2*Pi*n] (n/E)^n, it comes out right, though: Ceiling@N[(Log[Sqrt[2 \[Pi] n]] + n (Log[n] - 1))/Log[10] /. n -> 12345678987654321, 20] yields 193299018111544064.

– evanb – 2015-12-30T22:27:11.820