Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created October 17, 2018 02:49
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/753e00cf7898cc5a9cc83f913103c9b5 to your computer and use it in GitHub Desktop.
Save vznvzn/753e00cf7898cc5a9cc83f913103c9b5 to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def re(ns, k)
n = ns.to_i(2)
c1 = 0
w = ''
k.times { b = n % 2; w << b.to_s; c1 += b; n = f2(n) }
return n, w, c1
end
def test(msb)
c = 20
3.times \
{
ns = (1..c).map { rand(2) }.join
m1, w, c1 = re(msb + '0' + ns, c)
m2, = re(msb + '1' + ns, c)
t = 3**c1 == m2 - m1
p({'a2' => msb, 'b2' => ns, 'm1' => m1, 'm2' => m2, 'Y - X' => m2 - m1, 'w_i' => w, 'n' => c1, '3^n' => 3**c1, '3^n == Y - X' => t})
raise if (!t)
}
end
test('1')
test('')
test((1..5).map { rand(2) }.join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment