Compute the probability in the birthday problem: given n people, what is the probability that at least two share the same birthday? Assume 365 equally likely birthdays.
def birthday_probability(n: int, days: int = 365) -> float:
"""
n: number of people
days: number of possible birthdays (default 365)
Returns: probability that at least two people share a birthday
"""
if n > days:
return 1.0
if n <= 1:
return 0.0
# P(no collision) = (365/365) * (364/365) * ... * ((365-n+1)/365)
p_no_collision = 1.0
for i in range(n):
p_no_collision *= (days - i) / days
return round(1.0 - p_no_collision, 6)