Created
May 19, 2018 22:05
-
-
Save vznvzn/2b54940b8e82908f3809ff1466d38a27 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 f1(n) | |
n = (n * 3 + 1) / 2 while (n.odd?) | |
n /= 2 while (n.even?) | |
return n | |
end | |
def f2(n) | |
return n.odd? ? (n * 3 + 1) / 2 : n / 2 | |
end | |
def count(x) | |
n = n1 = x['n'] | |
l = [n] | |
while (n >= n1 && n != 1) | |
n = {'packed' => true, 'unpacked' => false}[$w] ? f1(n) : f2(n) | |
l << n | |
end | |
cm = (0...l.size).max_by { |x| l[x] } | |
return l[0..cm] | |
end | |
def uniq(l) | |
a = {} | |
l.each { |x| a[x['n']] = x } | |
$stderr.puts({'ls1' => l.size, 'ls2' => a.size}.inspect) | |
return a.values | |
end | |
def out(fn, l1) | |
f = File.open(fn, 'w') | |
l1.reverse.each_with_index \ | |
{ | |
|l2, i| | |
l2.each_with_index \ | |
{ | |
|x, j| | |
f.puts([j, x.to_s(2).length, l1.size - i].join("\t")) | |
} | |
f.puts | |
} | |
f.close | |
end | |
# $w = 'packed' | |
$w = 'unpacked' | |
l = uniq(File.open('tmp/mixdb.txt').readlines.map { |x| Kernel.eval(x) }) | |
l1 = l.map { |x| count(x).reverse } | |
out('out1.txt', l1.sort_by { |x| x.size }) | |
out('out2.txt', l1.sort_by { |x| x[0] }) | |
f = File.open('out3.txt', 'w') | |
l1.each { |x| f.puts([x[0].to_s(2).length, x.size].join("\t")) } | |
f.close | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment