RosettaCodeData/Task/Evaluate-binomial-coefficients/Tcl/evaluate-binomial-coefficients-1.tcl
Ingy döt Net 764da6cbbb CDE
2013-04-10 16:57:12 -07:00

17 lines
484 B
Tcl

package require Tcl 8.5
proc binom {n k} {
# Compute the top half of the division; this is n!/(n-k)!
set pTop 1
for {set i $n} {$i > $n - $k} {incr i -1} {
set pTop [expr {$pTop * $i}]
}
# Compute the bottom half of the division; this is k!
set pBottom 1
for {set i $k} {$i > 1} {incr i -1} {
set pBottom [expr {$pBottom * $i}]
}
# Integer arithmetic divide is correct here; the factors always cancel out
return [expr {$pTop / $pBottom}]
}