RosettaCodeData/Task/Evaluate-binomial-coefficients/Groovy/evaluate-binomial-coefficients-1.groovy
Ingy döt Net db842d013d A-M baby
2013-04-10 21:29:02 -07:00

10 lines
262 B
Groovy

def factorial = { x ->
assert x > -1
x == 0 ? 1 : (1..x).inject(1G) { BigInteger product, BigInteger factor -> product *= factor }
}
def combinations = { n, k ->
assert k >= 0
assert n >= k
factorial(n).intdiv(factorial(k)*factorial(n-k))
}