Evaluate binomial coefficients
For a start, you can get the length of the corresponding list of combinations:
say combinations(5, 3).elems;
Output:
10
This method is efficient, as Perl 6 will not actually compute each element of the list, since it actually uses an iterator with a defined count-only
method. Such method performs computations in a way similar to the following infix operator:
sub infix:<choose> { [*] ($^n ... 0) Z/ 1 .. $^p }
say 5 choose 3;
A possible optimization would use a symmetry property of the binomial coefficient:
sub infix:<choose> { [*] ($^n ... 0) Z/ 1 .. min($n - $^p, $p) }
One drawback of this method is that it returns a Rat, not an Int. So we actually may want to enforce the conversion:
sub infix:<choose> { ([*] ($^n ... 0) Z/ 1 .. min($n - $^p, $p)).Int }
And this is exactly what the count-only
method does.