Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created January 17, 2020 00:12
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/06759a80f64c376034eb6b2595208a46 to your computer and use it in GitHub Desktop.
Save vznvzn/06759a80f64c376034eb6b2595208a46 to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
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 outa(f, l, a = {}, c = '')
f.puts('$dat << eof')
k = l[0].keys
f.puts(k.join("\t"))
l.each { |x| f.puts(x.values.join("\t")) }
f.puts('eof')
f.puts("set colors classic; set key top left opaque; ")
f.puts("set ytics nomirror; set y2tics;")
f.puts("plot \\")
k.each \
{
|x|
next if (a.member?(x) && a[x].nil?)
f.puts("'$dat' using #{c}(column('#{x}')) with line #{a.fetch(x, '')} lw 2 title '#{x}',\\")
}
f.puts
# f.puts("reset; pause -1;")
end
def msbs(n3, ns)
i = 0
i += 1 while (n3[i, 1] == ns[i, 1] && i < n3.length && i < ns.length)
return i
end
l = File.open('db.txt').readlines.map { |x| Kernel.eval(x) }
ks = l.map { |x| x['k'] }.uniq
p(ks)
ks.sort_by! { |k| l.select { |x| x['k'] == k }.map { |x| x['cg'] }.max }
p(ks)
l2 = []
x1 = 0
ks.reverse.each \
{
|k|
x = l.select { |x| x['k'] == k }.max_by { |x| x['cg'] }
$stderr.puts(x)
n3 = n = x['n']
l1 = seq(n)
cg = l1.index { |x| x < n }
p(cg)
l1[0..(cg + 200)].each_with_index \
{
|x, j|
ns = x.to_s(2)
l2 << {'x1' => x1,
'nw' => ns.length,
'msb' => msbs(n3.to_s(2), ns),
}
x1 += 1
n3 *= 3 if (x.odd?)
}
x1 += 200
l2 << {}
}
outa(File.open('gnuplot.cmd', 'w'), l2,
{'x1' => nil,
},
"(column('x1')):")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment