Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created June 5, 2015 23:08
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/edfd1d26eafef0fba85b to your computer and use it in GitHub Desktop.
Save vznvzn/edfd1d26eafef0fba85b 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
c2 = 0
n1 = n
a = {}
loop \
{
n2 = n & (p - 1)
x = n2.to_s(2)
a[x] = nil
n21 = n2
while (n >= n1 && n.even? == n2.even?)
# p([n.to_s(2), n2.to_s(2), n2 >= n21, n2])
n = adv(n, w)
n2 = adv(n2, w)
break if (n2 == 1)
c += 1
end
c2 += 1
break if (n < n1)
}
return c, c2, a.keys.size
end
c = ARGV[0].to_i
n = (1 << (c - 1)) + 1
h = {}
while (n.to_s(2).length <= c)
l = f(n, 2)
x = l[2]
h[x] = 0 if (h[x].nil?)
h[x] += 1
n += 2
end
p(h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment