Created
February 6, 2020 01:49
-
-
Save vznvzn/864262a966b57b1436f1fa41d6774214 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 seqg(n) | |
n1 = n | |
l = [n1] | |
while (n != 1 && n >= n1) | |
n = f2(n) | |
l << n | |
end | |
return l | |
end | |
def outa(f, l, a = {}, t = '') | |
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 key top left opaque; set title '#{t}'; ") | |
f.puts("set ytics nomirror; set y2tics;") | |
f.puts("plot \\") | |
k.each \ | |
{ | |
|x| | |
next if (a.member?(x) && a[x].nil?) | |
f.puts("'$dat' using (column('#{x}')) with line #{a.fetch(x, '')} lw 2,\\") | |
} | |
f.puts | |
# f.puts("reset; pause -1;") | |
end | |
def outafn(l, a = {}, fn = nil) | |
outa(f = File.open(fn.nil? ? 'gnuplot.cmd' : fn, 'w'), l, a) | |
f.close | |
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 adj(x, m, n, s2, p2, p, x3) | |
s2.replace(p[0...x] + (x3 % 2).to_s) | |
m = (s2[x, 1] == p[x, 1]) | |
n = p2.reverse.to_i(2) | |
p2[x, 1] = m ? '11' : '01' | |
return m, n, x3 | |
end | |
def terras121(p) | |
p2 = ['01', '11'][p[0, 1].to_i] | |
n = 1 | |
n3 = 0 | |
m = p2 == '11' | |
x3 = (p2.reverse.to_i(2)) >> 1 | |
(1...p.length).each \ | |
{ | |
|x| | |
if (!m) | |
ns = n.to_s(2) | |
ns[0, 1] = '' | |
m1 = x3 - 3**n3 | |
n1 = f2(m1) | |
end | |
n3 = p[0...x].split('').select { |z| z == '1' }.size | |
s2 = '' | |
m, n, x3 = adj(x, m, n, s2, p2, p, m ? (f2(x3) + 3**n3) : (n1 + 3**n3)) | |
} | |
n = p2.reverse.to_i(2) | |
return n | |
end | |
def lenx(ns, p) | |
l = ns.split(p) | |
return [0] if (l.empty?) | |
l.shift if (l[0] == '') | |
return l.map { |x| x.length } | |
end | |
def len1x(ns) | |
return lenx(ns, /0+/) | |
end | |
def len0x(ns) | |
return lenx(ns, /1+/) | |
end | |
def triangle1(c) | |
l = [] | |
c.times \ | |
{ | |
|nw| | |
ns = '1' * (nw + 1) | |
l << ns.to_i(2) | |
} | |
return l | |
end | |
def mx(l1, fn) | |
l1 = [''] if (l1.empty?) | |
l = l1.map { |x| mx = fn.call(x).max; {'mx' => mx, 'mxs' => mx == 0 ? 0 : mx.to_f / x.length} } | |
return {'mx' => l.map { |x| x['mx'] }.max, | |
'mxs' => l.map { |x| x['mxs'] }.max} | |
end | |
def data(l) | |
nw = l[0].to_s(2).length | |
l1 = l[nw..-1].map { |x| x.to_s(2) } | |
# p(l1) | |
mx1 = mx(l1, lambda { |x| len1x(x) }) | |
mx0 = mx(l1, lambda { |x| len0x(x) }) | |
return {'cg' => l.size, 'cgnw' => l.size - nw, | |
'mx0' => mx0['mx'], | |
'mx1' => mx1['mx'], | |
'mx01' => [mx0['mx'], mx1['mx']].max, | |
'mx0s' => mx0['mxs'] * nw, | |
'mx1s' => mx1['mxs'] * nw, | |
'mx01s' => [mx0['mxs'], mx1['mxs']].max * nw | |
} | |
end | |
def graph(l) | |
l2 = [] | |
l.each_with_index \ | |
{ | |
|x, j| | |
l1 = seqg(x) | |
l2 << data(l1) | |
$stderr.puts(j) if (j % 10 == 0) | |
} | |
outafn(l2, {'cg' => 'axes x1y2', 'cgnw' => 'axes x1y2'}) | |
end | |
def glides(t) | |
l1 = [] | |
e = 0.64 | |
t.times \ | |
{ | |
|nw| | |
d = e + rand() * (1.0 - e) | |
p = dense(nw + 1, d) | |
n = terras121(p) | |
l = seqg(n) | |
redo if (l.size < n.to_s(2).length) | |
l1 << n | |
$stderr.puts(nw) if (nw % 10 == 0) | |
} | |
return l1 | |
end | |
l = ARGV[0].nil? ? glides(1000) : triangle1(1000) | |
graph(l) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment