擅长:python、mysql、java
<p>您试图计算的是<a href="https://www.hackerrank.com/challenges/s10-binomial-distribution-1/tutorial" rel="nofollow noreferrer">Cumulative Probability of a Binomial Distribution</a>。因此,对于<code>x</code>的人口,你想知道至少有两个人的生日在同一天的概率。您可以使用以下选项:</p>
<pre><code>import math
# x is the size of your population
def birthday(x):
# Probability of having birthay in a specific day of the year
p = 1/365
q = 1-p
acc = 0
# Summing from 2 until size of population (you need at least 2)
for i in range(2,x+1):
acc += (math.factorial(n)*(p**i)*(q**(n-i)))/(math.factorial(i)*math.factorial(n-i))
return acc
</code></pre>