Created
February 26, 2016 03:22
-
-
Save vznvzn/e875dd13973cf02fd7cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 dense2(n) | |
s = n.to_s(2) | |
n2 = s[(s.length / 2)..-1].to_i(2) | |
return dense(n2) | |
end | |
def stat2(l) | |
x1 = stat(l.map { |x| dense2(x) - 0.5 }) | |
x2 = stat(l.map { |x| dense(x) - 0.5 }) | |
return x1[0] - x2[0], x1[1] - 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] } | |
x1 = stat2(l[0..m]) | |
x2 = stat2(l[m..-1]) | |
puts((x1 + x2).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