Last active
October 6, 2015 11:47
-
-
Save jkbrzt/2988482 to your computer and use it in GitHub Desktop.
Birthday paradox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
from operator import mul | |
def Q(n): | |
""" | |
Let Q(n) be the probability that | |
no two people have a common birthday. | |
""" | |
y = 365 | |
# The number of n birthdays without any duplicates. | |
dd = reduce(mul, range(y - n + 1, y + 1)) | |
# The total number of n possible birthdays. | |
dr = y ** n | |
return dd / dr | |
def P(n): | |
"""Let P(n) be the probability in question.""" | |
return 1 - Q(n) | |
print P(30) | |
print P(23) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment