Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created October 16, 2018 00:58
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/1f1c904f29bc35150b3446a4e5d2ada3 to your computer and use it in GitHub Desktop.
Save vznvzn/1f1c904f29bc35150b3446a4e5d2ada3 to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def re(n, t)
s = (n % 2).to_s
n1 = n
t.times \
{
n = f2(n)
s << (n % 2).to_s
}
return s
end
p = (1..20).map { rand(2) }.join
p2 = p[0, 1]
(1...p.length).each \
{
|x|
n = p2.reverse.to_i(2)
s = re(n, x)
b = s[-1, 1]
p2 += (b == p[x, 1]) ? '0' : '1'
p([s, n, n.to_s(2).reverse])
}
n = p2.reverse.to_i(2)
s = re(n, p.length - 1)
p([s, n, n.to_s(2).reverse, p == s])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment