Situation:
What I would really love:
Note:
Closing comments:
The problem is that I know how to solve these equations by hand/with a calculator, but I'm not sure how to solve them in code.
As it seems you are trying to implement the Sieve of Atkin then you are also probably aware that 4x^2+y^2=n is only the first of three equations. I don't want to spoil your fun and thus the below only implements that one. If you get stuck, just comment 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
I suppose you are implementing the Sieve of Atkin. In that case, you don't actually solve the equation. Look at the original paper for the actual algorithm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With