Created
September 16, 2016 00:51
-
-
Save vznvzn/81f9c85eb38f35cdab6ff3056102774a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def d(s) | |
c = s.split('').select { |x| x == '1' }.size | |
d = c.to_f / s.length | |
return d | |
end | |
def adv(x) | |
p = x['p'] | |
n1 = n = x['n'] | |
ns = n.to_s(2) | |
x['d'] = d(ns) | |
x['ns'] = ns.length | |
return [x] if (n == 1) | |
while (n >= n1 && n != 1) | |
n = n * 3 + 1 if (n.odd?) | |
if (n.even?) then | |
n /= 2 | |
p -= 1 | |
return [x] if (p == 0) | |
end | |
end | |
return [] | |
end | |
l = adv({'n'=>1, 'p'=>0}) | |
seen = {} | |
5000.times \ | |
{ | |
j = (0...l.size).min_by { |x| l[x]['d'] } | |
z = l.delete_at(j) | |
n = z['n'] | |
puts([z['ns'], z['d'], l.size].join("\t")) if (!seen.member?(n)) | |
$stdout.flush | |
seen[n] = nil | |
p = z['p'] + 1 | |
l += adv({'n'=>z['n'] + 2**p, 'p'=>p}) | |
l += adv(z.merge({'p'=>p})) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment