Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created May 23, 2018 01:24
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/decf2110ab6c54d74ab4a7a4d2d3e61a to your computer and use it in GitHub Desktop.
Save vznvzn/decf2110ab6c54d74ab4a7a4d2d3e61a 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 total(l)
t = 0
l.each { |x| t += x }
return t
end
def h(n)
ns = n.to_s(2)
nl = ns.length
nl = 1.0
l1 = ns.split(/0+/).map { |x| x.length.to_f / nl }
l0 = [ns.split(/1+/)[1..-1]].compact.flatten.map { |x| x.length.to_f / nl }
return l0, l1
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].reverse
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 read()
l = (f = File.open('tmp/mixdb.txt')).readlines.map { |x| Kernel.eval(x) }
f.close
return uniq(l)
end
# $w = 'packed'
$w = 'unpacked'
l = read().map { |x| count(x) }
mx = l.max_by { |x| x.size }.size
$stderr.puts({'mx' => mx}.inspect)
(0..mx).each \
{
|i|
l1 = l.map { |x| x[i] }.compact
a = {}
l1.each \
{
|x|
j = x.to_s(2).length
a[j] = a.fetch(j, 0) + 1
}
puts([100, 0, i].join("\t"))
a.sort.each \
{
|j, y|
puts([j, y, i].join("\t"))
}
puts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment