Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Last active March 2, 2016 05: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/b836e5c99ec275b4d3ed to your computer and use it in GitHub Desktop.
Save vznvzn/b836e5c99ec275b4d3ed to your computer and use it in GitHub Desktop.
def dense(n)
s = n.to_s(2)
c = 0
s.length.times \
{
|i|
c += s[i, 1].to_i
}
return c.to_f / s.length
end
def f1(n)
n = (n * 3 + 1) / 2 while (n.odd?)
n /= 2 while (n.even?)
return n
end
def adv(n)
l = [n]
n1 = n
while (n != 1)
n = f1(n)
break if (n < n1)
l << n
end
return l
end
def stat(l)
return 0, 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)
raise if (sd.nan?)
return a, sd
end
def stat2(l1, l2)
x1 = stat(l1)
x2 = stat(l2)
return x1[0] - x2[0], x1[1] - x2[1] #, x1[0] / x1[1] - x2[0] / x2[1]
end
def scan(c)
z = []
(c..(c + 99)).each { |i| z[i - c] = eval($l[i]) }
z.sort_by! { |x| x['cg'] }
z.each \
{
|x|
n = x['n']
l = adv(n)
m = (0...l.size).max_by { |x| l[x] }
l.map! { |x| dense(x) - 0.5 }
x1 = stat2(l[0..m], l[m..-1])
x2 = stat2(l[0..m].select { |x| x > 0 }, l[m..-1].select { |x| x > 0 })
x3 = stat2(l[0..m].select { |x| x < 0 }, l[m..-1].select { |x| x < 0 })
puts((x1 + x2 + x3).join("\t"))
}
end
$f = File.open('db.txt')
$l = $f.readlines
scan(0)
scan(400)
scan(500)
scan(600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment