Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created March 9, 2019 05:08
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/cbde13fd8806aa0af91d3d5a83fda16a to your computer and use it in GitHub Desktop.
Save vznvzn/cbde13fd8806aa0af91d3d5a83fda16a to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def dense(w, d)
w2 = w - 1
a = (0...w2).to_a
s = '0' * w2
(1..(d * w - 1)).map { a.delete_at(rand(a.size)) }.each { |x| s[x, 1] = '1' }
return ('1' + s)
end
def len(ns, p)
l = ns.split(p)
l = [''] if (l.nil? || l.empty?)
return l.map { |x| x.length }
end
def len01(ns)
return len(ns, /0+/), len(ns, /1+/)
end
def mx01(ns)
l1, l0 = len01(ns)
return l1.max, l0.max
end
def put01(w)
ns = dense(w, 0.5)
r = (0..1).map { rand(w) }.sort
w1 = r[1] - r[0] + 1
s = ('01' * (w1 / 2 + 1))[0...w1]
ns[r[0]..r[1]] = s
ns1 = '-' * w
ns1[r[0]..r[1]] = s
ns2 = f2(ns.to_i(2)).to_s(2)
return ns, ns2
end
def make(w)
ns = ns2 = r = nil
t = 0
loop \
{
t += 1
ns, ns2 = put01(w)
mx1 = mx01(ns2)[0]
r = mx1.to_f / w
break if (r > 0.45 && r < 0.55)
}
$stderr.puts([r, t].inspect)
return ns
end
def add(l, mxp, n, x)
p = (n % 2).to_s + x['p']
l1m, l0m = mx01(p)
return if ([l0m, l1m].max >= mxp)
w = n.to_s(2).length
l1 = [n] + x['l']
c = x['c'] + (n.even? ? 1 : 0)
l << {'n' => n,
'p' => p,
'w' => n.to_s(2).length,
'r' => c.to_f / w,
'l' => l1,
'c' => c}
end
def back(ns)
mxp = 4
add(l = [], mxp, ns.to_i(2), {'p' => '', 'l' => [], 'c' => 0})
mx = nil
k = 'r'
t = 0
e = 2
loop \
{
return if (l.empty?)
break if (!mx.nil? && mx[k] >= e)
l.sort_by! { |x| -x[k] }
mx = [l[0], mx].compact.max_by { |x| x[k] }
x = l.delete_at(rand([l.size, 50].min))
puts([t, x[k]].join("\t")) if (t % 100 == 0)
n = x['n']
add(l, mxp, (n - 1) / 3, x) if ((n - 1) % 3 == 0 && ((n - 1) / 3).odd?)
add(l, mxp, n * 2, x)
t += 1
}
return mx
end
def reverse(w, mk)
t = 0
x = nil
loop \
{
t += 1
ns = mk.call(w)
x = back(ns)
break if (!x.nil?)
}
# $stderr.puts([t, x].inspect)
return x
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def e(ns)
return len01(ns).flatten.size.to_f / ns.length
end
def fmt1(l, f)
w = l.max_by { |x| x.length }.length
f.puts("set colors classic; set ytics nomirror; set y2tics; set key top right opaque;\\")
f.puts("plot \\")
f.puts("[0:#{l.size - 1}][0:#{w}] '-' matrix using 2:1:3 with image title '', \\")
f.puts("'-' using 1 with line title 'e' axes x1y2 lw 3, \\")
f.puts("'-' using (column('d')) with line axes x1y2 lw 3 ")
l1 = []
l.each_with_index \
{
|z, i|
l1 << [d(z), e(z)]
c = w - z.length
z[z.length...w] = '0' * c
f.puts(z.split('').join(' '))
}
f.puts("eof")
f.puts("eof")
l1.each { |x| f.puts(x[1]) }
f.puts("eof")
f.puts("d")
l1.each { |x| f.puts(x[0]) }
f.puts("eof")
end
def grid(l)
f = File.open(fn = "gnuplot1.cmd", 'w')
f.puts("set palette negative grayscale; unset colorbox;")
fmt1(l.map { |x| x.to_s(2).reverse }, f)
f.close
end
x = reverse(100, lambda { |w| make(w) })
p(x['r'])
n = x['n']
l = [n]
i = 0
while (i < x['p'].length)
i += n.even? ? 1 : 2
n = f2(n)
l << n
end
grid(l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment