Skip to content

Instantly share code, notes, and snippets.

@prakhar1989
Last active December 25, 2015 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prakhar1989/6902197 to your computer and use it in GitHub Desktop.
Save prakhar1989/6902197 to your computer and use it in GitHub Desktop.
The birthday problem
# 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