Created
April 14, 2016 01:20
-
-
Save vznvzn/86272b4290a2b4d0ab0d081a723ad134 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 prime(n) | |
return true if (n == 2) | |
return false if (n.even?) | |
f = 3 | |
while (f < n / 2) | |
$f << f | |
return false if (n % f == 0) | |
f += 2 | |
end | |
return true | |
end | |
def seq1() | |
$f = [] | |
l = [] | |
t = 0 | |
n = 3 | |
while (l.size < $c) | |
if prime(n) then | |
l << $f.size | |
$f.clear | |
end | |
n += 2 | |
end | |
return l | |
end | |
def f(n) | |
c = 0 | |
while (n != 1) | |
n = (3 * n + 1) if (n.odd?) | |
# n = (3 * n + 1) / 2 while (n.odd?) | |
n = n / 2 if (n.even?) | |
# n /= 2 while (n.even?) | |
c += 1 | |
end | |
return c | |
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 seq2() | |
n = 3 | |
t = 0 | |
l = [] | |
l2 = [] | |
c = 0 | |
while (l2.size < $c) | |
l << f(n) | |
n += 2 | |
if (l.size == 100) then | |
a, sd = stat(l) | |
l2 << a | |
l.clear | |
c += 1 | |
end | |
end | |
return l2 | |
end | |
$c = 500 | |
l1 = seq1() | |
l2 = seq2() | |
[l1, l2].transpose.each \ | |
{ | |
|z| | |
puts(z.join("\t")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment