It seems to you that you are trying to implement Atkin's Sieve , then you probably also know that 4x ^ 2 + y ^ 2 = n is only the first of three equations. I do not want to spoil your pleasure, and thus, only that one realizes it below. If you are stuck, just comment on this answer and I will get back to you.
max = 100 primes = Array.new(max + 1) { false } sqrt = Math.sqrt(max) 1.upto(sqrt) do |x| 1.upto(sqrt) do |y| n = 4 * x**2 + y**2 primes[n] ^= true if n <= max && (n % 12 == 1 || n % 12 == 5) end end
source share