Prove that every prime $p$ of which $-1$ is a quadratic residue can be represented in the form $x^2+y^2$. [duplicate]

Prove that every prime $p$ of which $-1$ is a quadratic residue can be represented in the form $x^2+y^2$.

In a similar question the result is achieved by noting that every prime of the form $p=4k+1$ can be written as the sum of two squares and "it follows that -1 is a quadratic residue $\pmod p$ ". So, in my question, does it just suffice to show that $p$ is the sum of two squares if $p \equiv 1 \pmod 4$? How should we proceed?

Suppose $n^2+1 = mp$. In the plane, mark all points of the form $(a,an)\pmod p$ It forms a regular grid of points. This grid repeats with offset $(p,0)$ and $(0,p)$, and there are $p$ points in any $p\times p$ square.
The distance between any two of these points is $d=\sqrt{(a-b)^2+(an-bn-cp)^2}$. The expression under the square-root is $(n^2+1)(a-b)^2 - 2cp(an-bn) + c^2p^2$ which is a multiple of $p$. So the possible distances are $\sqrt{p},\sqrt{2p},\sqrt{3p},...$
If the smallest of these distances is $\sqrt{p}$, then $(a-b)^2 + (an-bn-cp)^2=p$, and $p$ is the sum of two squares.
If not, then the distance between any two points is at least $\sqrt{2p}$. So there is a circle of radius $\sqrt{p/2}$ around each point, and they don't intersect. The total area of $p$ of these circles is $p\pi(p/2)>p^2$. But $p$ of the circles fit in a $p\times p$ square. This is a contradiction, so the closest points are $\sqrt{p}$ apart, and $p$ is the sum of two squares.

• I.m going to study this when I'm not so tired. Looks very interesting. A geometric approach,it seems. – DanielWainfleet Apr 13 '18 at 9:57

Notation: All variable-symbols for numbers, except $A$ and $B$, which occur near the end, are positive integers.

By induction. We have $2=1^2+1^2.$ The inductive hypothesis is that prime $p\equiv 1\mod 4$ and that every prime $q<p$ such that $q\not\equiv 3\mod 4$ is a sum of two squares. We show this implies $p$ is a sum of two squares.

Let $S =\{n:\exists x,y\;(x^2+y^2=np\}.$ Let $N=\min S.$ Of course we want to show that $N=1$. Let $a^2+b^2=Np .$

(1). We know $N<p$ because there exists $x$ with $x\leq (p-1)/2$ such that $(x^2+1)/p\in S,$ and we have $(x^2+1)/p<((p/2)^2+1)/p<p.$

(2). We have $a<p$ and $b<p$ (otherwise $N=(a^2+b^2)/p\geq p$).

(3). We have $\gcd(a,b)=1.$ Proof: Suppose $a=a_1z$ and $b=b_1z.$ Now $\gcd (z^2,p)=1$ (because $z\leq a<p,$ and $p$ is prime) and $z^2|(Np),$ so $z^2|N.$ Therefore $$(a_1^2+b_1^2)/p= (a^2+b^2)/(z^2p)=(Np)/ (z^2p)=N/z^2\in \Bbb N .$$ $\quad$ So $\min S=N\geq N/z^2\in S,$ so $z=1.$

(4). We need the result that if $q$ is prime and $q\equiv 3\mod 4$ then $q\not|\;(x^2+y^2)$ unless $q|x$ and $q|y.$ Any prime divisor of $N$ is a divisor of $a^2+b^2,$ and $\gcd(a,b)=1.$ Therefore no prime divisor of $N$ is $\equiv 3$ modulo $4.$

(5). Finally we show by contradiction that $N=1.$ Suppose instead that a prime $q$ divides $N.$ The inductive hypothesis and (4) and (1) imply that $q=u^2+v^2$ for some $u,v.$ Let $v\equiv ju \mod q$ where $j^2+1\equiv 0\mod q.$ Since $q|(a^2+b^2)$ and $\gcd(a,b)=1$ we have $b\equiv \pm ja\mod q .$

Let $N=N'q.$ We have $$N'q^2p=Nqp=q(a^2+b^2)=$$ $$=(u^2+v^2)(a^2+b^2)=$$ $$(i)...\quad =(ua+vb)^2+(ub-va)^2=$$ $$(ii)....\quad =(ua-vb)^2+(ub+va)^2.$$ Since $v\equiv ju \mod q$ and $b\equiv \pm ja\mod q,$ one of expressions in (i),(ii) above is equal to $(qA)^2+(qB)^2$ for some non-negative integers $A,B$.

So $N'q^2p=(Aq)^2+(Bq)^2.$ So $N'p=A^2+B^2.$ Now $0<N'<p$ and $p$ is prime so $N'p$ is not a square, so $A\ne 0\ne B.$ So $N'\in S.$ But $\min S=N>N'\in S,$ a contradiction.

We conclude that $N=1$, so $a^2+b^2=Np=p.$