Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Python and Combinatorics (http://www.velocityreviews.com/forums/t546724-python-and-combinatorics.html)

 none 10-24-2007 10:20 PM

Python and Combinatorics

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

 mensanator@aol.com 10-24-2007 11:09 PM

Re: Python and Combinatorics

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

 none 10-24-2007 11:37 PM

Re: Python and Combinatorics

mensanator@aol.com 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!

 Alan Isaac 10-25-2007 09:14 PM

Re: Python and Combinatorics

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.

 Gerard Flanagan 10-26-2007 07:41 AM

Re: Python and Combinatorics

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/

 All times are GMT. The time now is 05:20 AM.