Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created June 5, 2015 17:11
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/d0e09f7dcd473b13411d to your computer and use it in GitHub Desktop.
Save vznvzn/d0e09f7dcd473b13411d to your computer and use it in GitHub Desktop.
def adv(n, w)
case w
when 0
return n.even? ? n / 2 : n * 3 + 1
when 1
return n.even? ? n / 2 : (n * 3 + 1) / 2
when 2
n = (n * 3 + 1) if (n.odd?)
n /= 2 while (n.even?)
return n
end
end
def f(n, w)
b = n.to_s(2)
p = 1 << (b.length - 1)
c = 0
n1 = n
a = {}
loop \
{
n2 = n & (p - 1)
x = n2.to_s(2)
a[x] = nil
while (n >= n1 && n.even? == n2.even?)
n = adv(n, w)
n2 = adv(n2, w)
c += 1
end
break if (n < n1)
}
return [c, a.keys.size]
end
c = ARGV[0].to_i
n = (1 << (c - 1)) + 1
while (n.to_s(2).length <= c)
l = [0, 1, 2].map { |w| f(n, w) }
puts([n, l].flatten.join("\t"))
n += 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment