Good method for solving nCr and nPr

1.5k Views Asked by At

Okay, so I'm designing a calculator that accepts real and imaginary inputs, and I'm going to implement the nPr and nCr functions soon.

I already programmed in the factorial function, which also accepts real and imaginary input, thanks to the incredibly fast Lanczos approximation.

I know the nCr function can be computed by $\frac{n!}{r!(n-r)!}$ and the nPr function can be computed by $\frac{n!}{(n-r)!}$, but this becomes a problem when n and r become very large integers.

OpenProcessing.org (where I'm programming the calculator) is able to process any number up to $2^{1024}$, and any number above that is registered as ∞ (which I don't mind, in fact as a number reaches the overflow limit, it does tend to act like ∞, so it's actually somewhat practical).

As a result, trying to use factorials to compute say, {$^{1000}_{997}$} would register as $\frac{∞}∞$. This results in an error despite the true answer being 166,167,000, a number well within acceptable limits.

My question is this: Is there an alternative method I can use when such values approach such large numbers.

And no, I cannot simply repeatedly multiply fractions since, as I've said before, this calculator needs to be able to accept noninteger arguments.

I already know of Sterlings approximation, and I don't think I'd find it very useful due to it being a finite formula, as opposed to a more preferred infinite summation, product, or repeated iteration of some function, preferably one that converges quickly.