Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created January 5, 2020 23:44
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/454be4e7d73f6752c1a77a244f7589f1 to your computer and use it in GitHub Desktop.
Save vznvzn/454be4e7d73f6752c1a77a244f7589f1 to your computer and use it in GitHub Desktop.
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 left 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
def axes2(x2)
return x2.inject({}) { |h, x| h.merge({x => 'axes x1y2' }) }
end
f = File.open('out.txt', 'w')
l2 = []
(2..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] }
wx = l1[x].to_s(2).length.to_f
wcm = l1[cm].to_s(2).length.to_f
wcg = l1[cg].to_s(2).length.to_f
mx = -wx / (l1.size - x)
mcm = -wcm / (l1.size - cm)
mcg = -wcg / (l1.size - cg)
l2 << {'x' => x,
'mcg' => mcg, 'mcm' => mcm, 'mx' => mx,
'cmx' => cm - x,
'dmxcm' => (mx - mcm) * 1000.0,
}
$stdout.puts(x)
}
f.close
outa(nil, l2, axes2(['mcg', 'mcm', 'mx']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment