python 组合数函数_python中的组合函数

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.

解决方案DanielJohnson

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

DanielJohnson:

Please help, I couldnt find the function through help.

You can''t find it because it''s not there:

def factorial(n):

"""factorial(n): return the factorial of the integer n.

factorial(0) = 1

factorial(n) with n<0 is -factorial(abs(n))

"""

result = 1

for i in xrange(1, abs(n)+1):

result *= i

if n >= 0:

return result

else:

return -result

def binomial(n, k):

"""binomial(n, k): return the binomial coefficient (n k)."""

assert n>0 and isinstance(n, (int, long)) and isinstance(k, (int,

long))

if k < 0 or k n:

return 0

if k == 0 or k == n:

return 1

return factorial(n) // (factorial(k) * factorial(n-k))

Bye,

bearophile

On Sun, 15 Apr 2007 02:38:31 -0700, bearophileHUGS wrote:

DanielJohnson:

>Please help, I couldnt find the function through help.

You can''t find it because it''s not there:

def factorial(n):

"""factorial(n): return the factorial of the integer n.

factorial(0) = 1

factorial(n) with n<0 is -factorial(abs(n))

"""

result = 1

for i in xrange(1, abs(n)+1):

result *= i

if n >= 0:

return result

else:

return -result

def binomial(n, k):

"""binomial(n, k): return the binomial coefficient (n k)."""

assert n>0 and isinstance(n, (int, long)) and isinstance(k, (int,

long))

if k < 0 or k n:

return 0

if k == 0 or k == n:

return 1

return factorial(n) // (factorial(k) * factorial(n-k))

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:

def binomial(n, k):

if not 0 <= k <= n:

return 0

if k == 0 or k == n:

return 1

# calculate n!/k! as one product, avoiding factors that

# just get canceled

P = k+1

for i in xrange(k+2, n+1):

P *= i

# if you are paranoid:

# C, rem = divmod(P, factorial(n-k))

# assert rem == 0

# return C

return P//factorial(n-k)

There''s probably even a really clever way to avoid that final division,

but I suspect that would cost more in time and memory than it would save.

--

Steven.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值