# Primality test using the sum of 2 squares representation of a prime $p^2= a^2+b^2$

We have seen that the sum of two squares (2 sq rep) of an integer $$N^2$$ can be used to factor $$N$$.

Here we use the same method to show that it can determine if an integer $$N$$ is a prime. For composite numbers represented by $$N^2= a^2+b^2$$, we have seen that $$a^2$$ and $$b^2$$ will have at least one common factor which is easily calculated using the $$\gcd(a^2,b^2)$$.

As a reminder, we will give the example of $$N=7*37=259$$. $$N^2=259^2=67081$$. $$N^2=84^2+245^2$$. Calculating the $$\gcd$$, we get $$\gcd(84^2,245^2)= 7^2$$. And $$\gcd(49,259)=7$$ showing that $$7$$ is a factor.

For primes $$p$$, we use the same method. First we calculate the 2sq rep of $$p^2=a^2 + b^2$$, then we calculate the $$\gcd(a^2,b^2)$$.

Example 1. $$N=13=2^2+3^2$$. We can't determine from the 2sq rep of $$N=13$$ if it is a prime. However, using $$N^2=13^2=5^2+12^2$$, we can check if $$a^2$$ and $$b^2$$ have a common factor. Calculating the $$\gcd$$, we get $$\gcd(5^2,12^2)=1$$. So we can conclude that $$N=13$$ is a prime.

This test cannot, of course, determine if a number $$N=4*k-1$$ is a prime but it can tell if an integer of the form $$N=4*k+1$$ is a prime.

It would be interesting to see how such a test does when dealing with large numbers and how the above method compare with other primality tests.

Any help with running time, proof and testing large numbers will be greatly appreciated.

• If we let $N=5*13 = 65$, we can have $$N^2 = 16^2 + 63^2$$ but $\gcd(16^2,63^2)=1$. The common factor argument is only always true if there are primes $p\equiv 3\pmod 4$ dividing $N$. So in general you'll need to find several $a,b$ combinations, not just one. – Yong Hao Ng Jan 17 at 17:20
• Yes you need to find all representations of $N^2$ if you want to factor N. $65$ has 3 more representations. $65^2=25^2+60^2=33^2+56^2=39^2+52^2$. Notice that that the 1st and last representation I provided will provide you with the common factors $5^2$ and $13^2$. And this is specifically mentioned in the post about factoring, not the above post. But remember, for primes, they admit only one representation because if they had two, we would be able to extract the factors using Euler formula. – user25406 Jan 17 at 19:03
• @user25406 The two-square-method will not beat primality-tests like the Miller-Rabin-Test. If we however know that $$N=a^2+b^2$$ is the ONLY representation and if we additionally have that $\gcd(a,b)=1$, then we know that $N$ is prime. In fact, we can use every "idoneal number" $d$ and look for $$N=a^2+db^2$$ For further details just google "idoneal numbers" – Peter Jan 17 at 20:51
• @Peter, when calculating the 2 sq reps, how do we know we got all of them? For the example given by Yong Hao Ng above, if we stopped at $N^2=16^2+63^2$, we would have declared it a prime since $gcd(16^2,63^2)=1$ – user25406 Jan 19 at 12:11
• I did not notice that we have the representation not for $N$, but for $N^2$. In this case, my comment is not very useful. – Peter Jan 20 at 12:26