Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created October 19, 2018 05:06
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/f1b5dc1c07f1f7c6fe7075bc1c7b7331 to your computer and use it in GitHub Desktop.
Save vznvzn/f1b5dc1c07f1f7c6fe7075bc1c7b7331 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
break if (n == 1)
}
return s, n
end
def adj(x, m, n, s2, p2, p, x3)
s2.replace(p[0...x] + (x3 % 2).to_s)
m = (s2[x, 1] == p[x, 1])
n = p2.reverse.to_i(2)
p2[x, 1] = m ? '11' : '01'
return m, n, x3
end
#srand(5)
p = (1..20).map { rand(2) }.join
p2 = ['01', '11'][p[0, 1].to_i]
n = 1
n3 = 0
m = p2 == '11'
x3 = (p2.reverse.to_i(2)) >> 1
(1...p.length).each \
{
|x|
if (!m)
ns = n.to_s(2)
ns[0, 1] = ''
m1 = x3 - 3**n3
n1 = f2(m1)
end
n3 = p[0...x].split('').select { |z| z == '1' }.size
s2 = ''
m, n, x3 = adj(x, m, n, s2, p2, p, m ? (f2(x3) + 3**n3) : (n1 + 3**n3))
t = s2[0...x] == p[0...x]
p({'k' => x, 'w_i' => s2, 'n' => n, 'n_2_r' => n.to_s(2).reverse, 'X' => x3, 't' => t})
s, n2 = re(n, x)
raise if (!t)
raise if (s != s2)
raise if (n2 != x3)
}
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