Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created August 26, 2016 01: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/b1e71fb9f9b814650fc067f8b0ae434e to your computer and use it in GitHub Desktop.
Save vznvzn/b1e71fb9f9b814650fc067f8b0ae434e to your computer and use it in GitHub Desktop.
def f1(n)
n = (n * 3 + 1) / 2 while (n % 2 == 1)
n /= 2 while (n % 2 == 0)
return n
end
def seq2(n)
l = [n]
n1 = n
l << (n = f1(n)) while (n >= n1 && n != 1)
return l
end
def mx(n)
return {'ls' => seq2(n).size}
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def stat(l)
return if (l.empty?)
t = t2 = 0
l.each \
{
|x|
t += x
t2 += x ** 2
}
c = l.size
a = t.to_f / c
z = t2.to_f / c - a ** 2
sd = Math.sqrt(z < 0 ? 0 : z)
return l.size, a, sd, l.max, l.min
end
l = [{'n' => 1, 'p' =>0}.merge(mx(1))]
a = {}
t1 = Time.now.to_i
loop \
{
l = l.sort_by { |x| -x['ls'] }
x = l.delete_at(rand([l.size, 50].min))
p = x['p'] + 1
n = x['n']
x1 = x.dup
x1['p'] = p
l.push(x1)
x2 = {}
n |= (1 << p)
x2['n'] = n
x2['p'] = p
x2.merge!(mx(n))
if (x2['ls'] > x['ls']) then
l.push(x2)
# j = p
j = x2['ls']
a[p] = a.fetch(p, []) + [x2]
t = sprintf("%.1fm\n", (Time.now.to_i - t1).to_f / 60).to_f
$stderr.puts([l.size, t].inspect) if (l.size % 100 == 0)
end
break if (l.size == 2000)
}
a.keys.sort.each \
{
|i|
l = a[i].map { |x| d(x['n'].to_s(2)) }
puts(([i] + stat(l)).join("\t"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment