Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created April 15, 2016 00:35
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/a0d7b5bbd631644c6022f7133e431489 to your computer and use it in GitHub Desktop.
Save vznvzn/a0d7b5bbd631644c6022f7133e431489 to your computer and use it in GitHub Desktop.
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(w)
$f = []
l = []
t = 0
n = 3
j = 0
while (l.size < $c)
if prime(n) then
j += 1
if (j % w == 0) then
l << $f.size
$f.clear
end
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(w)
n = 3
t = 0
l = []
l2 = []
c = 0
while (l2.size < $c)
l << f(n)
n += 2
if (l.size == w) then
a, sd = stat(l)
l2 << a
l.clear
c += 1
end
end
return l2
end
$c = 500
l = []
(1..7).each { |w| l << seq2(w * 500) }
i = 0
l.each_with_index { |l1, c| l1.each { |x| puts([i, x, c].join("\t")); i += 1 }; puts }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment