binomialCoefficient function Null safety

int binomialCoefficient(
  1. int n,
  2. int k
)

Binomial coefficient ("given N items, how many combinations have K items?").

Uses double-precision floating-point values in browsers, which means that precision issues may appear when factorial values reach 48 bits.

Examples

import 'package:calc/calc.dart';

void main() {
  // "10 choose 5"
  binomialCoefficient(10, 5); // --> 252
}

Implementation

int binomialCoefficient(int n, int k) {
  // TODO: Faster algorithm
  return factorial(n) ~/ (factorial(k) * factorial(n - k));
}