Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created February 1, 2019 01:46
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/066d9e702361d2cc7820a17a93400303 to your computer and use it in GitHub Desktop.
Save vznvzn/066d9e702361d2cc7820a17a93400303 to your computer and use it in GitHub Desktop.
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