Advertisement
makispaiktis

Birthday Problem

Apr 25th, 2020 (edited)
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. def sameBirthdayInCrowd(n):
  2.     if n != int(n) or n <= 0 or n > 365:
  3.         return None
  4.     product = 1
  5.     for i in range(n):
  6.         product *= (365 - i)/365
  7.     return 1 - product
  8.  
  9. def sameBirthdayWithMe(n):
  10.     if n != int(n) or n <= 0:
  11.         return None
  12.     return 1 - (364/365)**n
  13.  
  14. # MAIN FUNCTION
  15. numbers = list(range(1, 366))
  16. import matplotlib.pyplot as plt
  17. plt.plot([sameBirthdayInCrowd(n) for n in numbers], label="Same Birthday in crowd of n people")
  18. plt.plot([sameBirthdayWithMe(n) for n in numbers], label="Same Birthday with me in crowd of n people")
  19. plt.xlabel("n")
  20. plt.ylabel("f(n)")
  21. plt.legend()
  22. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement