combination function in python

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sun Apr 15 08:15:18 EDT 2007


Steven D'Aprano:
> That's a naive and slow implementation. For even quite small values
> of n and k, you end up generating some seriously big long ints,
> and then have to (slowly!) divide them.
> A better implementation would be something like this:

You are right, thank you for the improvement (the advantage of the
older implementation is that it's naive, so it's a bit more probably
correct compared to more complex code I may write. For Python code I
often tend to write a naive version first, create many testcases,
slowly fixing all the corner cases (like factorial(-5)), and only
later find a faster/better implementation if I have some time to do it
or if I need it. If I need to do lot of binomials the gmpy by Alex
helps).

Bye,
bearophile




More information about the Python-list mailing list