Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created September 2, 2016 00:55
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/e8cae73840163a35727a9f1692c1fda9 to your computer and use it in GitHub Desktop.
Save vznvzn/e8cae73840163a35727a9f1692c1fda9 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 d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def val(z)
n = z['n']
l = seq2(n)
cm = (0...l.size).max_by { |x| l[x] }
ns = n.to_s(2)
d = d(ns)
nl = ns.length
return z.merge({'cm' => cm, 'nl' => nl, 'd' => d, 'by' => nl - cm})
end
def hard(c)
l = [val({'n' => 1, 'p' =>0})]
c.times \
{
l.sort_by! { |x| [x['by'], x['d']] }
x = l.delete_at(rand([l.size, 100].min))
p = x['p'] + 1
l.push(x.merge('p' => p))
l.push(val({'n' => x['n'] | (1 << p), 'p' => p}))
$stderr.puts([l.size, x].inspect)
}
return l
end
l = hard(5000)
l[0...10].each { |x| $stderr.puts(x.inspect) }
$stderr.puts(l[0].keys.inspect)
l.sort_by! { |x| x['n'] }.each { |x| p(x.values) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment