Created
December 7, 2018 22:55
-
-
Save vznvzn/41577a16b40586fac46c1cca242428a4 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 dense(w, d) | |
w2 = w - 1 | |
a = (0...w2).to_a | |
s = '0' * w2 | |
(1..((d * w).to_i - 1)).map { a.delete_at(rand(a.size)) }.each { |x| s[x, 1] = '1' } | |
return ('1' + s) | |
end | |
def len01(ns) | |
nl = ns.length | |
l1 = ns.split(/0+/).map { |x| x.length } | |
l2 = ns.split(/1+/)[1..-1] | |
l2 = [''] if (l2.nil?) | |
l0 = l2.map { |x| x.length } | |
return l0, l1 | |
end | |
def mx01(ns) | |
return len01(ns).flatten.max | |
end | |
def sum(l) | |
return l.inject(0){ |t, x| t + x } | |
end | |
def h(l, i, s = 1) | |
a = {} | |
l.flatten.each \ | |
{ | |
|x| | |
a[x] = a.fetch(x, 0) + 1 | |
} | |
a.sort_by { |x| x[0] }.each \ | |
{ | |
|x, y| | |
puts([x * s, y, -i].join("\t")) | |
} | |
puts | |
end | |
n1 = n = dense(100, 0.5).to_i | |
l = [] | |
l2 = [] | |
while (n != 1) | |
l << n | |
l2 << len01(n.to_s(2)) | |
n = f2(n) | |
end | |
$stderr.puts(l.size) | |
w = 10 | |
(0..(l.size - w)).to_a.reverse.each \ | |
{ | |
|i| | |
l0, l1 = l2[i...(i + w)].transpose | |
h(l1, i) | |
h(l0, i, -1) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment