Solving $x!=n$ without a calculator for large $n$

algebra-precalculusapproximationfactorialsystems of equations

Solve the equation for $x$: $$x!=n$$ where $x,n \in \mathbb N$.

(Here $n \in \mathbb N$ is a given number such that $x!=n$ has a solution in $x \in \mathbb N$.)
The problem is easy with a calculator and for small $n$. But I want to solve this for large $n$ without a calculator (Why? Just curious!).
Here is an example of how large $n$ can be: $$x!=523 022 617 466 601 111 760 007 224 100 074 291 200 000 000$$ (Here $n$ is a $45$-digit number and $x=38$.)
To solve the equation, I could think of two methods. Here is their description:

Method 1 – prime factorization:
In this method, we find the prime factorization of $n$. To do this, we keep dividing $n$ by $2$ until we get an odd number. This is equivalent to finding $\log_2 n$. But since $n$ is a large number, it is very difficult and long process.
For the example problem, we have to divide $n$ by $2$, $35$ times. Yet this doesn't give the exact answer. Since $\lfloor 38/2 \rfloor + \lfloor 38/2^2 \rfloor + \dots =\lfloor 39/2 \rfloor + \lfloor 39/2^2 \rfloor + \dots=35$ this implies that $x=38$ or $x=39$. We have to divide the $n$ by $13^3$ to be sure that $x=38$ is the solution.

Method 2 – Stirling's approximation:
In this method, we use the Stirling's approximation formula, which is $x! \sim \sqrt {2\pi x}(\frac x e)^x$. But it is not easy to find $x$ in terms of $n$. Here is my workings to do that: $$x!=n$$ $$\implies \sqrt {2\pi x}(\frac x e)^x=n$$ $$\implies 2\pi x (\frac x e)^{2x}=n^2$$ $$\implies x^ {2x+1}\cdot \frac 1 {e^{2x}}=\frac {n^2}{2\pi}$$
I am unable to proceed after that. But I don't think the derived equation can solve for $x$ in terms of $n$ without using a calculator (or at least this would be too hard).


I hope my workings are correct. So, is there some easier way to solve the equation? And how do I complete my workings on method 2?

Best Answer

For the solution of $x!=n$, @Gary proposed a superb approximation (have a look here)

$$x = \frac{{\log \left( {\frac{n}{{\sqrt {2\pi } }}} \right)}}{{W\left( {\frac{1}{e}\log \left( {\frac{n}{{\sqrt {2\pi } }}} \right)} \right)}} - \frac{1}{2}$$ where $W(.)$ is Lambert function.

Let us rewrite it as $$x=e \frac t{W(t)}-\frac{1}{2} \qquad \text{with} \qquad t=\frac 1e\log \left( {\frac{n}{{\sqrt {2\pi } }}} \right)$$

Now, for large values of $t$, use what is given in the Wikipedia page $$W(t) \sim L_1-L_2+\frac{L_2}{L_1}+\frac{L_2(L_2-2)}{2L_1^2}+\frac{L_2(2L_2^2-9L_2+6)}{6L_1^3}+\cdots$$ where $L_1=\log(t)$ and $L_2=\log(L_1)$

Using my $50+$ years old non-programmable pocket calculator for $$n=123456789876543212345678987654321234567898765432123456789$$ $$t=47.1756\qquad L_1=3.85388\qquad L_2=1.34908$$ Using only the first three terms, then $$W(t)\sim3.85388-1.34908+\frac{1.34908}{3.85388}=2.85485$$ $$x\sim e \frac{47.1756}{2.85485}-\frac 12=44.4188$$ while, as a real, the solution is $45.0083$.

Speaking about integers, for sure, you need to use $\lceil x \rceil$ and the results are the same. $$45!=119622220865480194561963161495657715064383733760000000000$$ $$46!=5502622159812088949850305428800254892961651752960000000000$$ Using the gamma function $$44.4188!=13053540092571206188366309387719398631004867852125601792$$ $$45.0083!=123456789876545254267360504092794809714819815437028556800$$

Related Question