Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created October 19, 2018 05:05
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/913c2527a2abc441351897c64f1a0f63 to your computer and use it in GitHub Desktop.
Save vznvzn/913c2527a2abc441351897c64f1a0f63 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
# s = ''
n1 = n
t.times \
{
n = f2(n)
s << (n % 2).to_s
break if (n == 1)
}
return s, n
end
p = (1..20).map { rand(2) }.join
p2 = ['01', '11'][p[0, 1].to_i]
(1...p.length).each \
{
|x|
n = p2.reverse.to_i(2)
s, n2 = re(n, x)
b = s[x, 1]
p2[x..-1] = (b == p[x, 1]) ? '11' : '01'
p({'k' => x, 'w_i' => s, 'n' => n, 'n_2' => n.to_s(2).reverse, 'X' => n2, 't' => s[0...x] == p[0...x]})
}
n = p2.reverse.to_i(2)
s, n2 = re(n, p.length - 1)
p({'k' => p.length, 'w_i' => s, 'n' => n, 'n_2' => n.to_s(2).reverse, 't' => p == s})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment