Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
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 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 seq(n)
n1 = n
l = [n1]
while (n != 1)
n = f2(n)
l << n
end
return l
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def count(l)
z = [0, 0]
l.each \
{
|x|
x.split('').each { |y| z[y.to_i] += 1 }
}
return z[0], z[1]
end
def avg(l)
return 0 if (l.nil?)
l.inject { |t, x| t + x }.to_f / l.size
end
f = File.open('gnuplot.cmd', 'w')
f.puts('$dat << eof')
f.puts(['d1', 'ba', 'ma', 'da', 'w'].join("\t"))
w = 500
c = 100
(0...20).each \
{
|j|
w = (j + 1) * 25
(c + 1).times \
{
|i|
d1 = i.to_f / c
n = terras121(dense(100, d1))
l = seq(n)
tb = tm = td = 0
l[0...w].each \
{
|x|
ns = x.to_s(2)
d = d(x.to_s(2))
td += d
b = d > 0.5 ? 1 : 0
tb += b
tm += ns[-1, 1].to_i ^ b ^ 1
}
f.puts([d1, tb.to_f / w, tm.to_f / w, td / w, w].join("\t"))
}
f.puts
}
f.puts('eof')
['ba', 'ma', 'da'].each \
{
|x|
f.puts("plot $dat using (column('d1')):(column('#{x}')):(column('w')) with line linecolor palette lw 2 title '#{x}'; pause -1;")
}
f.close
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.