Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created September 17, 2016 01:01
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/061ae3ab0d7aaa9cbe1a4d516316aa45 to your computer and use it in GitHub Desktop.
Save vznvzn/061ae3ab0d7aaa9cbe1a4d516316aa45 to your computer and use it in GitHub Desktop.
def f1(n)
n = (n * 3 + 1) / 2 while (n % 2 == 1)
n /= 2 while (n % 2 == 0)
return n
end
def f(n, w)
n1 = n
c = nil
l = []
while (n >= n1 && n != 1)
l << n
n = n * 3 + 1 if (n.odd?)
if (n.even?) then
n /= 2
c = l.size if (w == 0)
w -= 1
end
end
return c, (0...l.size).max_by { |x| l[x] }
end
l = File.open('db.txt').readlines.map { |z| Kernel.eval(z) }
w = [1, 5, 6, 7]
w.each_with_index \
{
|i, c|
a = (i - 1) * 100
b = a + 99
l2 = l[a..b].select { |x| x['cg'] > 20}.sort_by { |x| x['cg'] }.reverse
l2.each \
{
|x|
n = x['n']
ns = n.to_s(2)
puts([x['cg'], f(n, ns.length)].join("\t"))
}
puts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment