binomial method
Returns the binomial coefficient of this int and the argument k
,
This is the number of ways, disregarding order, that k
objects can be
chosen from among n
objects.
Implementation
int binomial(int k) {
var n = this;
if (k < 0 || k > n) {
throw ArgumentError('$n.binomial($k) is undefined');
}
if (k == 0 || k == n) {
return 1;
}
if (k == 1 || k == n - 1) {
return n;
}
if (k > n - k) {
k = n - k;
}
var r = 1;
for (var i = 1; i <= k; i++) {
r *= n--;
r = r ~/ i;
}
return r;
}