Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created September 17, 2016 01:02
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/e9b6f11ed8565a51abcf014301f0aa6d to your computer and use it in GitHub Desktop.
Save vznvzn/e9b6f11ed8565a51abcf014301f0aa6d 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(x)
n1 = n = x['n']
ns = n.to_s(2)
x['d'] = d(ns)
w = ns.length
x['w'] = w
l = []
c = nil
while (n != 1)
l << n
n = n * 3 + 1 if (n.odd?)
if (n.even?) then
n /= 2
w -= 1
c = l.size if (w == 0)
end
end
x['i2'] = c
x['ls'] = l.size
x['cg'] = (0...l.size).find { |x| l[x] < n1 }
x['cg2'] = (0...l.size).to_a.reverse.find { |x| l[x] > n1 }
x['cm2'] = (0...l.size).max_by { |x| l[x] }
x['g'] = x['cm2'] - x['i2']
return [x]
end
l = [{'n'=>1, 'p'=>0, 'g' => 0}]
seen = {}
10000.times \
{
|i|
$stderr.puts(i) if (i % 100 == 0)
l = l.sort_by! { |x| [x['g'], x['p']] }
l.reverse!
j = rand([l.size, ARGV[0].to_i].min)
z = l.delete_at(j)
n = z['n']
puts([l.size, z['g'], z['w']].join("\t")) if (!seen.member?(n))
$stdout.flush
seen[n] = nil
p = z['p'] + 1
l += [z.merge({'p'=>p})]
l += adv({'n'=>z['n'] + 2**p, 'p'=>p})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment