Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created December 30, 2018 19:17
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/7ea043ea16297e0c896cf59b8d7816f1 to your computer and use it in GitHub Desktop.
Save vznvzn/7ea043ea16297e0c896cf59b8d7816f1 to your computer and use it in GitHub Desktop.
def f1(n)
n = (n * 3 + 1) / 2 while (n.odd?)
n /= 2 while (n.even?)
return n
end
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 count(x, l = nil)
n = n1 = x['n']
l = [] if (l.nil?)
l << n
while (n >= n1 && n != 1)
n = $f.call(n)
l << n
end
d = d(ns = n1.to_s(2))
cm = (0...l.size).max_by { |x| l[x] }
nl = ns.length
na = l[nl]
na = 0 if (na.nil?)
w1 = na.to_s(2).length
w2 = l[cm].to_s(2).length
w21 = w2 - w1
w21 = -w21 if (cm < nl)
return x.merge({'da' => (d - 0.5).abs,
'ns' => ns,
'nl' => nl,
'cm' => cm,
'cg' => l.size,
'w1' => w1,
'w2' => w2,
'w21' => w21 * 10})
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 next2(z, l, k)
out(z.select { |k, v| !['n', 'ns', 'p'].member?(k) },
{k => 'lw 2', 'da' => 'axes x1y2 dt 3'}, k) if (z['t'] % 10 == 0)
p = z['p'] + 1
l << [z, count({'n'=>z['n'] + 2 ** p, 'p'=>p, 't' => z['t']}), z.merge({'p'=>p})]
return
end
def hard(k)
$f1 = nil
l = []
next2(count({'n' => 1, 'p' => 0, 't' => 0}), l, k)
add = nil
seen = {}
f = File.open($fn, 'w')
c = 20
5e3.to_i.times \
{
|t|
l1 = (0...l.size).sort_by { |x| [-l[x][1][k], l[x][1]['da']] }
a = {'t1' => t, 'l1' => l.size, 'c' => seen.size}.merge(l[l1[0]][1])
if (!seen.member?(a['n'])) then
seen[a['n']] = nil
f.puts(a)
f.flush
end
$stderr.puts(a.select { |k, | !['n'].member?(k)}) if ((t + 1) % 500 == 0)
i = l1[rand([l1.size, c].min)]
z = l.delete_at(i)
[z[1], z[2]].each { |x| next2(x.merge({'t' => t}), l, k) }
l.delete_at(l1[-1]) if (l.size > 2000)
}
f.close
end
$f = lambda { |x| f2(x) }
$fn = 'mixdb.txt'
hard('w21')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment