Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Last active October 1, 2015 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vznvzn/afd9383e73a5e42f210a to your computer and use it in GitHub Desktop.
Save vznvzn/afd9383e73a5e42f210a to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby1.8
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 oddop(n)
case $o
when 0
return n * 3 + 1
when 1
return (n * 3 + 1) / 2
when 2
begin
n = (n * 3 + 1) / 2
end while (n.odd?)
return n
end
end
def evenop(n)
case $e
when 0
return n / 2
when 1
begin
n /= 2
end while (n.even?)
return n
end
end
def f1(n)
n = oddop(n) if (n.odd?)
n = evenop(n) if (n.even?)
return n
end
def f(n)
n1 = n
c = 0
l = []
while (n != 1 && n >= n1)
n = f1(n)
l << n
c += 1
end
return {'c' => c, 'd' => (0.5 - dense(n1)).abs, 'l' => l}
end
def f2(l)
n = l.last
while (n != 1)
n = f1(n)
l << n
end
end
def delta2(l)
l = (1...l.size).map { |i| l[i] - l[i - 1] }
y = 0
l = (1...l.size).map { |i| x = l[i] - l[i - 1]; y += x; [x, y] }
return l
end
def long(m)
l = [{'n' => 1, 'p' => 0}.merge(f(1))]
t = 0
loop \
{
l = l.sort_by { |x| [x['c'], -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))
l << x2
t += 1
return if (t >= 2000)
z = l.max_by { |x| x['c'] }
return z['n'], z['l'] if (z['c'] >= m)
}
end
def test(w)
$stderr.puts(w.inspect)
$o, $e = w
c1 = c2 = c3 = c4 = 0
m = 100
m.times \
{
|i|
$stderr.print("#{i}\t") if (i % 10 == 0)
begin
n, l = long(80)
c4 += 1 if (n.nil?)
end while (n.nil?)
f2(l)
l1 = delta2(l.map { |x| Math.log(x) })
l2 = delta2(l.map { |x| x.to_s(2).length })
l = [l1, l2].transpose
c1 += 1 if (!l.select { |x| x[0][1] > 0 }.empty?)
c2 += 1 if (!l.select { |x| x[1][1] > 0 }.empty?)
c3 += l.size
}
$stderr.puts
p([w, c1, c2, c3.to_f / m, c4])
end
3.times { |x| 2.times { |y| test([x, y]) } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment