Is there an inverse to Stirling's approximation? The factorial function cannot have an inverse, $0!$ and $1!$ having the same value. However, Stirling's approximation of the factorial $x! \sim x^xe^{-x}\sqrt{2\pi x}$ does not have this problem, and could provide a ballpark inverse to the factorial function. But can this actually be derived, and if so how? Here is my work:
$$
\begin{align}
y &= x^xe^{-x}\sqrt{2\pi x}\\
y^2 &= 2\pi x^{2x + 1}e^{-2x}\\
\frac{y^2}{2\pi} &= x^{2x + 1}e^{-2x}\\
\ln \frac{y^2}{2\pi} &= (2x + 1)\ln x - 2x\\
\ln \frac{y^2}{2\pi} &= 2x\ln x + \ln x - 2x\\
\ln \frac{y^2}{2\pi} &= 2x(\ln x - 1) + \ln x
\end{align}
$$
That is as far as I can go. I suspect the solution may require the Lambert W function.
Edit: I have just realized that after step 3 above, one can divide both sides by e to get
$$\left(\frac{x}{e}\right)^{2x + 1} = \frac{y^2}{2e\pi}$$
Can this be solved?

It is better to start with
$$
x! \approx \left( {\frac{{x + \frac{1}{2}}}{\mathrm{e}}} \right)^{x + \frac{1}{2}} \sqrt {2\pi } .
$$
Setting
$$
y = \left( {\frac{{x + \frac{1}{2}}}{\mathrm{e}}} \right)^{x + \frac{1}{2}} \sqrt {2\pi } ,
$$
we find
$$
\frac{1}{\mathrm{e}}\log \left( {\frac{y}{{\sqrt {2\pi } }}} \right) = \frac{{x + \frac{1}{2}}}{\mathrm{e}}\log \left( {\frac{{x + \frac{1}{2}}}{\mathrm{e}}} \right) = \exp \left( {\log \left( {\frac{{x + \frac{1}{2}}}{\mathrm{e}}} \right)} \right)\log \left( {\frac{{x + \frac{1}{2}}}{\mathrm{e}}} \right).
$$
We use the Lambert $W$-function defined by $W(z)\mathrm{e}^{W(z)}  = z$ for $z>0$. From the above we see that
$$
\log \left( {\frac{{x + \frac{1}{2}}}{\mathrm{e}}} \right) = W\!\left( {\frac{1}{\mathrm{e}}\log \left( {\frac{y}{{\sqrt {2\pi } }}} \right)} \right).
$$
Whence
$$
\frac{{x + \frac{1}{2}}}{\mathrm{e}} = \exp \left( {W\!\left( {\frac{1}{\mathrm{e}}\log \left( {\frac{y}{{\sqrt {2\pi } }}} \right)} \right)} \right) = \frac{{\frac{1}{\mathrm{e}}\log \left( {\frac{y}{{\sqrt {2\pi } }}} \right)}}{{W\!\left( {\frac{1}{\mathrm{e}}\log \left( {\frac{y}{{\sqrt {2\pi } }}} \right)} \right)}},
$$
i.e.,
$$
x = \frac{{\log \left( {\frac{y}{{\sqrt {2\pi } }}} \right)}}{{W\!\left( {\frac{1}{\mathrm{e}}\log \left( {\frac{y}{{\sqrt {2\pi } }}} \right)} \right)}} - \frac{1}{2}.
$$
For example, if $y=720$ then
$$
x = \frac{{\log \left( {\frac{{720}}{{\sqrt {2\pi } }}} \right)}}{{W\!\left( {\frac{1}{\mathrm{e}}\log \left( {\frac{{720}}{{\sqrt {2\pi } }}} \right)} \right)}} - \frac{1}{2} \approx 5.99658,
$$
which is a very good approximation.
