Created
December 30, 2018 04:52
-
-
Save vznvzn/c5fd4b63a3ffc3742b36c3377e5cd0c2 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 count(n, l = []) | |
c = 0 | |
n1 = n | |
while (n > 1) | |
n = f2(n) | |
l << n | |
c += 1 | |
end | |
return c | |
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 sample() | |
l = [] | |
w = 200 | |
100.times \ | |
{ | |
ns = dense(w, 0.5) | |
n = ns.to_i(2) | |
c = count(n, l1 = []) | |
redo if (l1.size <= 600) | |
l << {'ns' => ns, 'l1' => l1} | |
} | |
return l.map { |x| x['l1'][600] }.select { |x| x.to_s(2).length == 50 } | |
end | |
def fmt(l, f) | |
w = l.max_by { |x| x.length }.length | |
f.puts("set colors classic; \\") | |
f.puts("plot \\") | |
f.puts("[0:#{w}][0:#{l.size - 1}] '-' matrix with image title ''") | |
l.each_with_index \ | |
{ | |
|z, i| | |
c = w - z.length | |
z[z.length...w] = '0' * c | |
f.puts(z.split('').join(' ')) | |
} | |
f.puts("eof") | |
f.puts("eof") | |
# f.puts("pause -1;") | |
end | |
def grid(l) | |
f = File.open(fn = "gnuplot1.cmd", 'w') | |
f.puts("set palette negative grayscale; unset colorbox;") | |
fmt(l.map { |x| x.to_s(2).reverse }, f) | |
f.close | |
end | |
l = [] | |
100.times { l += sample() } | |
l.each \ | |
{ | |
|x| | |
count(x, l1 = []) | |
p(l1.size) | |
} | |
l.size.times \ | |
{ | |
ns = dense(50, 0.5) | |
count(ns.to_i(2), l1 = []) | |
p(l1.size) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment