Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created March 30, 2019 01:12
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/06158c983db9bedd80af9c2413ce1717 to your computer and use it in GitHub Desktop.
Save vznvzn/06158c983db9bedd80af9c2413ce1717 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 keys(ks2)
ks = []
ksmx = []
ks2.each \
{
|x|
if (x.end_with?('-')) then
x = x.dup
x.chop
else
ksmx << x
end
ks << x
}
return ks, ksmx
end
def order(l, ks2)
l2 = (0...l.size).map { {} }
ks, ksmx = keys(ks2)
ks.each \
{
|k|
l1 = (0...l.size).map { |x| {k => l[x][k], 'i' => x} }
s = ksmx.member?(k) ? -1 : 1
l1.sort_by! { |x| s * x[k] }
(0...l1.size).each { |x| l2[l1[x]['i']][k] = x }
}
l2.each { |x| x['z'] = x.values.inject(0) { |t, x| t + x } }
return (0...l2.size).sort_by { |x| l2[x]['z'] }
end
def out(a, a2 = {}, t = nil)
fn = 'out.txt'
k = a.keys
if ($f1.nil?) then
$f1 = File.open(fn, 'w')
$f1.puts(k.join("\t"))
f2 = File.open('gnuplot.cmd', 'w')
st = t.nil? ? 'unset title; ' : "set title '#{t}'; "
f2.puts("set ytics nomirror; set y2tics; set colors classic; set key top left box opaque; #{st}; plot \\")
(a.keys - ['t']).each \
{
|x|
f2.puts("'#{fn}' using (column('t')):(column('#{x}')) with line title '#{x}' #{a2[x]}, \\")
}
f2.puts
f2.close
end
$f1.puts(a.values_at(*k).join("\t"))
$f1.flush
end
def remove(a, ks)
a = a.dup
ks.each { |x| a.delete(x) }
return a
end
def seq1(n)
n1 = n
l = [n]
while (n >= n1 && n != 1)
n = f2(n)
l << n
end
return l
end
def count1(ns, x2)
l = seq1(ns.to_i(2))
nw = ns.length
cm = (0...l.size).max_by { |x| l[x] }
cg = l.size
return {'ns' => ns,
'nw' => nw,
'cg' => cg,
'cm' => cm,
'cgd' => cg - x2['cg'],
'cmd' => cm - x2['cm'],
}
end
def seq2(n)
n1 = n
l = [n]
while (n != 1)
n = f2(n)
l << n
end
return l
end
def count2(ns, x2)
l = seq2(n1 = ns.to_i(2))
nw = ns.length
cm1 = (0...l.size).max_by { |x| l[x] }
cg1 = l.rindex { |x| x > n1 }
cg1 = l.size if (cg1.nil?)
return {'ns' => ns,
'nw' => nw,
'cg' => cg1,
'cm' => cm1,
'cgd' => cg1 - x2['cg'],
'cmd' => cm1 - x2['cm'],
}
end
def count(ns, x2)
return $w == 'count1' ? count1(ns, x2) : count2(ns, x2)
end
def add(l, ns, x2)
$t = 0 if ($t.nil?)
$t += 1
x = count(ns, x2).merge({'t' => $t})
l << x
out(remove(x, ['ns']), {'cgd' => 'lw 2', 'cmd' => 'lw 2 dt 3'}, $w) if ($t % $d == 0)
p(x) if ($t % 200 == 0)
end
def adj(ns)
ns1 = ns.dup
ns1[0, 1] = '10'
ns2 = ns.dup
ns2[0, 1] = '11'
return ns1, ns2
end
def opt(ks)
c = 5e3.to_i
$d = c * 2 / 2000
add(l = [], '1', {'cm' => 0, 'cg' => 0})
c.times \
{
l1 = order(l, ks)
i = l1[rand([l1.size, 500].min)]
x = l.delete_at(i)
ns1, ns2 = adj(x['ns'])
add(l, ns1, x)
add(l, ns2, x)
}
l.sort_by! { |x| x['nw'] }
return l[-1]
end
def fmt1(l, f)
w = l.max_by { |x| x.length }.length
f.puts("set colors classic; set ytics nomirror; set y2tics; set key top right opaque;\\")
f.puts("plot \\")
f.puts("[0:#{l.size - 1}][0:#{w}] '-' matrix using 2:1:3 with image title '',")
# f.puts("'-' using 1 with line title 'e' axes x1y2 lw 3, \\")
# f.puts("'-' using (column('d')) with line axes x1y2 lw 3 ")
l1 = []
l.each_with_index \
{
|z, i|
# l1 << [d(z), e(z)]
c = w - z.length
z[z.length...w] = '0' * c
f.puts(z.split('').join(' '))
}
f.puts("eof")
f.puts("eof")
# l1.each { |x| f.puts(x[1]) }
# f.puts("eof")
# f.puts("d")
# l1.each { |x| f.puts(x[0]) }
# f.puts("eof")
end
def grid(l)
f = File.open(fn = "gnuplot1.cmd", 'w')
f.puts("set palette negative grayscale; unset colorbox;")
fmt1(l.map { |x| x.to_s(2).reverse }, f)
f.close
end
$w = ARGV[0].nil? ? 'count1' : 'count2'
ks = ['nw', 'cm', 'cg', 'cmd', 'cgd']
x = opt(ks)
$stderr.puts(x.inspect)
l = seq1(x['ns'].to_i(2))
grid(l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment