Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created October 2, 2015 21:44
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/da4bd12ce93b2fbd443c to your computer and use it in GitHub Desktop.
Save vznvzn/da4bd12ce93b2fbd443c 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 f1(n)
n = (n * 3 + 1) / 2 if (n.odd?)
n /= 2 while (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}
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'] if (z['c'] >= m)
}
end
def adj1(n, m)
return n + (m * 2)
end
def adj2(n, m)
n >>= m
return n != 0 ? n : 1
end
def mono(l)
y = nil;
c = m = a = 0
b = nil
l.each_with_index \
{
|x, i|
c += 1
if (c > m) then
m = c
b = a
end
next if (!(y.nil? || x <= y))
y = x
c = 0
a = i
}
return m, b
end
def test(n, t)
l = [n]
(t - 1).times { |i| l << adj1(n, i + 1) }
l2 = []
begin
c = 0
l.size.times { |i| l[i] = f1(l[i]); c += l[i].to_s(2).length }
l2 << c
end while (l.select { |x| x == 1 }.size < l.size)
$stderr.puts(mono(l2).inspect)
return l2
end
n = long(150)
l1 = test(n, 50)
l2 = test(n, 500)
[l1, l2].transpose.each { |x| puts(x.join("\t")) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment