Skip to content

Instantly share code, notes, and snippets.

@vznvzn

vznvzn/prime2.rb

Created Apr 2, 2016
Embed
What would you like to do?
def prime(n)
return true if (n == 2)
return false if (n.even?)
f = 3
while (f < n / 2)
return false if (n % f == 0)
f += 2
$c += 1
end
return true
end
$c = 0
l = []
(2..2000).each { |n| prime(n); l << $c }
w = 40
l2 = []
(0...(l.size - w)).each \
{
|j|
t = 0
w.times { |i| t += l[j + i] }
l2 << t
}
l3 = []
c = 0
(1...l2.size).each \
{
|i|
c += 1 if (l[i] == l[i - 1])
l3 << [c, l2[i] - l2[i - 1]]
}
l3.size.times { |i| puts(([l[i], l2[i]] + l3[i]).join("\t")) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment