# Python and Combinatorics

Discussion in 'Python' started by none, Oct 24, 2007.

1. ### noneGuest

Hello,

Is there some package to calculate combinatorical stuff like (n over
k), i.e., n!/(k!(n - k!) ?

I know it can be written in about 3 lines of code, but still...

Thanks,

Ami

none, Oct 24, 2007

2. ### Guest

On Oct 24, 5:20 pm, none <""atavory\"@(none)"> wrote:
> Hello,
>
> Is there some package to calculate combinatorical stuff like (n over
> k), i.e., n!/(k!(n - k!) ?

Sure, the gmpy module.

>>> import gmpy
>>> for m in xrange(10):

for n in xrange(m+1):
print '%4d' % (gmpy.comb(m,n)),
print

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

>
> I know it can be written in about 3 lines of code, but still...
>
> Thanks,
>
> Ami

, Oct 25, 2007

3. ### noneGuest

wrote:
> On Oct 24, 5:20 pm, none <""atavory\"@(none)"> wrote:
>> Hello,
>>
>> Is there some package to calculate combinatorical stuff like (n over
>> k), i.e., n!/(k!(n - k!) ?

>
> Sure, the gmpy module.
>

Excellent, many thanks!

none, Oct 25, 2007
4. ### Alan IsaacGuest

none wrote:
> Is there some package to calculate combinatorical stuff like (n over
> k), i.e., n!/(k!(n - k!) ?

Yes, in SciPy.
Alan Isaac

>>> from scipy.misc.common import comb
>>> help(comb)

Help on function comb in module scipy.misc.common:

comb(N, k, exact=0)
Combinations of N things taken k at a time.

If exact==0, then floating point precision is used, otherwise
exact long integer is computed.

Notes:
- Array arguments accepted only for exact=0 case.
- If k > N, N < 0, or k < 0, then a 0 is returned.

Alan Isaac, Oct 25, 2007
5. ### Gerard FlanaganGuest

On Oct 25, 12:20 am, none <""atavory\"@(none)"> wrote:
> Hello,
>
> Is there some package to calculate combinatorical stuff like (n over
> k), i.e., n!/(k!(n - k!) ?
>
> I know it can be written in about 3 lines of code, but still...
>
> Thanks,
>
> Ami

http://probstat.sourceforge.net/

Gerard Flanagan, Oct 26, 2007