Probability there is a day on Facebook without a birthday
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
module Prob | |
@@cache ||= {} | |
def probability_of_nonbday day_of_year, number_of_friends | |
return @@cache[number_of_friends][day_of_year] if | |
@@cache[number_of_friends] && @@cache[number_of_friends][day_of_year] | |
@@cache[number_of_friends] ||= {} | |
return 0 if day_of_year < 0 | |
@@cache[number_of_friends][day_of_year] = | |
(1-probability_of_nonbday(day_of_year-1,number_of_friends))* | |
(364.to_f/365)**(number_of_friends-day_of_year) + | |
probability_of_nonbday(day_of_year-1,number_of_friends) | |
end | |
end | |
include Prob | |
announcement_triggered = false | |
friends_count = 0 | |
until announcement_triggered | |
if probability_of_nonbday(364,friends_count) < 0.5 | |
puts "At #{friends_count} friends, the probability of having a day without"+ | |
"birthdays is #{probability_of_nonbday(364,friends_count)}" | |
announcement_triggered = true | |
end | |
friends_count += 1 | |
end | |
# At 2482 friends, the probability of having a day withoutbirthdays is 0.49960360037037066 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment