Created
September 22, 2020 04:54
-
-
Save vznvzn/0d068e98fbb954946bed180b7ed097b6 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) | |
n1 = n | |
l = [n1] | |
while (n != 1) | |
n = f2(n) | |
l << n | |
end | |
return l | |
end | |
def adj(ns) | |
ns1 = ns.dup | |
ns1[0, 1] = '10' | |
ns2 = ns.dup | |
ns2[0, 1] = '11' | |
return ns1, ns2 | |
end | |
def expand() | |
l = [{'ns' => '1'}] | |
20.times \ | |
{ | |
|i| | |
l2 = [] | |
l.each \ | |
{ | |
|x| | |
adj(x['ns']).each \ | |
{ | |
|x| | |
n = x.to_i(2) | |
l1 = seq(n) | |
cg = l1.index { |x| x < l1[0] } | |
cg = l1.size if (cg.nil?) | |
l2 << {'ns' => x, 'l' => l1, 'cg' => cg, 'l1' => l1[i...cg]} if (cg >= x.length) | |
} | |
} | |
l = l2 | |
$stderr.puts(l.map { |x| x['cg'] }.max) | |
} | |
return l | |
end | |
def out(a, fn) | |
f = File.open(fn, 'w') | |
f.puts('$dat << eof') | |
a.sort_by { |k, v| v}.each { |k, v| f.puts([-k[0], k[1], Math.log(v)].join("\t")) } | |
f.puts('eof') | |
f.puts('plot $dat using 1:2:3 with point pt 5 linecolor palette') | |
f.close | |
$stderr.puts([fn, a.size].inspect) | |
end | |
l = expand() | |
$stderr.puts(l.size) | |
a = {} | |
a2 = {} | |
l.each_with_index \ | |
{ | |
|x2, j| | |
c = c2 = 0 | |
x2['l1'].reverse.each_with_index \ | |
{ | |
|x, i| | |
c += [-1, 1][x % 2] | |
c2 += [-1, 3][x % 2] | |
a[[i, c]] = a.fetch([i, c], 0) + 1 | |
a2[[i, c2]] = a2.fetch([i, c2], 0) + 1 | |
} | |
} | |
out(a, 'gnuplot.cmd') | |
out(a2, 'gnuplot2.cmd') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment