Skip to content

Instantly share code, notes, and snippets.

@StevenClontz
Last active August 29, 2015 14:16
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 StevenClontz/7504581f8d4a3526f96b to your computer and use it in GitHub Desktop.
Save StevenClontz/7504581f8d4a3526f96b to your computer and use it in GitHub Desktop.
Probability there is a day on Facebook without a birthday
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