Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Last active February 18, 2017 20:15
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/e8189aa09a1437b00a75a280412ba4d8 to your computer and use it in GitHub Desktop.
Save vznvzn/e8189aa09a1437b00a75a280412ba4d8 to your computer and use it in GitHub Desktop.
def f2(n)
n = (n * 3 + 1) / 2 while (n.odd?)
n /= 2 while (n.even?)
return n
end
def adv(x)
n1 = n = x['n']
l = [n]
while (n >= n1 && n != 1)
n = f2(n)
l << n
end
x['l'] = l
x['ls'] = l.size
x['ns'] = x['n'].to_s(2).length
x['h'] = x['ls'].to_f / x['ns']
x['h2'] = (x['h'] * 50).to_i
return x
end
def next2(z)
l = [z]
p = z['p'] + 1
l << adv({'n'=>z['n'] + 2**p, 'p'=>p})
l << z.merge({'p'=>p})
return l
end
def stat(l)
l = [0] 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 a, sd, l.max.to_f, l.min.to_f
end
def opt(c)
l1 = []
l2 = []
l1 << next2({'n'=>1, 'p'=>0})
puts('# ' + Time.now.to_s)
t = Time.now.to_i
c.times \
{
|i|
$stderr.puts([i, sprintf('%.1fm', (Time.now.to_i - t) / 60.0), Time.now.to_s].join("\t")) if (i % 100 == 0)
l1 = l1.sort_by { |x| [x[1][ARGV[0]]] }.reverse
z = l1.delete_at(rand([l1.size, 20].min))
l1.pop if (l1.size > 1000)
l2 << z
l1 << next2(z[1])
l1 << next2(z[2])
}
return l2.map { |x| x[1].reject { |k, v| k == 'l' } }
end
def out(fn, l)
f = File.open("#{fn}.txt", 'w')
f.puts(l[0].keys.join("\t"))
l.each { |x| f.puts(x.values.join("\t")) }
f.close
end
l = opt(5000)
out('out', l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment