Last active
November 13, 2016 19:10
-
-
Save mvw/c855f2d03c257b9ea1c92a96698ca0cf to your computer and use it in GitHub Desktop.
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
# http://math.stackexchange.com/q/2012173/86776 | |
def sim(n, runs) | |
a0 = Array.new | |
(1..n).each do |k| | |
a0 << k | |
end | |
puts "a0 = #{a0}" | |
h = Hash.new | |
found = 0 | |
(1..runs).each do | |
a = a0.clone | |
ok = nil | |
begin | |
d = draw(n, a) | |
ok = !consecutive(d) | |
if not ok | |
put_back(d, a) | |
end | |
end until ok | |
d.sort! | |
if h[d].nil? | |
h[d] = true | |
found += 1 | |
end | |
end | |
i = 0 | |
h.keys.each do |d| | |
i += 1 | |
puts "#{i}. #{d}" | |
end | |
return found | |
end | |
def consecutive(d) | |
if (d[0] - d[1]).abs == 1 | |
return true | |
end | |
if (d[0] - d[2]).abs == 1 | |
return true | |
end | |
if (d[1] - d[2]).abs == 1 | |
return true | |
end | |
return false | |
end | |
def draw(n, a) | |
d = Array.new | |
(1..3).each do | |
begin | |
i = rand(n) | |
end until a[i] != 0 | |
d << a[i] | |
a[i] = 0 | |
end | |
return d | |
end | |
def put_back(d, a) | |
a[d[0] - 1] = d[0] | |
a[d[1] - 1] = d[1] | |
a[d[2] - 1] = d[2] | |
end | |
sim(17, 10000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment