Created
January 4, 2016 04:35
-
-
Save vznvzn/4ae062f19d52d05ee9fd 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
#!/usr/bin/ruby1.8 | |
def max_by_n(l) | |
i = -1 | |
return l.map { |x| i += 1; [x, i] }.max[1] | |
end | |
def f1(n) | |
n = (n * 3 + 1) / 2 while (n % 2 == 1) | |
n /= 2 while (n % 2 == 0) | |
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 = max_by_n(l) | |
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].sort_by { |x| x['r'] }[-1] | |
else | |
z = l.sort_by { |x| x[w] }[-1] | |
end | |
z.merge!({ 't' => t, 's' => t.to_f / e }) | |
return z if (z['s'] >= m) | |
} | |
end | |
def clear() | |
$c = [0] * 2 | |
$a = {} | |
$x = 0 if ($x.nil?) | |
end | |
def adv(n) | |
l = [n] | |
n1 = n | |
while (n != 1 && n >= n1) | |
n = f1(n) | |
l << n | |
end | |
return l.map { |x| x.to_s(2).length } | |
end | |
def diff(n1) | |
ns = n1.to_s(2) | |
return if (ns.length == 1) | |
n2 = ('1' + ns[2..-1]).to_i(2) | |
l1 = adv(n1) | |
l2 = adv(n2) | |
return if (l1.size <= 1 || l2.size <= 1) | |
m1 = max_by_n(l1) | |
m2 = max_by_n(l2) | |
l1 = l1[m1..-1] | |
l2 = l2[m2..-1] | |
return if (l1.size <= 1 || l2.size <= 1) | |
m = [l1.size - 1, l2.size - 1].max | |
mx = nil | |
(0..m).each \ | |
{ | |
|x| | |
r = x.to_f / m | |
x1 = (r * (l1.size - 1)).to_i | |
x2 = (r * (l2.size - 1)).to_i | |
y1 = l1[x1].to_f / l1[0] | |
y2 = l2[x2].to_f / l2[0] | |
d = (y1 - y2).abs | |
# puts([y1, y2].join("\t")) | |
mx = [mx, d].compact.max | |
} | |
return [mx] | |
end | |
def test(p, k, c, m) | |
z = long(m, k) | |
clear() if ($c.nil?) | |
$c[0] += 1 | |
n = z['n'] | |
return if ($a.member?(n)) | |
$c[1] += 1 | |
$a[n] = nil | |
$stderr.puts([$c, k, z['s'], n.to_s(2)].inspect) | |
l = diff(n) | |
puts(([$x, c] + l).join("\t")) if (!l.nil?) | |
$stdout.flush | |
$x += 1 | |
end | |
def r2(x) | |
sprintf("%.2f", x).to_f | |
end | |
def all(p) | |
c = 100 | |
p.each_with_index \ | |
{ | |
|k, i| | |
clear() | |
(c + 1).times \ | |
{ | |
|w| | |
test(p, k, i, w / c.to_f) | |
} | |
puts | |
$x += 25 | |
} | |
end | |
p = ['w', 'p', 'c', 'm', 'cg', 'cm', 'r', 'z'] | |
# test(p, 'w', 50, 0.5); exit | |
all(p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment