Created
March 12, 2016 06:27
-
-
Save vznvzn/74d358e3e3ffeab021b5 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 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 test(k, m, x, j) | |
if (x.empty?) then | |
x['k'] = k | |
x['a'] = {} | |
x['c1'] = 0 | |
x['c2'] = 0 | |
end | |
z = long(m, k) | |
x['c1'] += 1 | |
n = z['n'] | |
return if (x['a'].member?(n)) | |
x['a'][n] = nil | |
if (!(l = track(z)).nil?) then | |
x['c2'] += 1 | |
puts(([j, j * 2 + m] + l).join("\t")) | |
end | |
$stderr.puts([x['c1'], x['a'].size, x['c2'], k, z['s'], n.to_s(2)].inspect) | |
$stdout.flush | |
end | |
def f1(n) | |
n = (n * 3 + 1) / 2 while (n.odd?) | |
n /= 2 while (n.even?) | |
return n | |
end | |
def adv(n) | |
n1 = n | |
c = 0 | |
while (n != 1) | |
n = f1(n) | |
break if (n < n1) | |
c += 1 | |
end | |
return c | |
end | |
def track(z) | |
n = z['n'] | |
x = n.to_s(2) | |
m = x.length - 1 | |
return if (m <= 2) | |
x1 = adv(('1' + x[2..m]).to_i(2)) | |
x2 = adv(('10' + x[2..m]).to_i(2)) | |
x3 = adv(('11' + x[2..m]).to_i(2)) | |
x4 = adv(x.to_i(2)) | |
x[1, 1] = (1 - x[1, 1].to_i).to_s | |
x5 = adv(x.to_i(2)) | |
return x1, x2, x3, x4, x5 | |
end | |
p = ['w', 'p', 'c', 'm', 'cg', 'cm', 'r', 'z'] | |
p2 = p.values_at(0, 4, 5, 6) | |
# test('w', 0.5, {}, 0); exit | |
c = 100 | |
p2.each_with_index \ | |
{ | |
|k, j| | |
z = {} | |
c.times { |i| test(k, i.to_f / c, z, j) } | |
puts | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment