Skip to content

Instantly share code, notes, and snippets.

@vznvzn vznvzn/prefix7.rb
Created Feb 2, 2019

Embed
What would you like to do?
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def seq(n)
l = [n]
while (n != 1)
n = f2(n)
l << n
end
return l
end
def prefix(l, f, m, s = 1)
f.puts('$dat << eof')
l.each_with_index \
{
|n, i|
ns = n.to_s(2)
ns = ns.reverse if (m)
t = 0.0
w = 1.0
ns.split('')[0..9].each_with_index \
{
|x, j|
f.puts([j * s, t, i].join("\t"))
t += [-1, 1][x.to_i] * w
w /= 2
}
f.puts
}
end
def out(l, w, f, m = false)
prefix(l[0..w], f, m, -1)
prefix(l[w..2*w], f, m)
f.puts('eof')
f.puts('plot $dat using 1:2:3 with line linecolor palette')
f.puts('pause -1')
end
fn1 = 'tmp/bitwise9/mixdb_mx.txt'
fn2 = 'tmp/bitwise9/mixdb_a.txt'
l = File.open(fn1).readlines.map { |x| Kernel.eval(x) }
x = l[-1]
n = x['n']
w = x['ns'].length
l = seq(n)
f = File.open('gnuplot.cmd', 'w')
out(l, w, f, true)
out(l, w, f)
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.