RosettaCodeData/Task/Evaluate-binomial-coefficients/Groovy/evaluate-binomial-coefficients-1.groovy

11 lines
262 B
Groovy
Raw Permalink Normal View History

2013-04-10 21:29:02 -07:00
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))
}