Created
May 29, 2018 02:12
-
-
Save vznvzn/a89713c301908b19eae805b6ed278cc3 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 count(x, w) | |
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] } | |
return l[0..cm] | |
end | |
def uniq(l) | |
a = {} | |
l.each { |x| a[x['n']] = x } | |
# $stderr.puts({'ls1' => l.size, 'ls2' => a.size}.inspect) | |
return a.values | |
end | |
def read() | |
l = (f = File.open('tmp/mixdb.txt')).readlines.map { |x| Kernel.eval(x) } | |
f.close | |
return uniq(l) | |
end | |
def mid(l) | |
l = l.sort_by { |x| x.size } | |
mn = (l.size * 0.02).to_i | |
mx = (l.size * 0.90).to_i | |
return l[0...mn], l[mn..mx], l[mx..-1] | |
end | |
def cmd(x = nil) | |
f = File.open('gnuplot.cmd', x.nil? ? 'w' : 'a') | |
f.puts("#{x}; pause -1;") if (!x.nil?) | |
f.close | |
end | |
def resample(l) | |
la, l, lb = mid(l) | |
l0 = l.map { |x| x.size } | |
l = l.dup | |
mn = l[0].size | |
mx = l[-1].size | |
l1 = [] | |
c = 115 | |
a = {} | |
l2 = (0...l.size).to_a | |
(c + 1).times \ | |
{ | |
|x| | |
r = x.to_f / c | |
t = mn + r * (mx - mn) | |
i = (0...l.size).min_by { |x| (l[x].size - t).abs } | |
l1 << l.delete_at(i) | |
a[l2.delete_at(i)] = nil | |
} | |
f = File.open('out1.txt', 'w') | |
la.each { |x| f.puts(x.size) } | |
l0.each_with_index { |x, i| f.puts([x, a.member?(i) ? x : nil].join("\t")) } | |
lb.each { |x| f.puts(x.size) } | |
f.close | |
f2 = File.open('out2.txt', 'w') | |
l1.each { |x| f2.puts(x.size) } | |
f2.close | |
cmd() | |
cmd("plot 'out1.txt' using 1 with line, '' using 2 pt 5 ps 0.5,'out2.txt' using 1 axes x2y2 pt 5 ps 0.5") | |
return l1 | |
end | |
def widthdiff(x, y) | |
y.to_s(2).length - x.to_s(2).length | |
end | |
def widthratio(x, y) | |
y.to_s(2).length.to_f / x.to_s(2).length | |
end | |
def out(fn, l) | |
f = File.open(fn, 'w') | |
l.each_with_index \ | |
{ | |
|l1, i| | |
f.puts([l1.size, widthdiff(l1[0], l1[-1]), widthratio(l1[0], l1[-1])].join("\t")) | |
} | |
f.close | |
end | |
l = read().map { |x| count(x, 'unpacked') } | |
l.sort_by! { |x| x.size } | |
out('outx.txt', l) | |
out('outy.txt', resample(l)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment