Created
December 30, 2019 03:40
-
-
Save vznvzn/a13c7ce829f91776feefcc37a6c41005 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 d(s) | |
c = s.split('').select { |x| x == '1' }.size | |
d = c.to_f / s.length | |
return d | |
end | |
def lenx(ns, p) | |
l = ns.split(p) | |
l = [] if (l.nil?) | |
l.shift if (l[0] == '') | |
return l.map { |x| x.length } | |
end | |
def len01x(ns) | |
return lenx(ns, /0+/), lenx(ns, /1+/) | |
end | |
def e(ns) | |
return len01x(ns).flatten.size.to_f / ns.length | |
end | |
def seq(n) | |
n1 = n | |
l = [n1] | |
while (n != 1) | |
n = f2(n) | |
l << n | |
end | |
return l | |
end | |
def outa(f1, l, a = {}, t = '') | |
f = f1.nil? ? File.open('gnuplot.cmd', 'w') : f1 | |
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 right opaque; set title '#{t}'; ") | |
# f.puts("set ytics nomirror; set y2tics;") | |
f.puts("plot \\") | |
(k - ['x']).each \ | |
{ | |
|x| | |
next if (a.member?(x) && a[x].nil?) | |
f.puts("'$dat' using (column('x')):(column('#{x}')) with line #{a.fetch(x, '')} lw 2 title '#{x}',\\") | |
} | |
f.puts | |
# f.puts("reset; pause -1;") | |
f.close if (f1.nil?) | |
end | |
l2 = [] | |
(100..1000).each \ | |
{ | |
|x| | |
ns = '1' * x | |
l1 = seq(ns.to_i(2)) | |
cg = l1.index { |x| x < l1[0] } | |
cg = l1.size if (cg.nil?) | |
cm = (0...cg).max_by { |x| l1[x] } | |
p1 = l1[cm..cg].map { |x| x % 2 }.join | |
p2 = l1[cg..-1].map { |x| x % 2 }.join | |
d1 = d(p1) | |
d2 = d(p2) | |
e1 = e(p1) | |
e2 = e(p2) | |
l2 << {'x' => x, 'd1' => d1, 'd2' => d2, 'e1' => e1, 'e2' => e2} | |
$stdout.puts(x) | |
} | |
outa(nil, l2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment