Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created October 6, 2016 03:42
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/5349fa571938fa19dfcbc963a334cf34 to your computer and use it in GitHub Desktop.
Save vznvzn/5349fa571938fa19dfcbc963a334cf34 to your computer and use it in GitHub Desktop.
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def adv(x1)
n1 = n = x1['n']
ns = n.to_s(2)
x1['d'] = d(ns)
x1['w'] = ns.length
cg = 0
while (n >= n1 && n != 1)
n = (n * 3 + 1) / 2 while (n.odd?)
n /= 2 while (n.even?)
cg += 1
end
x1['cg'] = cg
x1['g'] = cg - x1['w']
return x1
end
def next2(z)
l = [z]
p = z['p'] + 1
l << adv({'n'=>z['n'] + 2**p, 'p'=>p})
l << z.merge({'p'=>p})
return l
end
l = [next2({'n'=>1, 'p'=>0})]
20000.times \
{
|i|
$stderr.puts(i) if (i % 100 == 0)
l = l.sort_by! { |x| x[1]['g'] }
l.reverse!
j = rand([l.size, 20].min)
z = l.delete_at(j)
l << next2(z[1])
l << next2(z[2])
puts([l.size, z[1]['g'], z[1].inspect].join("\t"))
$stdout.flush
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment