Skip to content

Instantly share code, notes, and snippets.

@mhberger
Created February 5, 2015 10:45
Show Gist options
  • Save mhberger/97cbacbe76976f65196f to your computer and use it in GitHub Desktop.
Save mhberger/97cbacbe76976f65196f to your computer and use it in GitHub Desktop.
drdrang (Leancrew.com) counting-heads done in ruby
#!/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