Created
December 10, 2015 21:08
-
-
Save vznvzn/e187873021745baa099b 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 mx(a, b) | |
a.nil? ? b : [a, b].max | |
end | |
def mn(a, b) | |
a.nil? ? b : [a, b].min | |
end | |
def dense(n) | |
s = n.to_s(2) | |
c = 0 | |
s.length.times \ | |
{ | |
|i| | |
c += s[i, 1].to_i | |
} | |
return c.to_f / s.length | |
end | |
def widthdiff(x, y) | |
x.to_s(2).length - y.to_s(2).length | |
end | |
def widthratio(x, y) | |
x.to_s(2).length.to_f / y.to_s(2).length | |
end | |
def f(n, w) | |
n1 = n | |
c = 0 | |
l = [n] | |
cg = nil | |
while (n != 1) | |
n = f1(n) | |
l << n | |
c += 1 | |
cg = c if (cg.nil? && n < n1) | |
break if (!cg.nil? && w != 'c') | |
end | |
cm = (0...l.size).max_by { |x| l[x] } | |
return {'cm' => cm, | |
'cg' => cg.nil? ? 0 : cg, | |
'm' => l[cm].to_s(2).length, | |
'w' => widthdiff(l[cm], n1), | |
'r' => widthratio(l[cm], n1), | |
'c' => l.size, | |
'd' => (0.5 - dense(n1)).abs} | |
end | |
def long(m, w) | |
return {'n' => rand(2 ** m) | 1 } if (w == 'z') | |
l = [{'n' => 1, 'p' => 0}.merge(f(1, w))] | |
t = 0 | |
e = 2000 | |
loop \ | |
{ | |
l = l.sort_by { |x| [x[w], -x['d']] }.reverse | |
x = l.delete_at(rand([l.size, 20].min)) | |
p2 = x['p'] + 1 | |
n2 = x['n'] + 2 ** p2 | |
x1 = x.dup | |
x1['p'] = p2 | |
l << x1 | |
x2 = {'n' => n2, 'p' => p2}.merge(f(n2, w)) | |
l << x2 | |
t += 1 | |
w2 = w | |
if (w == 'r') then | |
z = l.sort_by { |x| x['n'] }.reverse[0...100].max_by { |x| x['r'] } | |
w2 = 's' | |
else | |
z = l.max_by { |x| x[w] } | |
end | |
z.merge!({ 't' => t, 's' => t.to_f / e }) | |
return z if (z[w2] >= m) | |
return if (t >= e) | |
} | |
end | |
def adv(n) | |
l = [] | |
s = '' | |
while (n != 1) | |
l << n | |
while (n.odd?) | |
n = (n * 3 + 1) / 2 | |
s << '1' | |
end | |
while (n.even?) | |
n /= 2 | |
s << '0' | |
end | |
end | |
return l | |
end | |
def ahead(m, l) | |
t = [0] * 2 | |
l.each \ | |
{ | |
|l1| | |
(m...l1.size).each \ | |
{ | |
|x| | |
a = l1[m - 1] | |
d = widthdiff(l1[x], a) | |
t[d < 0 ? 0 : 1] += d | |
} | |
} | |
return t.map { |x| x.abs * (x < 0 ? -1 : 1) } | |
end | |
def delta(n) | |
l = [] | |
c = 100 | |
c.times { l << adv(n); n += 2 } | |
return ahead(1, l) | |
end | |
def clear() | |
$c = [0] * 3 | |
$a = {} | |
$x = 0 if ($x.nil?) | |
end | |
def test(p, k, i, m) | |
r = p[k] | |
a, b, c = r | |
return if (c && m >= 1.0) | |
x = a + m * (b - a) | |
z = long(x, k) | |
clear() if ($c.nil?) | |
$c[0] += 1 | |
return if (z.nil?) | |
$c[1] += 1 | |
n = z['n'] | |
return if ($a.member?(n)) | |
$c[2] += 1 | |
$a[n] = nil | |
$stderr.puts([$c, k, r2(m), r2(x), z['s'], n.to_s(2)].inspect) | |
puts([$x, i, delta(n)].join("\t")) | |
$x += 1 | |
$stdout.flush | |
end | |
def r2(x) | |
sprintf("%.2f", x).to_f | |
end | |
def all(p) | |
p.keys.each_with_index \ | |
{ | |
|k, i| | |
clear() | |
100.times \ | |
{ | |
|w| | |
test(p, k, i, w / 100.0) | |
} | |
puts | |
$x += 25 | |
} | |
end | |
p = {'w' => [0, 80, false], | |
'p' => [0, 350, false], | |
'c' => [0, 550, false], | |
'm' => [0, 300, false], | |
'cg' => [0, 140, false], | |
'cm' => [0, 50, false], | |
'r' => [0.0, 1.0, true], | |
'z' => [0, 1000, true], | |
} | |
# test(p, 'w', 0, 0.1); exit | |
all(p) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment