[issue35431] The math module should provide a function for computing binomial coefficients

Raymond Hettinger report at bugs.python.org
Thu Dec 6 19:04:44 EST 2018


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

+1 I have wanted this a number of times.

FWIW, most recently I wrote it like this:

    def comb(n, r):
        'Equivalent to factorial(n) // (factorial(r) * factorial(n-r))'
        c = 1
        r = min(r, n-r)
        for i in range(1, r+1):
            c = c * (n - r + i) // i
        return c

I'm not sure is this is better than a single divide, but it kept the intermediate values from exploding in size, taking advantage of cancellation at each step.

Also, I'm not sure what the predominant choice for variable names should be, "n things taken r at a time" or "n things taken k at time".

Also, it's worth considering whether the function should be called "binomial", "choose", "combinations", or "comb".  The word "binomial" seems too application specific but would make sense if we ever introduced a "multinomial" counterpart.  The word "choose" is how we usually pronounce it.  The word "combinations" fits nicely with the related counting functions, "combinations, permutations, and factorial".  The word "comb" is short, works well with "perm" and "fact", and nicely differentiates itself as the integer counterparts of the combinatoric functions in the itertools module.

Wolfram uses both choose and Binomial[n,m]
SciPy uses comb(n, k).
Maple uses both numbcomb(n,m) and binomial(n,m).
TeX uses {n \choose k}

----------
nosy: +mark.dickinson, rhettinger, tim.peters

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35431>
_______________________________________


More information about the Python-bugs-list mailing list