41

I'm trying to solve hard combinatorics that involve complicated factorials with large values.

In a simple case such as $8Pr = 336$, find the value of $r$, it is easy to say it equals to this: $$\frac{8!}{(8-r)!} = 336.$$

Then $(8-r)! = 336$ and by inspection, clearly $8-r = 5$ and $r = 3$.

Now this is all and good and I know an inverse function to a factorial doesn't exist as there is for functions like sin, cos and tan etc. but how would you possibly solve an equation that involves very large values compared to the above problem without the tedious guess and checking for right values.

Edit: For e.g. if you wanted to calculate a problem like this (it's simple I know but a good starting out problem) Let's say 10 colored marbles are placed in a row, what is the minimum number of colors needed to guarantee at least $10000$ different patterns? WITHOUT GUESS AND CHECKING

Any method or explanation is appreciated!

TripleA
  • 1,503
  • 4
    Stirling approximation. – Simply Beautiful Art Jan 01 '17 at 01:30
  • I have seen some literature online about finding the inverse of the Gamma Function. You may consider looking into that. – pseudoeuclidean Jan 01 '17 at 01:41
  • Do you have a link? – TripleA Jan 01 '17 at 01:54
  • 1
    "In a row" would seem to imply that "blue, red, red, red, red, red, red, red, red, red" and "red, blue, red, red, red, red, red, red, red, red" are different patterns. If so, factorials are not applicable. Of course there are other combinatorial problems that involve large factorials to which your question still applies. – David K Jan 01 '17 at 02:01
  • Actually you could use factorials for that question, except with identical elements.... – TripleA Jan 01 '17 at 02:07
  • http://math.stackexchange.com/questions/18362/is-there-an-inverse-gamma-gamma-1-z-function – jimjim Jan 01 '17 at 02:11
  • 1
    FWIW, I posted a Python 2 function that uses Stirling's approximation & the Newton-Raphson method to invert log factorial here. – PM 2Ring Jan 01 '17 at 03:11
  • For your add-on question, if there are $n$ colors there are $n^{10}$ patterns. We don't need factorials for this, but you want $n^{10} \gt 10^5$ or $10 \log_{10}n \gt 5, \log_{10}n \gt 0.5, n \gt 3.16$, so we need $4$ colors. – Ross Millikan Jan 01 '17 at 04:31
  • @RossMillikan It's $10^4$, not $10^5$. – TheNumberOne Jan 01 '17 at 16:34
  • @TheNumberOne: you are right, I miscounted the zeros. The same approach works. Now we need $\log_{10} n \gt 0.4, n \gt 2.51$, so we need $3$ colors – Ross Millikan Jan 01 '17 at 17:04
  • The answer is actually 4.... – TripleA Jan 01 '17 at 23:03
  • Related: https://math.stackexchange.com/questions/1413411/solve-the-factorial-equation-x-c, https://math.stackexchange.com/questions/61755/ – Watson Jan 02 '17 at 11:22
  • @RossMillikan the answer is 4 – TripleA Jan 02 '17 at 21:55
  • 1
    @TripleA, why do you say the answer is $4$? There is nothing wrong in Ross Millikan's analysis for the add-on question as it is stated: $3^{10}=59{,}049\gt10{,}000$. So unless you have some nonstandard definition of what's required for two patterns to be considered "different," the answer is $3$. – Barry Cipra Jan 03 '17 at 01:48
  • @BarryCipra This is an identical elements case. $3^{10}$ doesnt work. The maximum number of ways you can obtain with $3$ colours for e.g. red, blue, yellow is $3$ reds, $3$ blues, $4$ yellows so $\frac{10!}{3!\times3!\times4!}$ which is less than 10000 unfortunately – TripleA Jan 03 '17 at 02:40
  • @BarryCipra What I mean is that let's say for example in the word WOOL, when calculatinG number of ways of ordering the letters we divide by 2! to account for the repeated letter O – TripleA Jan 03 '17 at 02:45
  • 1
    @TripleA, ah, I see what you mean now. It would help to edit the add-on question to clarify that the different patterns must all come from a single choice of $10$ marbles of various diffferent colors. Both Ross and I interpreted it as meaning that each pattern is simply an assignment of an allowed color to each marble. – Barry Cipra Jan 03 '17 at 02:52
  • @BarryCipra I don't understand how you and Ross interpreted the question but since more than one person has interpreted differently to what I intended, I'm should edit it, except I don't know what to change it to. It would be great if you could do so :) – TripleA Jan 03 '17 at 02:55
  • I don't see anything in the question or the other answers suggesting that there is any restriction on the number of marbles of each color. If you insist that the colors be distributed as evenly as possible, the question is solvable, but it is a different question. Rereading the question, I can see that you are thinking of a particular set of $10$ marbles that you can order in $10^4$ ways and yes you then need four colors so that $2,2,3,3$ of each gives $10!/2!/2!/3!/3!=100800 \gt 10^4$ (even $10^5$) – Ross Millikan Jan 03 '17 at 03:05
  • @RossMillikan There is no restriction but the more you distribute them evenly, the more ways you are going to get. Also, what I'm looking for is a way to solve the question without guessing and checking, for the instance of large numbers lets say $10^{12}$ ways with 50 marbles. – TripleA Jan 03 '17 at 03:08

5 Answers5

29

I just wrote this answer to an old question. Using $a=1$, we get a close inverse for the factorial function: $$ n\sim e\exp\left(\operatorname{W}\left(\frac1{e}\log\left(\frac{n!}{\sqrt{2\pi}}\right)\right)\right)-\frac12\tag{1} $$

robjohn
  • 345,667
  • 10
    Just to add a small detail, robjohn's solution is strictly exact if we use $$n=\left\lceil e^{W\left(\frac{\log \left(\frac{n!}{\sqrt{2 \pi }}\right)}{e}\right)+1}-\frac{1}{2}\right\rceil$$ – Claude Leibovici Jan 02 '17 at 04:51
  • 4
    @ClaudeLeibovici: if we know that $n$ is an integer. This is also a good inverse for $\Gamma(n+1)$. $$n\sim e\exp\left(\operatorname{W}\left(\frac1{e}\log\left(\frac{\Gamma(n)}{\sqrt{2\pi}}\right)\right)\right)+\frac12$$ is an approximate inverse for $\Gamma(n)$. – robjohn Jan 02 '17 at 06:58
  • @ClaudeLeibovici: I posted this answer recently. The comparison between Stirling and the approximation that this inverse is based on indicates why this inverse is pretty good. – robjohn Oct 28 '20 at 16:43
  • Thanks for telling me ! You know what ? I am happy ! thanks to you ! – Claude Leibovici Oct 28 '20 at 16:59
  • I don't know what I did, but I am glad that you're happy! – robjohn Oct 28 '20 at 17:20
  • When I look up Lambert $W$, I notice that $W_{k}(z)$ is often stated with an integer subscript $k$. What is $k$ set to in your problem above? – Stan Shunpike Sep 01 '22 at 14:10
  • @StanShunpike: There are two real branches of Lambert $W$. $W_0:\left[-1/e,\infty\right)\to[-1,\infty)$ and $W_{-1}:\left[-1/e,0\right)\to[-1,-\infty)$. By using other complex starting points, we can get other branches, but the ones given in that post are for real branches. – robjohn Sep 01 '22 at 15:32
11

By Stirling's formula

$$n! \sim \sqrt{2\pi n} \left({\frac{n}{e}}\right)^n $$

So we can given a large $n!$ we can attempt to numerically solve,

$$n!=\sqrt{2\pi x} \left({\frac{x}{e}}\right)^x$$

For $x$ by Newton's method to get an approximate inverse.

The function $\mathbb{N} \to \mathbb{N}$ given by $f(n)=n!$ is increasing. Also,

$$\sqrt{2\pi}n^{n+\frac{1}{2}}e^{-n} \leq n! \leq e n^{n+\frac{1}{2}}e^{-n}$$

So by numerically solving $n!=\sqrt{2\pi}x^{x+\frac{1}{2}}e^{-x}$ and $n!=ex^{x+\frac{1}{2}}e^{-x}$ we can find bounds for $n$.

9

For equations involving large factorials, I find the elementary inequalities $(n/e)^n < n! < (n/e)^{n+1}$ often useful.

Once these have been used, you can use Stirling's approximation.

These can be proved by induction from the elementary inequalities $(1+1/n)^n < e < (1+1/n)^{n+1}$.

marty cohen
  • 107,799
  • 2
    So let's say 10 coloured marbles are placed in a row for example, what is the minimum number of colours needed to guarantee atleast 10000 different patterns? – TripleA Jan 01 '17 at 01:36
4

Would you be fine with an algorithm instead of a mathematical function?

Solve $nPx = p$ for $x$:

x = 0
while p > 1:
    p /= n
    n--
    x++
return x

Solve $xPr = p$ for $x$:

x = r
while p > 1:
    x++
    p /= x
return x

Solve $x!=y$ for $x$:

x = 1
while y > 1:
    x++
    y /= x
return x

Your example problem can be modeled without the factorial function pretty easily. I'm assuming two marbles with the same color are indistinguishable, that we have at least 10 marbles of each color, and that the order of the marbles matters:

$$ x^{10}\ge10000\\ x\ge10000^{1/10}\approx2.512\\ x=3 $$

0

The inverse function of $y = x!$ means getting x in terms of $y$ , i.e $x =$ the largest number in factorisation of y as a factorial.(Where factorising as a factorial means you divide $y$ by $2$, then $3$ and so on. You stop when you get $1$) For example let $5040 = x! , x = ?$

Factoring $5040$ as a factorial $5040= 7\times 6\times 5\times 4\times 3\times 2\times 1$ , and $7$ is the largest number of that factorial $\implies x = 7$ In your problem $8!/336 = (8 – r)! , r = ?$

$8!/336 = 120$ , let $(8 – r) = x$ , hence $120 = x! , x = ?$

$120 = 5\times 4\times 3\times 2\times 1$, and the largest number of that factorial $ = x = 5 = (8 – r) \implies r = 3.$

amWhy
  • 209,954
A.Eriba
  • 11
  • 1
    Welcome to MSE. Please try to use MathJax -- it makes your answer more readable and increases the chance being read by other users. –  Dec 10 '18 at 13:12