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 -- http://mail.python.org/mailman/listinfo/python-list