Created
May 21, 2018 00:58
-
-
Save vznvzn/41b0546901b6c587ebb5e0075b3c77cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, l0, l1) | |
ns = n.to_s(2) | |
nl = ns.length | |
l1.concat(ns.split(/0+/).map { |x| x.length.to_f / nl }) | |
l0.concat([ns.split(/1+/)[1..-1]].compact.flatten.map { |x| x.length.to_f / nl }) | |
end | |
def h2(l) | |
l0 = [] | |
l1 = [] | |
l.each \ | |
{ | |
|n| | |
h(n, l0, l1) | |
} | |
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] | |
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) }.sort_by { |x| x.size } | |
l2 = l.map { |x| h2(x)[1].select { |x| x >= 1.0 / 15 }.sort }#.sort_by { |x| x.size } | |
c = 500 | |
f = File.open('out1.txt', 'w') | |
f2 = File.open('out2.txt', 'w') | |
c.times \ | |
{ | |
|i| | |
j = (i.to_f / (c - 1) * (l2.size - 1)).to_i | |
l1 = l2[j] | |
next if (l1.empty?) | |
mx = l1.max | |
mn = l1.min | |
mx += 1 if (mx == mn) | |
l1 = l1.map { |x| (x.to_f - mn) / (mx - mn) } | |
l1.each_with_index \ | |
{ | |
|y, x| | |
f.puts([x.to_f / (l1.size - 1), y, i].join("\t")) | |
} | |
f.puts | |
a = 0.0 | |
b = 1.0 | |
d = nil | |
begin | |
m = a + (b - a) / 2 | |
y = 1.0 - l1[(m * (l1.size - 1)).to_i] | |
if (m < y) then | |
a = m | |
else | |
b = m | |
end | |
end while (b - a > 0.01) | |
f2.puts([m, l1[(m * (l1.size - 1)).to_i]].join("\t")) | |
} | |
f.close | |
f2.close | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment