Solving $n!=x$ for $n$.

approximationfactoriallambert-wreal-analysis

Trying to solve / approximate for $n$ in
$$n!=x\tag 1$$
where $x$ is given, I started with Stirling's approximation

$$n!\approx\sqrt{2\pi n}(n/e)^n\tag 2$$

Taking log and divide by $e$:

$$\frac1{2e}\ln(2\pi n)+\frac ne\ln(n/e)\approx \frac1e\ln x \tag3$$

Now in order to proceed, I neglected the left term with $\ln(2\pi n)$ and took the 0-th branch of Lambert $W$ assuming $x$ is large:

$$\ln n – 1 = \ln(n/e) = W(\tfrac ne\ln(n/e))\approx W(\tfrac1e\ln x) \tag4$$

Finally, adding $1$ and taking $\exp$ yields the approximation

$$n \approx \exp\left(1+W\Big(\frac1e\ln x\Big)\right) \tag5$$

That approximation is not too bad, but the results are too big by ca. 0.7 for bigger numbers; for example $x=100!$ yields $n\approx 100.7$.

Question: Is there a way to improve the result? For example, something that's smarter than just throwing away $\ln(2\pi n)$ in $(3)$.

I tried to find a better approximation of $\sum_k \ln k$ but all I found was solutions that effectively referred back to Stirling's $(2)$.


That linked question doen't answer my question. First, it doesn't explain how to improve my calculation; and my question is about improving my calculation. Second, the term +0.5 is coming out of the blue and without explanation, except that it's "good". So no, that answer didn't help. In particular I don't seek for sites to copy-paste from, I am striving to getting better technique etc.


I checked that other answer again and i still does not explain where the 0.5 is coming from. It just asserts that value from the start and claims it it "good" starting with it.

Best Answer

something that's smarter than just throwing away $\ln(2πn)$ in $(3)$

Taking up the comments, the first idea is to divide out $\sqrt{2\pi}$ from the start so that $(3)$ becomes:

$$\frac1{2e}\ln(n)+\underbrace{\frac ne\ln(n/e)}_{\textstyle=:f(n)}\approx \frac1e\ln\frac x{\sqrt{2\pi}} \tag{3'}$$

Second idea is to set $f(n) = \dfrac ne\ln(n/e)$ and to find $\Delta$ such that

$$f(n+\Delta)\approx f(n) + \Delta f'(n) \approx f(n) + \frac 1{2e}\ln n\tag{*}$$

i.e. one adds some amout $\Delta$ to the argument of $f$ such that it accounts for the term $(\ln n)/(2e)$ in the LHS of $(3')$ that was previously neglected. Solving $(*)$ for $\Delta$ gives:

$$\begin{align} \Delta \approx \frac{\ln n}{2e\cdot f'(n)} = \frac{\ln n}{2e\cdot\Big(\dfrac1e(1+\ln (n/e))\Big)} = \frac12\\ \end{align}$$

This turns $(3')$ into

$$\frac1{2e}\ln(n)+\frac ne\ln(n/e) \approx \frac {n+0.5}e\ln\frac {n+0.5}e \approx \frac1e\ln\frac x{\sqrt{2\pi}} \tag{3"}$$ The rest of the derivation is the same: Apply Lambert $W$:

$$\ln\frac {n+0.5}e \approx W\left(\frac1e\ln\frac x{\sqrt{2\pi}}\right) \tag{4'}$$ and finally solve for $n$:

$$\bbox[10px,border:1px solid]{n \approx \exp\left(1+W\left(\frac1e\ln\frac x{\sqrt{2\pi}}\right)\right) -\frac12 }\tag{5'}$$

The results are much more precise now, for example:

$$3! \mapsto 2.9905$$

$$100! \mapsto 99.99991$$

which is perfectly fine for my purposes.