Advertisement
Guest User

Number of people needed to cover all year with birthdays with p=50%

a guest
Sep 4th, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. # Calculate minimum number of people required, so that the probability
  2. # that their birthdays cover all days of the year is at least 50%
  3.  
  4. BUCKETS=365
  5. p = [0]*(BUCKETS+1)
  6. people = 1
  7. scale = 1
  8. p[1] = 1
  9.  
  10. # p[n] holds probability that after "people" birthdays were randomly selected
  11. # the number of days covered is n. All probabilities are represented as integers
  12. # with the scale factor of BUCKETS**(people-1)
  13. #
  14. # Initial condition: we have one person: people=1
  15. # their birthday covers 1 day with 100% probability: p[1] == 1
  16. # For any other n, p[n] == 0
  17.  
  18. while p[BUCKETS]*2 < scale:
  19.     p1 = [0]*(BUCKETS+1)
  20.     p1[1] = 1
  21.     for n in range(2,BUCKETS+1):
  22.         # Calculate probability that the birthdays cover n days after we pick
  23.         # on more person. This may happen if either
  24.         # - all birthdays previously covered n-1 days and new person's birthday
  25.         #   falls onto a new day; the probability of that is p[n-1] multiplied
  26.         #   by (BUCKETS-(n-1))/BUCKETS
  27.         # - all birthdays already covered n days, and new day is one of the existing
  28.         #   days; the probability of that is p[n]*n/BUCKETS
  29.         #
  30.         # We omit division by BUCKETS to keep the calculation in integers
  31.         p1[n] = p[n-1]*(BUCKETS-n+1) + p[n]*n
  32.     p = p1
  33.     scale *= BUCKETS
  34.     people += 1
  35.     assert sum(p) == scale
  36.     print(people, p[BUCKETS]/scale)
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement