Last active
December 25, 2015 02:19
-
-
Save prakhar1989/6902197 to your computer and use it in GitHub Desktop.
The birthday problem
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
# birthday problem - http://en.wikipedia.org/wiki/Birthday_problem | |
def get_prob(n): | |
p, i = 1.0, 0 | |
while (i < n): | |
p = p * (365 - i) / 365 | |
i += 1 | |
return (1 - p) | |
def func_get_prob(n): | |
""" functional version of the above function """ | |
def iter(n): | |
if (n == 0): | |
return 1 | |
else: | |
return (365.0 - n) / 365 * iter(n-1) | |
return 1 - iter(n-1) | |
def solve_birthday_problem(n, f): | |
print "P(n={:}) = {:.2%}".format(n, f(n)) | |
solve_birthday_problem(23, get_prob) # output: 50.73% | |
solve_birthday_problem(23, func_get_prob) # output: 50.73% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment