Skip to content

Instantly share code, notes, and snippets.

@robbl
Created November 18, 2011 14:24
Show Gist options
  • Save robbl/1376571 to your computer and use it in GitHub Desktop.
Save robbl/1376571 to your computer and use it in GitHub Desktop.
fusion festival: probability to get fusion tickets by crew size
def next_row row
([0] + row).zip(row + [0]).collect do |l, r|
l + r
end
end
def pascal n
([nil] * n).inject([1]) do |row, y|
y = next_row row
end
end
class Array
def sum
inject(0) { |sum, x| sum+x }
end
end
max_clique_size = 15
puts ".---------------------------------------------------------."
puts "| | combinations | |"
puts "| clique size | overall | we win | we lose | probability |"
puts "+---------------------------------------------------------+"
(1..max_clique_size).to_a.each do |clique_size|
pascal_array = pascal(clique_size)
overall_combinations = 2 ** clique_size
overall_wins = pascal_array[0..pascal_array.size/2 - (clique_size % 2)].sum # odd
overall_loses = overall_combinations - overall_wins
prob = (overall_wins.to_f/overall_combinations.to_f) * 100
puts "| %11s | %7s | %7s | %7s | %11s |" % [clique_size, overall_combinations, overall_wins, overall_loses, "%.2f %" % prob]
end
puts "`---------------------------------------------------------´"
.---------------------------------------------------------.
| | combinations | |
| clique size | overall | we win | we lose | probability |
+---------------------------------------------------------+
| 1 | 2 | 1 | 1 | 50.00 % |
| 2 | 4 | 3 | 1 | 75.00 % |
| 3 | 8 | 4 | 4 | 50.00 % |
| 4 | 16 | 11 | 5 | 68.75 % |
| 5 | 32 | 16 | 16 | 50.00 % |
| 6 | 64 | 42 | 22 | 65.63 % |
| 7 | 128 | 64 | 64 | 50.00 % |
| 8 | 256 | 163 | 93 | 63.67 % |
| 9 | 512 | 256 | 256 | 50.00 % |
| 10 | 1024 | 638 | 386 | 62.30 % |
| 11 | 2048 | 1024 | 1024 | 50.00 % |
| 12 | 4096 | 2510 | 1586 | 61.28 % |
| 13 | 8192 | 4096 | 4096 | 50.00 % |
| 14 | 16384 | 9908 | 6476 | 60.47 % |
| 15 | 32768 | 16384 | 16384 | 50.00 % |
`---------------------------------------------------------´
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment