Created
February 5, 2015 10:45
-
-
Save mhberger/97cbacbe76976f65196f to your computer and use it in GitHub Desktop.
drdrang (Leancrew.com) counting-heads done in ruby
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
#!/usr/bin/env ruby | |
# See http://leancrew.com/all-this/2015/02/counting-heads/ | |
# | |
# Eight people are sitting around a circular table. Each has a coin. They all | |
# flip their coins. What is the probability that no two adjacent people will | |
# get heads? | |
rounds = [] | |
(0..255).each { |i| | |
num = i.to_s(2).rjust(8, '0') | |
if num.include?('11') or (num.start_with?('1') and num.end_with?('1')) | |
rounds << num | |
end | |
} | |
puts "Total Rounds with adjacent Heads #{rounds.size}" | |
puts "Total Rounds with no adjacent Heads #{256 - rounds.size}" | |
puts "Probablity that no adjacent Heads is #{(256 - rounds.size)/256.0}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment