# Factorial digits

How many digits does the factorial of $n$ have?


```python
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sympy
```

Compute the factorial of numbers ranging over several orders of magnitude.


```python
order = 5
x_values = np.logspace(0, order, 4*order + 1, dtype=np.int64)
fac_values = [sympy.factorial(x) for x in x_values]
nr_digits = list(map(lambda x: len(str(x)), fac_values))
```

Plot the result in a $\log$-$\log$ plot.


```python
figure, axes = plt.subplots(nrows=1, ncols=2, figsize=(12, 6))
axes[0].loglog(x_values, fac_values)
axes[0].set_xlabel(r'$n$')
axes[0].set_ylabel(r'$n!$')
axes[1].loglog(x_values, nr_digits)
axes[1].set_xlabel(r'$n$')
axes[1].set_ylabel(r'nr. digits of $n!$')
figure.tight_layout()
```


The number of digits grows approximately linear with $n$.  This is easy to see since $n! = 10^{\log_{10} n!}$, $n! = \prod_{i=1}^{n} i$ and hence $\log_{10} n! = \sum_{i=1}^{n} \log_{10} i$.  Now we can bound this sum from above since $\log i < 1$ for $i < 10$, $\log i < 2$ for $i < 100$, and so on.  So if $n = 10^p$, $\log_{10} n! < 1 \times 10 + 2 \times 100 + 3 \times 1000 + \ldots + p \times 10^p$.  So $\log_{10} 10^p! < (p + 1) \times 10^p$.  Since the number of digits of an integer $n$ is $\lceil \log_{10} n \rceil$, this proves the point.

03792f737d7e2aa4afbce96853ebbc70d0111011
ipynb
Jupyter Notebook
Python/Sympy/factorial_digits.ipynb
Gjacquenot/training-material
16b29962bf5683f97a1072d961dd9f31e7468b8d
CC-BY-4.0
2015-03-23T13:34:42.000Z
2022-03-21T00:27:21.000Z
Python/Sympy/factorial_digits.ipynb
Gjacquenot/training-material
16b29962bf5683f97a1072d961dd9f31e7468b8d
CC-BY-4.0
2015-02-25T15:04:26.000Z
2022-01-03T07:42:48.000Z
Python/Sympy/factorial_digits.ipynb
Gjacquenot/training-material
16b29962bf5683f97a1072d961dd9f31e7468b8d
CC-BY-4.0
2015-11-26T11:44:51.000Z
2022-03-21T00:27:22.000Z
Qwen/Qwen-72B
1. YES
2. YES