Skip to content

Instantly share code, notes, and snippets.

@mhberger
Last active August 29, 2015 14:14
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 mhberger/d49733e793c3d1e94329 to your computer and use it in GitHub Desktop.
Save mhberger/d49733e793c3d1e94329 to your computer and use it in GitHub Desktop.
drdrang (Leancrew.com) counting-heads done in Groovy
#!/usr/bin/env groovy
// 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 {
def num = Integer.toBinaryString(it).padLeft(8, '0')
if (num.contains('11') || (num.startsWith('1') && num.endsWith('1'))) {
rounds << num
}
}
System.out.println "Total Rounds with adjacent Heads ${rounds.size()}"
System.out.println "Total Rounds with no adjacent Heads ${256 - rounds.size()}"
System.out.println "Probablity that no adjacent Heads is ${(256 - rounds.size())/256}"
// assert 209 == rounds.size()
// assert 47 == 256 - rounds.size()
// assert 0.18359375 == (256 - rounds.size())/256
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment