Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created May 16, 2018 02:25
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/4063589468b3f46213b1069c94700be5 to your computer and use it in GitHub Desktop.
Save vznvzn/4063589468b3f46213b1069c94700be5 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 avg(l)
return 0 if (l.empty?)
t = 0
l.each { |x| t += x }
return t.to_f / l.size
end
def avg2(n)
ns = n.to_s(2)
nl = ns.length
l1 = ns.split(/0+/).map { |x| x.length }
l0 = [ns.split(/1+/)[1..-1]].compact.flatten.map { |x| x.length }
return l0.select { |x| x >= 8 }, l1.select { |x| x >= 8 }
end
def count(x)
n = n1 = x['n']
l = [n]
l0a = []
l1a = []
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] }
l[0..cm].each \
{
|n|
l0, l1 = avg2(n)
l0a.concat(l0)
l1a.concat(l1)
}
return avg(l0a), avg(l1a)
end
# $w = 'packed'
$w = 'unpacked'
l = File.open('tmp/mixdb.txt').readlines.map { |x| Kernel.eval(x) }
puts("0\t1\tcm")
l.sort_by { |x| x['cm'] }.each \
{
|x|
a, b = count(x)
puts([a, b, x['cm']].join("\t"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment