Skip to content

Instantly share code, notes, and snippets.

@vznvzn

vznvzn/construct166.rb

Last active Oct 13, 2020
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 count(ns)
l = seq(n = ns.to_i(2))
nw = ns.length
cg = l.index { |x| x < l[0] }
cg = l.size - 1 if (cg.nil?)
cm = (0..cg).max_by { |x| l[x] }
c = l.size
return {'ns' => ns,
'cm' => cm,
'nw' => nw,
'cg' => cg,
'cgnw' => cg.to_f / nw
}
end
def outd(f, l)
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')
return k
end
def hide(a, x)
return (a.member?(x) && a[x].nil?)
end
def outa(f, l, a = {}, t = '')
k = outd(f, l)
f.puts("set colors classic; set key top left opaque; set title '#{t}'; ")
f.puts("set ytics nomirror; set y2tics;")
f.puts("plot \\")
ct = ''
k, ct = [k - ['t'], "(column('t')):"] if (k.member?('t') && !hide(a, 't'))
k.each \
{
|x|
next if (hide(a, x))
opt = a.fetch(x, '')
opt += ' with line lw 2 ' if (!opt.include?('with'))
f.puts("'$dat' using #{ct}(column('#{x}')) #{opt} title '#{x}',\\")
}
f.puts
# f.puts("reset; pause -1;")
end
def outafn(l, a = {}, fn = nil, t = '')
fn = 'gnuplot.cmd' if (fn.nil?)
outa(f = File.open(fn, 'w'), l, a, t)
f.close
$stderr.puts([fn, l.size, t].inspect)
end
def axes2(x2)
return x2.inject({}) { |h, x| h.merge({x => 'axes x1y2' }) }
end
def x1y2()
return $a2 if (!$a2.nil?)
$a2 = axes2(['cgnw']) if ($a2.nil?)
return $a2
end
def off()
return {'ns' => nil}
end
l = []
1000.times \
{
|i|
x = count('1' * (i + 1))
l << x
$stderr.puts(i) if (i % 100 == 0)
}
outafn(l, off().merge(x1y2()))
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.