fac: work out the factorial of any positive integer
comobs: given n amount of things, and picking r amount from it, where order does not matter, how many combos are there?
permutations: give n amount of things, picking r amount from it, where order matters, how many permutations are there?
def fac = {it = it as BigInteger; it <= 1 ? 1 : it * call(it - 1)} // c(n,r) = n!/r!(n-r)! // n = 52 cards ; r = 5 cards per hand // order doesnt matter!! def combos = { n, r -> fac(n).divide(fac(r)*fac(n-r))} // P(n,r) = n!/(n-r)! // order matters!! def permutations = { n, r-> fac(n).divide(fac(n-r))}
combos(52,5) 2598960
permutations(10, 2) 90
For big numbers:
ReplyDeletedef fac
fac = {it, acc=1 -> it <= 1 ? acc : fac.trampoline(it - 1, it*acc)}.trampoline()