I could use two loops to check all combinations of two integers that are less than pprime, but this is very inefficient. Is there a better algorithm to solve this problem? Any ideas?
p
Where p mod 4 = 1.
p mod 4 = 1
Thank,
You can try using the Hermite-Serret algorithm .
You can also find a good list of algorithms on this math.se page: https://math.stackexchange.com/questions/5877/efficiently-finding-two-squares-which-sum-to-a-prime
, : https://math.stackexchange.com/questions/5877/efficiently-finding-two-squares-which-sum-to-a-prime/5883#5883
. :
? p, , , , .
, 4n + 1.
, . Mathematica:
P[p_] := Reduce[-p + x^2 + y^2 == 0, {x, y}, Integers]
Finding solutions for the first few primes p that are 1 or 2 (mod 4).
P /@ {2, 5, 13, 17, 29, 37, 41, 53, 61}