Author rhettinger
Recipients kellerfuchs, mark.dickinson, rhettinger, steven.daprano, tim.peters
Date 2018-12-07.00:04:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
+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}
Date User Action Args
2018-12-07 00:04:44rhettingersetrecipients: + rhettinger, tim.peters, mark.dickinson, steven.daprano, kellerfuchs
2018-12-07 00:04:44rhettingersetmessageid: <>
2018-12-07 00:04:44rhettingerlinkissue35431 messages
2018-12-07 00:04:44rhettingercreate