For an engineering application, I need the inverse functions of the computations of the number of combinations and permutations.
In the thread How to reverse the $n$ choose $k$ formula? it shows how to reverse/inverse the number of combinations function...
Given:
$X = C(n, k) = \binom{n}{k} = \frac{n! }{ k!(n-k)! }$
then you can limit n to:
$\sqrt[k]{k! X} + k \ge n \ge \sqrt[k]{k! X}$
And thus you at most need to check k+1 possible values of n (or with a binary search, just log(k+1) checks). Great!
But how do I solve for k instead of for n? (Given n and the number of combinations, how do I compute k?)
And given instead number of Permutations:
$X = P(n, k) = \frac{n! }{ (n-k)! }$
then I believe I am correct in assuming from above that:
$\sqrt[k]{X} + k \ge n \ge \sqrt[k]{X}$
And in this case, I actually can solve for k as well since k only appears once:
$k = n - InverseLnFactorial(ln(n!) - ln(X))$
And finally, given instead CR is Combinations with Repetitions:
$X = CR(n, k) = \binom{n+k-1}{k} = \binom{n+k-1}{n-1} = \frac{ (n+k-1)! }{ k!(n-1)! }$
then I believe I am correct in assuming from above that:
$\sqrt[k]{k! X} \ge n \ge \sqrt[k]{k! X} - k$
But again, how would I then solve for k instead of n? (Given n and the number of combinations with repetitions, how would I compute k?)
In both cases of Combinations (with and without repetitions), having k appear inside two different factorial terms makes it difficult for me to find an algebraic way to solve for k. Any help, or even clue, on how to do the algebra for those two cases would be greatly appreciated!






We can find $k$ by a binary search as follows.
It is easy to check that when $n$ is sufficiently big and fixed and $k$ increases in its range, $X$ increases too. Namely, it holds uder the following assumptions.
So we can look for $k$ first by an increasing binary search, trying $k=0,1,2,4,\dots$ until $C(n,k)>X$ and then by a decreasing binary search.
We can improve this algorithm by providing lower bounds for $k$.
Namely, if $X$ is $C(n,k)$ or $P(n,k)$ then $X\le n^k$, so $k\ge \ln X/\ln n$. A bit better bound can be obtained as follows. Since $\ln (1+x) \le x$ for any $x>-1$,
$$\ln X\le \ln\left(\prod_{i=0}^{k-1} n-i\right)=\sum_{i=0}^{k-1} \ln(n-i)=\sum_{i=0}^{k-1} \ln \left(n\left(1-\frac in\right)\right)=$$ $$ \sum_{i=0}^{k-1} \ln n +\ln \left(1-\frac in\right)\le k\ln n - \sum_{i=0}^{k-1} \frac in= k\ln n-\frac {k(k-1)}{2n}.$$
Thus $$k\ge n\ln n+\frac 12-\sqrt{\left(n\ln n+\frac 12\right)^2-2n\ln X}>\frac{\ln X}{\ln n+\tfrac 1{2n}} .$$
For $X=CR(n,k)$ we similarly obtain
$$\ln X\le \ln\left(\prod_{i=0}^{k-1} n+i\right)=\sum_{i=0}^{k-1} \ln(n+i)=\sum_{i=0}^{k-1} \ln \left(n\left(1+\frac in\right)\right)=$$ $$ \sum_{i=0}^{k-1} \ln n +\ln \left(1+\frac in\right)\le k\ln n + \sum_{i=0}^{k-1} \frac in= k\ln n+\frac {k(k-1)}{2n}.$$
Thus
$$k\ge -n\ln n+\frac 12+\sqrt{\left(n\ln n-\frac 12\right)^2+2n\ln X}.$$