2 回答
TA贡献1784条经验 获得超8个赞
假设您的算法是正确的,则可以按如下方式分析确定total_prob。
这个总结:
prob = 0
for j in range(0, n1+1):
if (j % 2 == 0):
prob += comb(n1, j)
正在计算二项式系数的偶数项,即:
comb(n1, 0) + comb(n1, 2) + ... + comb(n1, J)
where J is even and J>=n1
J > n1 没问题,因为对于 J > n1,comb(n1, J) = 0(nCr 的定义)
这个总和就是来源:
prob = 2**(n1 - 1)
代入total_prob方程中的prob:
total_prob = (2**n0) *(2**(n1-1)) / (2 ** (n0+n1))
total_prob = 2**(n0 + n1 - 1)/(2**(n0+n1))
total_prob = 0.5 (always)
TA贡献1789条经验 获得超8个赞
import math
def comb(n, k): # Calculates the combination based on n and k values
return math.factorial(n) // math.factorial(n - k) //math.factorial(k)
def question(n0, n1): # probability that the total number of coins in the random subset is even
"""probability of sums of even / total probability"""
p = 0
for i in range(0, n1+1):
if (i % 2 == 0):
p += comb(n1, i)
return p / (2 ** n1)
添加回答
举报