Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created May 23, 2018 01:26
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/03b4ad82907d84548e4f9e2fd70dbf39 to your computer and use it in GitHub Desktop.
Save vznvzn/03b4ad82907d84548e4f9e2fd70dbf39 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 stat(l)
l = [0] if (l.empty?)
t = t2 = 0
l.each \
{
|x|
t += x
t2 += x ** 2
}
c = l.size
a = t.to_f / c
z = t2.to_f / c - a ** 2
sd = Math.sqrt(z < 0 ? 0 : z)
return {'a' => a, 'sd' => sd, 'mx' => l.max, 'mn' => l.min}
end
def total(l)
t = 0
l.each { |x| t += x }
return t
end
def h(n)
return if (n.nil?)
ns = n.to_s(2)
nl = ns.length
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 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]
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 review(l1)
mn = l1.min_by { |x| x.size }.size
$stderr.puts({'mn' => mn}.inspect)
c = 100
l = []
mn.times \
{
|i|
l2 = l1.map { |x| h(x[i]) }.flatten.sort.select { |x| x > 1.0 / 15 }
l << (0...c).map { |j| l2[(j.to_f / c) * (l2.size - 1)] }
}
return l
end
def normcol(l)
w = l[0].size
l0 = (0...w).map { |i| stat(l.map { |x| x[i] }) }
l1 = l.map { |x| (0...w).map { |i| (x[i] - l0[i]['mn']) / (l0[i]['mx'] - l0[i]['mn']) } }
l2 = l.map { |x| (0...w).map { |i| (x[i] - l0[i]['a']) / l0[i]['sd'] } }
return l1, l2
end
def out(fn, l)
f = File.open(fn, 'w')
l.each_with_index \
{
|l1, c|
l1.each_with_index { |x, j| f.puts([j, x, c].join("\t")) }
f.puts
}
f.close
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'
l1, l2 = normcol(review(read().map { |x| count(x).reverse }))
out('out1.txt', l1)
out('out2.txt', l2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment