solve for the lowest value of $k$ in ${n}\choose {k}$ $\geq$ $x$ , given $x$ and $n$

53 Views Asked by At

Let's suppose that $n = 10$ and $x = 500$. I want to find the smallest value of $k$ for which ${n}\choose {k}$ $\geq$ $x$

I can check for all values of $k$ from $1$ to $n/2$ and pick the least one which produces an ${n}\choose {k}$ greater than or equal to $x$ (or alternatively I can use an efficient search algorithm like binary-search, that's beside the point), but I wanted to know if I could solve the inequality for a formula that spits out the exact value immediately.

This is for a python program, so I want to minimize run-time as much as possible.

1

There are 1 best solutions below

0
On

From a computing point of view, I think I should treat the problem using algebra considering $k$ as a continuous variable considering first $$\binom{n}{k}=x \implies \frac{n!}{k!(n-k)!}=x\implies\frac{\Gamma(n+1)}{\Gamma(k+1)\Gamma(n+1-k)}=x$$ and then search for the zero of function $$f(k)=\log (\Gamma (n+1-k))+\log (\Gamma (k+1))-A$$ where $A=\log (\Gamma (n+1))-\log(x)$. Newton method would be quite efficient and starting iterating at $k_0=0$, by Darboux theorem, the solution would be reach without any overshoot.

The first iterate would be $$k_1=\frac{\log (\Gamma (n+1))-A}{H_n}$$ (which I should probably not use at all) and just use $$k_{m+1}=k_m-\frac{f(k_m)}{f'(k_m)}$$ where $f'(k_m)$ would be computed using finite differences. This means that only one single function is required.

Let us try for $n=100$ and $x=12345678987654321$. For this case, the iterates would be $$\left( \begin{array}{cc} m & k_m \\ 0 & 0.00000 \\ 1 & 7.14274 \\ 2 & 12.4175 \\ 3 & 13.2797 \\ 4 & 13.2972 \\ 5 & 13.2973 \end{array} \right)$$

Remember that function $\color{red}{\text{lgamma(x)}}$ is available in Python library.