Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created May 23, 2018 01:27
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/93c42f15a7fe47ae5b2e0d50afff8198 to your computer and use it in GitHub Desktop.
Save vznvzn/93c42f15a7fe47ae5b2e0d50afff8198 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, w)
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 h1(n)
ns = n.to_s(2)
nl = ns.length
l = ns.split(/0+/)
return l.map { |x| x.length }.max, l.map { |x| x.length.to_f / nl }.max
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 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 out(fn, w2, l1)
l0 = []
l1.reverse.each_with_index \
{
|l2, i|
l = l2.map { |x| x.to_s(2).length }
(0...(l2.size - 1)).each \
{
|j|
mx, mxs = h1(l2[j])
mx = (Math.log(mx) * 30).to_i
l0 << {'md' => j, 'w1' => l[j], 'w2' => l[j + 1], 'mx' => mx, 'mxs' => mxs}
}
}
if (w2 == 'scaled') then
s1 = stat(l0.map { |x| x['mx'] })
s2 = stat(l0.map { |x| x['mxs'] })
l0.each { |x| x['mx'] = (Math.log(2 + (x['mxs'] - s2['a']) / s2['sd'] - (x['mx'] - s1['a']) / s1['sd']) * 100).to_i }
end
f = File.open(fn, 'w')
l0.sort_by { |x| x['mx'] }.each \
{
|x|
f.puts([x['md'], x['w1'], x['mx']].join("\t"))
f.puts([x['md'] + 1, x['w2'], x['mx']].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
i = 1
['unpacked', 'packed'].each \
{
|w|
['unscaled', 'scaled'].each \
{
|w2|
out("out#{i}.txt", w2, read().map { |x| count(x, w).reverse })
$stderr.puts([i, w, w2].inspect)
i += 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment