Is there a math nCr function in python? -
possible duplicates:
statistics: combinations in python
counting combinations , permutations efficiently
project euler problem in python (problem 53)
i'm looking see if built in math library in python ncr (n choose r) function:
i understand can programmed thought i'd check see if it's built in before do.
the following program calculates ncr
in efficient manner (compared calculating factorials etc.)
import operator op def ncr(n, r): r = min(r, n-r) if r == 0: return 1 numer = reduce(op.mul, xrange(n, n-r, -1)) denom = reduce(op.mul, xrange(1, r+1)) return numer//denom
Comments
Post a Comment