Jump to content

Evaluate binomial coefficients: Difference between revisions

→‎{{header|Perl 6}}: using a symmetry property
(→‎{{header|Ruby}}: Added Array.combination.size)
(→‎{{header|Perl 6}}: using a symmetry property)
Line 1,337:
{{out}}
<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:
<lang perl6>sub infix:<choose> { ([*] ($^n ... 0) Z/ 1 .. min($n - $^p, $p)).Int }</lang>
 
=={{header|PL/I}}==
1,934

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.