I need to calculate ${Q + K - 1 \choose K - 1}$ *$k!$. I came across this small logic which calculates the above expression but could not get the logic as how this works ?
Here is the logic :
K*product(K-i+Q) , for i=1..(K-1)
and here is the code that i got
long r = K;
for (int i=1; i<=K-1; i++) {
r = ( r * ((K - i + Q);
}
I need an explanation as how the logic is correct ?
That snippet of code calculates
$$K\prod_{i=1}^{K-1}(K-i+Q)\;.$$
As $i$ runs from $1$ to $K-1$, the factor $K-i+Q$ runs from $K-1+Q$ down to $1+Q$, so
$$\prod_{i=1}^{K-1}(K-i+Q)=(K-1+Q)(K-2+Q)\ldots(1+Q)=\frac{(K-1+Q)!}{Q!}\;.$$
Thus,
$$\begin{align*} K\prod_{i=1}^{K-1}(K-i+Q)&=K\cdot\frac{(K-1+Q)!}{Q!}\\ &=K(K-1)!\cdot\frac{(K-1+Q)!}{Q!(K-1)!}\\ &=K!\binom{K-1+Q}{K-1}\;. \end{align*}$$