#!/usr/local/bin/python3 # Classic birthday problem. What is the probability that in a room with n people, at least two people will have the same birthday. # Input n -- number of people in room # Return -- probability that at least two people will share a birthday def any_birthday_match(n): if n > 365: result = 1 elif n < 2: result = 0 else: prob = 1.0 for i in range(364, 365 - n, -1): prob *= i / 365 result = 1 - prob return result # Probability that in a room of n OTHER people at least one person will share your birthday. # Input n -- number of people in room # Return -- probability that at least one other person will share your birthday def specific_birthday_match(n): if n > 364: result = 1 elif n < 1: result = 0 else: result = 1 - (364 / 365) ** n return result if __name__ == "__main__": import sys n = 23 result = any_birthday_match(n) s = 'In a group of {0:d} people, the probability of two people sharing the same birthday is {1:.4f} .'.format(n, result) print(s) n = 253 result = specific_birthday_match(n) s = '\nIn a group of {0:d} other people, the probability of someone having your birthday is {1:.4f} .'.format(n, result) print(s)