binomial function

double binomial (int n, int k)

Computes the binomial coefficient: n choose k.

Implementation

double binomial(int n, int k) {
  if (k < 0 || n < 0 || k > n) {
    return 0.0;
  }

  return (0.5 + exp(factorialLn(n) - factorialLn(k) - factorialLn(n - k)))
      .floorToDouble();
}