Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created October 7, 2018 20:03
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/d6ec1796c2a8517387c687ed7bd22d2f to your computer and use it in GitHub Desktop.
Save vznvzn/d6ec1796c2a8517387c687ed7bd22d2f to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def climb(n)
n1 = n
l = [n]
begin
n = f2(n)
l << n
end while (n > 2)
case 'global'
when 'global'
m = (0...l.size).max_by { |x| l[x] }
b = m
b += 1 while (l[b] > n1)
when 'local'
b = (0...l.size).to_a.index { |x| l[x] < n1 }
m = (0..b).max_by { |x| l[x] }
end
l = l.map { |x| x.to_s(2) }
return l[0..m], l[m..b], l[(b + 1)..-1]
end
def sample(l, c)
r = l.size / c
l2 = *(0...l.size).select { |x| x % r == 0 }
l2[c..-1] = []
return l.values_at(*l2)
end
def sum(l)
t = 0.0
l.each { |x| t += x }
return t
end
def avg(l)
return nil if (l.empty?)
return sum(l) / l.size
end
def len01(ns)
nl = ns.length
l1 = ns.split(/0+/).map { |x| x.length }
l2 = ns.split(/1+/)[1..-1]
l2 = [''] if (l2.nil?)
l0 = l2.map { |x| x.length }
return l1
return l0 + l1
end
fns = ['tmp/bitwise8/out/10/mixdb.txt',
'tmp/bitwise9/mixdb_a.txt',
'tmp/bitwise9/mixdb_mx.txt']
fn = fns[ARGV[0].to_i]
l = File.open(fn).readlines.map { |x| Kernel.eval(x) }
c = 20
h = [0] * c
f = File.open('out.txt', 'w')
l.each_with_index \
{
|x, j|
n = x['n']
l1, l2, = climb(n)
l = l1
# l = l2
$stderr.puts(l.size)
mx = l.map { |x| len01(x) }.flatten.max
l0 = (0...l.size).select { |x| len01(l[x]).member?(mx) }
l0.map { |x| x.to_f / (l.size + 1)}.each \
{
|x|
f.puts([mx, j, x].join("\t"))
h[(x * c).to_i] += 1
}
}
f.close
f = File.open('out1.txt', 'w')
h.each { |x| f.puts(x) }
f.close
f = File.open('gnuplot.cmd', 'w')
f.puts("set title '#{fn}'; set x2tics; set xtics nomirror; set y2tics; set ytics nomirror; " +
"plot 'out.txt' using 2:3:1 with points lt palette pt 7 ps 0.5,'out1.txt' using 1 axes x2y2 with line lw 4")
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment