Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created May 13, 2018 20:00
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/cbcbdd892b0f009c4c5f3a2f796ce180 to your computer and use it in GitHub Desktop.
Save vznvzn/cbcbdd892b0f009c4c5f3a2f796ce180 to your computer and use it in GitHub Desktop.
def f1(n)
n = (n * 3 + 1) / 2 while (n.odd?)
n /= 2 while (n.even?)
return n
end
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def mx(n)
ns = n.to_s(2)
nl = ns.length
l1 = ns.split(/0+/).map { |x| x.length }
l0 = [ns.split(/1+/)[1..-1]].compact.flatten.map { |x| x.length }
return l0.max, l1.max
end
def count(x, h1, h2, m)
n = n1 = x['n']
l = [n]
while (n >= n1 && n != 1)
n = {'packed' => true, 'unpacked' => false}[$w] ? f1(n) : f2(n)
l << n
end
cm = (0...l.size).max_by { |x| l[x] }
l.each_with_index \
{
|x, i|
mx0, mx1 = mx(x)
j = [mx0, mx1, [mx0, mx1].max][m]
if (i < cm) then
h1[j] = h1.fetch(j, 0) + 1
else
h2[j] = h2.fetch(j, 0) + 1
end
}
return l
end
def out(h, c)
mx = h.map { |x| x[1] }.max
h.sort.each { |k, v| puts([k, v.to_f / mx, c].join("\t")) }
puts
end
def nil20(x)
return x.nil? ? 0 : x
end
# $w = 'packed'
$w = 'unpacked'
l = File.open('tmp/mixdb.txt').readlines.map { |x| Kernel.eval(x) }
a = {}
c = ['mx0', 'mx1', 'mx01']
mxb = mxa = mxd = nil
[0, 1, 2].each \
{
|m|
h1 = {}
h2 = {}
r = (0...l.size)
# r = (0...10)
l[r].each { |x| count(x, h1, h2, m) }
l1 = h1.to_a
l2 = h2.to_a
mxb = [mxb, (l1 + l2).map { |x| x[0] }.max].compact.max
mxa = [mxa, l1.map { |x| x[1] }.max].compact.max
mxd = [mxd, l2.map { |x| x[1] }.max].compact.max
a["#{c[m]}_a"] = h1
a["#{c[m]}_d"] = h2
}
$stderr.puts({'mxb' => mxb, 'mxa' => mxa, 'mxd' => mxd}.inspect)
a2 = {}
mx = {'a' => mxa, 'd' => mxd }
['a', 'd'].each \
{
|d|
c.each \
{
|m|
k = "#{m}_#{d}"
a2[k] = (1..mxb).map { |x| nil20(a[k][x]).to_f / mx[d] }
}
}
f = File.open('out.txt', 'w')
f.puts(a2.keys.join("\t"))
a2.values.transpose.each \
{
|x| f.puts(x.join("\t"))
}
f.close
f = File.open('gnuplot.cmd', 'w')
f.puts("plot \\")
a2.keys.each_with_index \
{
|x, i|
f.puts("'out.txt' using (column('#{x}')) with line lw 2 #{i == 5 ? 'lt 7' : ''}, \\")
}
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment