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