combination function in python

Alex Martelli aleax at mac.com
Sun Apr 15 01:58:05 EDT 2007


DanielJohnson <diffuser78 at gmail.com> wrote:

> how to use the combination function in python ?
> 
> For example 9 choose 2 (written as 9C2) = 9!/7!*2!=36
> 
> Please help, I couldnt find the function through help.

If you download and install gmpy, it's easy:

>>> import gmpy
>>> gmpy.comb(9,2)
mpz(36)

However, there's no equivalent function built into Python, if that's
what you're asking (gmpy's a third-party extension).  It's of course
easy to write one for yourself, if you want the functionality but don't
want to download gmpy and don't care for gmpy's superb speed.


Alex




More information about the Python-list mailing list