Created
June 21, 2015 21:53
-
-
Save vznvzn/c1949128cdb6f9583204 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
#!/usr/bin/ruby1.8 | |
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) | |
return d3(a), d3(sd) | |
end | |
def d3(x) | |
sprintf("%.3f", x).to_f | |
end | |
def f(x) | |
x = (x % 2 == 0) ? x / 2 : x * 3 + 1 | |
end | |
Dir.glob('out*.txt').each { |fn| File.delete(fn) } | |
l = [] | |
l2 = [] | |
z = r = c = j = r2 = s = 0 | |
n = 3 | |
d = 10 | |
t = Time.now.to_i | |
f2 = File.open('out.txt', 'w') | |
w = ARGV[0].to_f | |
ls = 1000 | |
loop \ | |
{ | |
while (l.size < ls) | |
l << [n] | |
n += 2 | |
end | |
i = 0 | |
ls = l.size | |
c = 0 | |
while (i < l.size) | |
l[i] << f(l[i][-1]) | |
if (l[i][-1] == 1) then | |
c += 1 | |
l.delete_at(i) | |
next | |
end | |
i += 1 | |
end | |
j += 1 | |
s += c | |
r2 = w * r + (1 - w) * r2 | |
r = s.to_f / j | |
ls += ((r > r2) ? -1 : 1) | |
x = [n, l.size, c, r, Time.now.to_i - t] | |
puts(x.join("\t")) | |
l2 << x | |
l2.shift if (l2.size > 2000) | |
if (Time.now.to_i - t > d) then | |
f = File.open("out#{z % 10}.txt", 'w') | |
l2.each { |x| f.puts(x.join("\t")) } | |
f.close | |
z += 1 | |
t = Time.now.to_i | |
l3 = l2.map { |x| x[1] } | |
f2.puts(([r, l3.min, l3.max] + stat(l3) + [Time.now]).join("\t")) | |
f2.flush | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment