13

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?

Oshawott
  • 4,028
  • 4
    Would you accept a 50 years old non-programmable pocket calculator ? – Claude Leibovici Jul 02 '21 at 12:46
  • 6
    As an initial step, I would say $x!$ has $8$ right-hand zeros, so $x$ would be somewhere from $7\times 5=35$ through to $39$ (remembering $25$ introduces a double zero) – Henry Jul 02 '21 at 12:47
  • 1
    After the estimate suggested by @Henry, you could check whether the given $x!$ is divisible by $37$, and whether it's divisible twice by $19$, etc., thus quickly narrowing down the possibilities. (This assume you know that the given number is the factorial of an integer.) – Andreas Blass Jul 02 '21 at 12:51
  • 1
    @ClaudeLeibovici Yes, but that should not contain logarithms in different bases (because logarithms are important in this problem). But this can contain logarithms in base 10 – Oshawott Jul 02 '21 at 13:25
  • @Henry that's a good idea indeed, that can replace my method 1 in the question. – Oshawott Jul 02 '21 at 13:27
  • The command of Mathematica Solve[x! == 523022617466601111760007224100074291200000000, x, Integers] // AbsoluteTiming results in {0.0141206, {{x -> 38}}}. – user64494 Jul 02 '21 at 14:09
  • Are you assuming that a value $n \in \mathbb N$ has been given to you such that $x!=n$ has a solution $x \in \mathbb N$? If not then the suggestion of @Henry will, with very high probability and very little work, tell you that no solution exists. – Lee Mosher Jul 02 '21 at 16:06
  • @LeeMosher Yes, I've just edited this issue in the question. – Oshawott Jul 02 '21 at 16:19
  • For large enough $n$ we have that the number of digits of $n$ uniquely determine the value of $x$ (if such $x$ exists) which could be found with summing $\log_10(i)$ from $i=2$ until you find the closest one to the number of digits - 1. – kingW3 Jul 02 '21 at 22:41

5 Answers5

11

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$$

5

Extending Henry's suggestion, we have any factor of 5 is going to be preceded by a factor of 2, so we can count the number of factors of 5 by counting the trailing 0's.

Every 5th 0 counts as two 5s, since those are multiples of 25. Every 25th 0 counts as 3 5s, etc. In other words, we just need to correct for the "overcounts" of 0s by things that have more than one factor of 5.

Thus, we know the largest the largest multiple of 5 is equal to or less than $x$ is given by letting $m$ equaling the number of trailing 0s in $n$ and solving for $r$ as follows: $$r:=\lfloor m/5 \rfloor + \lfloor m/5^2 \rfloor + \dots $$ Then we get $$5r\leq x<5(r+1) $$ This converges a lot faster than powers of 2 and then narrows your answer down to 5 possibilities. I'm not sure if there's a simple way to expressly state $x4$ in terms of $n$ beyond this range of 5 to check, but if you didn't need that, this converges fast enough and is easy enough to check the remaining numbers

Alan
  • 16,895
1

Integer binary algorithm

Assume that we have integer input $x!=n$

  1. If $n=1$ return $1$
  2. Count the number of trailing zeros, $t$, in the binary representation of $n$, trailing zeros are telling the highest $2^k$ number is divisible by, you do not need a full binary expansion
  3. Calculate $w=\frac{n}{t!}$
  4. Calculate the number of times $t+1$ divides $w$, rounded down to the nearest integer giving this result: $$r= \left \lfloor \frac{\ln(w)}{\ln(t+1)} \right \rfloor$$
  5. Return $t+r$

Example:

$$2432902008176640000_{10}=10000111000011011001110111110010000010101101000000000000000000_2$$

Notice that in the practice you are not doing a full binary conversion, you just want to know if the number is divisible by $2,4,8,16,32,64,...$ i.e. $2^k$.

So start from:

$$\frac{2432902008176640000}{18!}=380$$

$$19^2<380<19^3$$

so $r=2$

so it is $2432902008176640000=(18+2)!=20!$

Notice that this method is giving you a very close value. You get for $100!$, $97$ already.

Dividing a large decimal number by 2.

  1. Write above each odd digit $1$.
  2. Decrement all odd digits by $1$.
  3. Now divide each digit by $2$.
  4. Add 5 to the digit that is to the right of $1$.
    8712364123019561232
    1.
     11 1  1 1 111 1 1 
    2.
    8602264022008460222
    3.
      55 5  5 5 555 5 5
    4301132011004230111
    4.
    4356182061509780616
  • Converting to binary looks like about as much work as simply dividing by $2,3,4,\ldots$ until you reach $1$. – TonyK Jul 02 '21 at 13:34
  • @TonyK Not necessarily, you are doing that up to reaching first 1. You just want to find trailing zeros. Notice that this is the same as dividing by 2,4,8,16,32,64... which is faster than dividing by 2,3,4,5,6... and to some extent somewhat obvious where to start from looking at the number itself. – Alex Peter Jul 02 '21 at 13:46
  • But then you have to do all that division anyway ("So start from:...")! – TonyK Jul 02 '21 at 14:05
  • @TonyK I am not sure what your goal is then. This is by far the simplest known algorithm, even for manual execution regarding the number of operations and complexity. It is not that I invented it, if that is what you hint. You could theoretically use decimal expansion but it would need some adjustment. Division by 2 when you have decimal numbers is very much trivial, ask that question and I will show you how. – Alex Peter Jul 02 '21 at 16:18
  • "Ask that question and I will show you how": thank you, but that won't be necessary. My point is that your method requires first counting the trailing zeroes in the binary expansion of $n$ (yes, this is trivial); and then evaluating $n/k!$ for the resulting $k$. But you can just skip the first step! – TonyK Jul 05 '21 at 17:02
1

Just looking at the number of digits seems to provide an accurate solution for $x > 9$.

$\ln x! = \sum_{1 \le k\le x} \ln k \approx \int _{1}^{x+1} \ln x\,\mathrm dx + \frac12 \ln (x+1)=(x+\frac12) \ln (x+1) - x$

The error tends to a constant, and by numerical evidence it is around 0.1. Therefore one can just ignore it.

So converting to digits, $\log x! \approx (x+\frac12) \log (x+1) - \frac x{\ln 10}$. This should serve for most purposes.

For $x < 10$, recite the factorials.

Trebor
  • 5,501
0

If we assume that values up to $10!$ are known, here is the list of signature values, that is first 6 digits from $11!$ to $99!$ that can be used to recognize factorial, providing it is integer factorial we are dealing with. $6$-digit signature is minimal possible. Tables are the shortest manual possible, right?

11! = 399168...
12! = 479001...
13! = 622702...
14! = 871782...
15! = 130767...
16! = 209227...
17! = 355687...
18! = 640237...
19! = 121645...
20! = 243290...
21! = 510909...
22! = 112400...
23! = 258520...
24! = 620448...
25! = 155112...
26! = 403291...
27! = 108888...
28! = 304888...
29! = 884176...
30! = 265252...
31! = 822283...
32! = 263130...
33! = 868331...
34! = 295232...
35! = 103331...
36! = 371993...
37! = 137637...
38! = 523022...
39! = 203978...
40! = 815915...
41! = 334525...
42! = 140500...
43! = 604152...
44! = 265827...
45! = 119622...
46! = 550262...
47! = 258623...
48! = 124139...
49! = 608281...
50! = 304140...
51! = 155111...
52! = 806581...
53! = 427488...
54! = 230843...
55! = 126964...
56! = 710998...
57! = 405269...
58! = 235056...
59! = 138683...
60! = 832098...
61! = 507580...
62! = 314699...
63! = 198260...
64! = 126886...
65! = 824765...
66! = 544344...
67! = 364711...
68! = 248003...
69! = 171122...
70! = 119785...
71! = 850478...
72! = 612344...
73! = 447011...
74! = 330788...
75! = 248091...
76! = 188549...
77! = 145183...
78! = 113242...
79! = 894618...
80! = 715694...
81! = 579712...
82! = 475364...
83! = 394552...
84! = 331424...
85! = 281710...
86! = 242270...
87! = 210775...
88! = 185482...
89! = 165079...
90! = 148571...
91! = 135200...
92! = 124384...
93! = 115677...
94! = 108736...
95! = 103299...
96! = 991677...
97! = 961927...
98! = 942689...
99! = 933262...