Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created August 22, 2015 18:09
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/4f98bceef9501f2aa8d2 to your computer and use it in GitHub Desktop.
Save vznvzn/4f98bceef9501f2aa8d2 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby1.8
def f(n)
n1 = n
c = 0
m = [0, 0]
while (n != 1 && n >= n1)
n = (n * 3 + 1) / 2 while (n % 2 == 1)
n /= 2 while (n % 2 == 0)
m = [m, [n, c]].max
c += 1
end
return {'m' => m[0],
'cm' => m[1],
'c' => c}
end
def long(r)
l = [{'n' => 1, 'p' => 0}.merge(f(1))]
w = 'cm'
j = 0
loop \
{
l = l.sort_by { |x| x[w] }.reverse
x = l.delete_at(rand([l.size, 10].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))
if (r.member?(x2['cm'])) then
x2['j'] = j
x2['s'] = l.size
return x2
else
l << x2
end
return if ((j += 1) >= 2000 || l.empty?)
}
end
def long2(r)
10.times \
{
|i|
x = long(r)
if (!x.nil?) then
x['i'] = i
return x
end
}
raise
end
def run(r, l0, l1, l2, t)
x = long2(r)
x.delete('m')
n = x.delete('n')
l = []
i = 0
while (n != 1)
c1 = c0 = 0
while (n % 2 == 1)
n = (n * 3 + 1) / 2
c1 += 1
end
while (n % 2 == 0)
n /= 2
c0 += 1
end
ns = n.to_s(2)
l0[i] = l1[i] = l2[i] = t[i] = 0 if (t[i].nil?)
l0[i] += c0
l1[i] += c1
l2[i] += n.to_s(2).length
t[i] += 1
i += 1
end
$i = 0 if ($i.nil?)
x['c2'] = i
$stderr.puts([$i += 1, x].inspect)
end
l0, l1, l2, t = [], [], [], []
ARGV[0].to_i.times { run(75..80, l0, l1, l2, t) }
t.size.times { |i| puts(([l0, l1, l2].map { |l| l[i].to_f / t[i] } + [t[i]]).join("\t")) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment