I think it’s a very bad idea to calculate two very large numbers and hope that the quotient comes out as something sensible.
Start by taking the natural log:
ln(n!/(q!)^r) = ln(n!) - r*ln(q!)
You can use gammaln()
for the two function values, simplify, then take exp()
to get the result you want:
value = exp(gammln(n+1) -r*gammln(q+1))
Numerical Recipes has a nice chapter on how to implement functions like gammln().
7
solved Fast algorithm to calculate n! / (q!)^r