Python and Combinatorics

N

none

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
 
M

mensanator

Hello,

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

Sure, the gmpy module.
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
 
A

Alan Isaac

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

Yes, in SciPy.
Alan Isaac
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top