Guest User

Untitled

a guest
Apr 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. # Birthday.rb -- happy bday to EY employees everywhere
  2. # with love from san diego :p, David Salamon 2008
  3.  
  4. def no_shared_birthdays_for(people, days=365)
  5. return 1.0 if people == 0
  6. return ((days - 1.0) / 365) * no_shared_birthdays_for(people - 1, days - 1)
  7. end
  8.  
  9. puts "Section one... how unlikely are collisions with different employee head counts?"
  10. (2..60).each {|e| puts " - with #{e} employees : #{1.0 - no_shared_birthdays_for(e)}" }
  11.  
  12.  
  13. def choose(n, m)
  14. m = n - m if m < n - m
  15. accumulator = 1.0
  16. (n-m).times {|i| accumulator *= (m + i + 1) }
  17. (n-m).times {|i| accumulator /= (i + 1)}
  18. accumulator.to_i
  19. end
  20.  
  21. def shared_birthdays_for(people, days=365)
  22. return 1 if people == 0 || people == 1
  23. return (1.0/days) * shared_birthdays_for(people-1, days)
  24. end
  25.  
  26. $employee_count = 50
  27. (2..10).each {|e|
  28. chance = choose($employee_count, e) * shared_birthdays_for(e) * no_shared_birthdays_for($employee_count - e)
  29. puts "chance of *exactly* #{e} people sharing a birthday: #{chance}"
  30. }
Add Comment
Please, Sign In to add comment