combination function in python

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sun Apr 15 15:33:29 EDT 2007


Mark Dickinson:
> def choose(n, k):
>     ntok = 1
>     for t in xrange(min(k, n-k)):
>         ntok = ntok*(n-t)//(t+1)
>     return ntok

With few tests, it seems this is faster than the version by Jussi only
with quite big n,k.

(Another test to be added, is the assert n>0 of my original version
(testing that n,k seem useful too)).

Bye,
bearophile




More information about the Python-list mailing list