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 6

10^10 bytes and just for the the zeros we need about 310^15 bytes. – Giorgio – 2015-12-30T19:17:15.1001@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.887Hi 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