Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created June 27, 2015 18: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/5983cd8d0558acb87959 to your computer and use it in GitHub Desktop.
Save vznvzn/5983cd8d0558acb87959 to your computer and use it in GitHub Desktop.
def f(n)
n2 = n
l = []
while (n >= n2)
l << (n % 2).to_s
n = n.even? ? n / 2 : n * 3 + 1
end
return l
end
def compress(l)
s = 'a'
loop \
{
l2 = (0...l.size).sort_by { |x| l[x..-1] }
z = (0...(l2.size - 1)).to_a.map \
{
|x|
i, j = l2[x], l2[x + 1]
i, j = [j, i] if (i > j)
t = 0
t += 1 while (l[i + t] == l[j + t] && i + t < j && j + t < l2.size)
[i, j, t]
}.sort_by { |x| x[2] }.last
i, j, t = z
break if (t == 1)
s2 = l[j, t]
l[j, t] = s
l[i, t] = s
s = s.succ
}
return l.size
end
def info(n)
return compress(f(n))
end
l = []
n = 3
c = 0
t = ARGV[0].to_i
seen = {}
loop \
{
n += 2
next if (seen.member?(n))
l << [n, info(n)]
next if (l.size < t)
l.sort_by! { |x| [x[1], x[0]] }
x = l.shift
puts((x + [c]).join("\t"))
$stdout.flush
n2 = x[0]
seen[n2] = nil
c += 1
if (c % t == 0) then
n = l.min_by { |x| x[0] }[0]
l = []
end
break if (c == 10000)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment