Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created May 19, 2018 22:05
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/2b54940b8e82908f3809ff1466d38a27 to your computer and use it in GitHub Desktop.
Save vznvzn/2b54940b8e82908f3809ff1466d38a27 to your computer and use it in GitHub Desktop.
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