Created
August 13, 2015 22:41
-
-
Save vznvzn/449a7927c96f04b9b614 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 power(x, p) | |
n = n1 = x ** p | |
w = n.to_s(2).length | |
c = 0 | |
while (n != 1 && n >= n1) | |
n = n * 3 + 1 if (n.odd?) | |
n /= 2 while (n.even?) | |
c += 1 | |
end | |
return w, c | |
end | |
def prime(p) | |
n = 3 | |
sq = Math.sqrt(p) | |
while (n <= sq) | |
return false if (p % n == 0) | |
n += 2 | |
end | |
return true | |
end | |
def out(a, b) | |
l1 = [] | |
50.times \ | |
{ | |
|i| | |
l2 = [] | |
[a, b].each_with_index \ | |
{ | |
|x, c| | |
l2 << power(x, i * 2 + 1) | |
} | |
puts l2.join("\t") | |
l1 << l2.flatten | |
} | |
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) | |
return a, sd | |
end | |
def nth(t) | |
l = [] | |
l2 = [] | |
n = 3 | |
while (l.size < t || l2.size < t) | |
if (n % 4 != 1) then | |
if (prime(n)) then | |
l << n | |
else | |
l2 << n if (l2.size < l.size) | |
end | |
end | |
n += 2 | |
end | |
return (0...t - 1).to_a.map { |x| [l[x], l2[x]] } | |
end | |
t = ARGV[0].to_i | |
l = nth(t) | |
l.each \ | |
{ | |
|x, y| | |
out(x, y) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment