Evaluate binomial coefficients: Difference between revisions
Content added Content deleted
(→{{header|Ruby}}: Added Array.combination.size) |
(→{{header|Perl 6}}: using a symmetry property) |
||
Line 1,337: | Line 1,337: | ||
{{out}} |
{{out}} |
||
<pre>10</pre> |
<pre>10</pre> |
||
A useful optimization would use a symmetry property of the binomial coefficient: |
|||
<lang perl6>sub infix:<choose> { [*] ($^n ... 0) Z/ 1 .. min($n - $^p, $p) }</lang> |
|||
One drawback of this method is that it returns a Rat, not an Int. If we really worry about it, we can enforce the conversion: |
One drawback of this method is that it returns a Rat, not an Int. If we really worry about it, we can enforce the conversion: |
||
<lang perl6>sub infix:<choose> { ([*] ($^n ... 0) Z/ 1 .. $^p).Int }</lang> |
<lang perl6>sub infix:<choose> { ([*] ($^n ... 0) Z/ 1 .. min($n - $^p, $p)).Int }</lang> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |