Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.