Created
May 16, 2018 02:25
-
-
Save vznvzn/e1d8c543e1725195218ba80df837b9cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, []) + [(cm - i).to_f / cm] | |
h1[j] = h1.fetch(j, []) + [cm - i] | |
else | |
# h2[j] = h2.fetch(j, []) + [-(i - cm).to_f / (l.size - cm)] | |
h2[j] = h2.fetch(j, []) + [-(i - 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...100) | |
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| | |
l.each { |y| puts([x, y, mx].join("\t")) } | |
x += 1 | |
} | |
x += 5 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment