Created
January 29, 2016 21:07
-
-
Save vznvzn/fe465181ca65982a5393 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 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 ** (s = m * 1000)) | 1, 's' => s } 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 | |
if (w == 'r') then | |
z = l.sort_by { |x| x['n'] }.reverse[0...100].max_by { |x| x['r'] } | |
else | |
z = l.max_by { |x| x[w] } | |
end | |
z.merge!({ 't' => t, 's' => t.to_f / e }) | |
return z if (z['s'] >= m) | |
} | |
end | |
def adv1(n) | |
l = [n] | |
n1 = n | |
while (n != 1) | |
n = f1(n) | |
break if (n < n1) | |
l << n | |
end | |
return l | |
end | |
def out(l, c, x0) | |
return if (l.size <= 1) | |
m = l.max | |
return if (m == 0) | |
l.size.times \ | |
{ | |
|i| | |
puts([x0 + i.to_f / (l.size - 1), l[i].to_f / m, c].join("\t")) | |
} | |
puts | |
end | |
def track(n, x0) | |
l = adv1(n).map { |x| x.to_s(2).length } | |
x1 = l[0] | |
l.map! { |x| x - x1 } | |
m = (0...l.size).max_by { |x| l[x] } | |
l1 = l[0..m] | |
l2 = l[m..-1] | |
out(l1, 1, x0) | |
out(l2, 2, x0) | |
end | |
def test(k, m) | |
$x['c'][k], $x['x'], $x['a'][k] = [0, $x['x'] + 2, {}] if ($x['c'][k].nil?) | |
z = long(m, k) | |
$x['c'][k] += 1 | |
n = z['n'] | |
return if ($x['a'][k].member?(n)) | |
$x['a'][k][n] = nil | |
$stderr.puts([$x['c'][k], $x['a'][k].size, k, z['s'], n.to_s(2)].inspect) | |
track(n, $x['x']) | |
$stdout.flush | |
end | |
def r2(x) | |
sprintf("%.2f", x).to_f | |
end | |
p = ['w', 'p', 'c', 'm', 'cg', 'cm', 'r', 'z'] | |
$x = {'a' => {}, 'c' => {}, 'x' => 0} | |
p.each { |k| 50.times { test(k, 0.75) } } | |
$x['c'].keys.each { |k| $stderr.puts("#{k}\t#{$x['c'][k]}") } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment