Created
February 1, 2019 01:46
-
-
Save vznvzn/066d9e702361d2cc7820a17a93400303 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 seq(n) | |
l = [n] | |
while (n != 1) | |
n = f2(n) | |
l << n | |
end | |
return l | |
end | |
def count2(l, j, w2, f) | |
a = {} | |
mx = 2 ** w2 | |
l.each_with_index \ | |
{ | |
|n, i| | |
s = n.to_s(2)[-w2..-1] | |
a[s] = a.fetch(s, 0) + 1 | |
f.puts([i, a.size.to_f / mx, j].join("\t")) | |
} | |
f.puts | |
end | |
def sample(l, c) | |
return l if (l.size <= c) | |
r = l.size / c | |
l2 = *(0...l.size).select { |x| x % r == 0 } | |
l2[c..-1] = [] | |
return l.values_at(*l2) | |
end | |
def test(l, w2, f) | |
(0...l.size).to_a.reverse.each \ | |
{ | |
|j| | |
x = l[j] | |
n = x['n'] | |
l1 = seq(n) | |
w = x['ns'].length | |
count2(l1[0..w], j, w2, f) | |
} | |
end | |
def graph(gp, fn) | |
l = File.open(fn).readlines.map { |x| Kernel.eval(x) } | |
l = sample(l, 100) | |
f = File.open(gp, 'a') | |
f.puts('$dat << eof') | |
(4..10).each \ | |
{ | |
|w2| | |
test(l, w2, f) | |
} | |
f.puts('eof') | |
f.puts("set title '#{fn}'; plot $dat using 1:2:3 with line linecolor palette") | |
f.puts('pause -1') | |
f.close | |
end | |
gp = 'gnuplot.cmd' | |
fn1 = 'tmp/bitwise9/mixdb_mx.txt' | |
fn2 = 'tmp/bitwise9/mixdb_a.txt' | |
File.open(gp, 'w').close | |
graph(gp, fn1) | |
graph(gp, fn2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment