Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created May 13, 2018 20:01
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/809009112fbb04d39d08e481bb0ae8d5 to your computer and use it in GitHub Desktop.
Save vznvzn/809009112fbb04d39d08e481bb0ae8d5 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, []) + [i.to_f / cm]
else
h2[j] = h2.fetch(j, []) + [(i - cm).to_f / (l.size - cm)]
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']
[0, 1, 2].each \
{
|m|
h1 = {}
h2 = {}
r = (0...l.size)
# r = (0...30)
l[r].each { |x| count(x, h1, h2, m) }
l1 = h1.to_a
l2 = h2.to_a
a["#{c[m]}_a"] = h1
a["#{c[m]}_d"] = h2
}
$stderr.puts(a.keys.inspect)
x = 0
a.each \
{
|k, a2|
a2.sort.each \
{
|mx, l|
t = 0
l.each { |y| t += y }
puts([x, t.to_f / l.size, mx].join("\t"))
x += 1
}
puts
x += 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment