Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created September 5, 2019 01:38
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 vznvzn/6572eb699c38e3a01f9f02929170d7c0 to your computer and use it in GitHub Desktop.
Save vznvzn/6572eb699c38e3a01f9f02929170d7c0 to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def dense(w, d)
w2 = w - 1
a = (0...w2).to_a
s = '0' * w2
(1..(d * w - 1)).map { a.delete_at(rand(a.size)) }.each { |x| s[x, 1] = '1' }
return ('1' + s)
end
def adj(x, m, n, s2, p2, p, x3)
s2.replace(p[0...x] + (x3 % 2).to_s)
m = (s2[x, 1] == p[x, 1])
n = p2.reverse.to_i(2)
p2[x, 1] = m ? '11' : '01'
return m, n, x3
end
def terras121(p)
p2 = ['01', '11'][p[0, 1].to_i]
n = 1
n3 = 0
m = p2 == '11'
x3 = (p2.reverse.to_i(2)) >> 1
(1...p.length).each \
{
|x|
if (!m)
ns = n.to_s(2)
ns[0, 1] = ''
m1 = x3 - 3**n3
n1 = f2(m1)
end
n3 = p[0...x].split('').select { |z| z == '1' }.size
s2 = ''
m, n, x3 = adj(x, m, n, s2, p2, p, m ? (f2(x3) + 3**n3) : (n1 + 3**n3))
}
n = p2.reverse.to_i(2)
return n
end
def seq(n)
n1 = n
l = []
begin
l << n
n = f2(n)
end while (n != 1)
return l
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def dp(x)
d(x.to_s(2)) >= 0.5
end
def avg(c)
return [c['t'], c['c'].to_f / c['t']]
end
def inc(c, x, l, c1)
c['c'] += x
c['t'] += 1
l << [avg(c), c1]
end
def seed(d, c1, l0, l1, l2)
n = terras121(dense(50, d))
l = seq(n)
a0, a1, da = (0..2).map { {'c' => 0, 't' => 0 } }
l.each_with_index \
{
|x, k|
dpx, dpn = [dp(x), dp(n)]
m = (dpx == dpn) ? 1 : 0
inc(dpn ? a1 : a0, m, dpn ? l1 : l0, c1)
inc(da, d(n.to_s(2)), l2, c1)
n *= 3 if (x.odd?)
}
l0 << []
l1 << []
l2 << []
end
def graph(f, l, t)
f.puts('$dat << eof')
l.each { |x| f.puts(x.join("\t")) }
f.puts('eof')
f.puts("set title '#{t}'; plot $dat using 1:2:3 with line linecolor palette lw 2; unset title; pause -1;")
end
def data()
l0 = []
l1 = []
l2 = []
100.times { |x| seed(x.to_f / 99, x, l0, l1, l2) }
f = File.open('gnuplot.cmd', 'w')
graph(f, l1, 'dpn=1')
graph(f, l0, 'dpn=0')
graph(f, l2.select { |x| x.empty? || x[1] <= 50 }, 'da <= 50')
graph(f, l2.select { |x| x.empty? || x[1] > 50 }, 'da > 50')
f.close
end
data()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment