Evaluate binomial coefficients: Difference between revisions

add Ruby
(→‎{{header|Java}}: Added translation of efficient Python)
(add Ruby)
Line 91:
Output:
<pre>10</pre>
 
=={{header|Ruby}}==
{{trans|Tcl}}
 
{{works with|Ruby|1.8.7+}}
<lang ruby>class Integer
# binomial coefficient: n C k
def choose(k)
# n!/(n-k)!
pTop = (self-k+1 .. self).inject(1, &:*)
# k!
pBottom = (2 .. k).inject(1, &:*)
pTop / pBottom
end
end
 
p 5.choose(3)
p 60.choose(30)</lang>
result
<pre>10
118264581564861424</pre>
 
=={{header|Tcl}}==
Anonymous user