Created
July 25, 2020 04:27
-
-
Save vznvzn/7c4292e52c48c0d762c3c6ed1333f9ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def f2(n) | |
return n.odd? ? (n * 3 + 1) / 2 : n / 2 | |
end | |
def line2(w, c1) | |
x1 = 2.0 ** w | |
mn = (w.to_f / (Math.log(3.0 / 2) / Math.log(2))).to_i | |
x = 1.0 | |
p = '' | |
c = mn + c1 | |
c.times \ | |
{ | |
|i| | |
z = Math.log(x1) / Math.log(2.0) * (i.to_f / c) | |
x2 = Math.log(x) / Math.log(2.0) | |
x, p1 = (x2 < z) ? [(3 * x + 1) / 2, 1] : [x / 2, 0] | |
p += p1.to_s | |
} | |
return p | |
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 | |
def terras121(p) | |
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)) | |
} | |
n = p2.reverse.to_i(2) | |
return n | |
end | |
def seq(n) | |
l = [n] | |
while (n != 1) | |
n = f2(n) | |
l << n | |
end | |
return l | |
end | |
def test1() | |
r = Math.log(2) / Math.log(3.0 / 2) | |
20.times \ | |
{ | |
|i| | |
c1 = i * 10 | |
w = 100 | |
p = line2(w, c1) | |
n = terras121(p) | |
l = seq(n) | |
w1 = l[0].to_s(2).length | |
$stderr.puts([100, c1, w1, w * r + c1, l[w1].to_s(2).length, w * r + c1 + w].inspect) | |
l.each_with_index { |x, i| puts([i - c1, Math.log(x) / Math.log(2.0), c1].join("\t")) } | |
puts | |
} | |
end | |
test1() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment