Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created August 3, 2016 04: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/6f3832a0650b62693f95de9425308d0b to your computer and use it in GitHub Desktop.
Save vznvzn/6f3832a0650b62693f95de9425308d0b 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
begin
l << (n = f1(n))
end while (n >= n1 && n != 1)
return l
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = s.empty? ? 0 : c.to_f / s.length
return (0.5 - d).abs, d
end
def r(x)
return sprintf("%.3f", x).to_f
end
def mx(n)
l = seq2(n)
d, = d(n.to_s)
ls = l.size
return {'by' => [-ls, d]}
end
def hard(c)
l = [{'n' => 1, 'p' =>0}.merge(mx(1))]
loop \
{
l.sort_by! { |x| x['by'] }
return l[0].reject { |x| x == 'by' } if (l.size >= c)
x = l.delete_at(rand([l.size, 20].min))
p = x['p'] + 1
l.push(x.merge('p' => p))
n = x['n'] | (1 << p)
l.push({'n' => n, 'p' => p}.merge(mx(n)))
}
end
def data(n, f)
l = seq2(n)
l.each_with_index \
{
|x, i|
ns = x.to_s(2)
nl = ns.length
m = nl / 2
nsh = ns[0...m]
nsl = ns[m..-1]
f.puts([i.to_f / l.size, d(ns), d(nsh), d(nsl)].join("\t"))
}
return l.size
end
f = File.open('db3.txt', 'w')
c = 0
(500...540).each \
{
|i|
z = hard(i)
c += data(z['n'], f)
p([i, c])
}
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment