Created
October 20, 2019 23:23
-
-
Save vznvzn/8565b276d9f13716f478a1ebecb00e85 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 f2(n) | |
return n.odd? ? (n * 3 + 1) / 2 : n / 2 | |
end | |
def dense(w, d) | |
w2 = w - 1 | |
a = (0...w2).to_a | |
s = '0' * w2 | |
(1..(d * w - 1)).map { a.delete_at(rand(a.size)) }.each { |x| s[x, 1] = '1' } | |
return ('1' + s) | |
end | |
def seq(n) | |
n1 = n | |
l = [] | |
begin | |
l << n | |
n = f2(n) | |
end while (n != 1) | |
return l | |
end | |
def outa(f, l, t = '', c) | |
# $stderr.puts([t, c].inspect) | |
f.puts('$dat << eof') | |
k = l[0].keys | |
f.puts(k.join("\t")) | |
l.each { |x| f.puts(x.values.join("\t")) } | |
f.puts('eof') | |
f.puts("set colors classic; set title '#{t}'; plot \\") | |
f.puts("'$dat' using (column('m1')):(column('m2')) lc #{c} pt 5 ps 0.5 title 'm2 vs m1',\\") | |
f.puts | |
f.puts("reset; pause -1;") | |
end | |
def slope(l) | |
n = l.size / 2 | |
m1 = (l[n].to_f - l[0]) / n | |
m2 = (l[-1].to_f - l[n]) / n | |
return {'n' => n, 'm1' => m1, 'm2' => m2} | |
end | |
def slopes(c) | |
l1 = [] | |
l2 = [] | |
c.times \ | |
{ | |
l = seq(dense(100, 0.5).to_i(2)) | |
l1 << slope(l.map { |x| x.to_s(2).length }) | |
l2 << slope(l.map { |x| Math.log(x) }) | |
} | |
return l1, l2 | |
end | |
f = File.open('gnuplot.cmd', 'w') | |
3.times \ | |
{ | |
|n| | |
c = 50 * 4 ** (n + 1) | |
$stderr.puts(c) | |
l1, l2 = slopes(c) | |
outa(f, l1, "c=#{c} bitwidth", 1) | |
outa(f, l2, "c=#{c} log", 2) | |
} | |
f.close | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment