Originally Posted by

**Bruno J.** Alright, so nobody has answered this problem so I'll give my answer.

For $\displaystyle p=8n+1$, both $\displaystyle -1,\ 2$ are quadratic residues; therefore $\displaystyle -2$ is a quadratic residue.

For $\displaystyle p=8n+3$, neither $\displaystyle -1,\ 2$ are quadratic residues; therefore $\displaystyle -2$ is a quadratic residue.

Therefore we can find $\displaystyle n,m \in \mathbb N$ such that $\displaystyle n^2=mp-2$, i.e. $\displaystyle mp=n^2+2=(n+\sqrt{-2})(n-\sqrt{-2})$. Since $\displaystyle \mathbb{Z}[\sqrt{-2}]$ is a unique factorization domain, and since neither factor on the left is divisible by $\displaystyle p$, we deduce that $\displaystyle p$ is not prime in $\displaystyle \mathbb{Z}[\sqrt{-2}]$, and admits a factorization $\displaystyle p=(a+b\sqrt{-2})(c+d\sqrt{-2})$. Taking norms we get $\displaystyle p^2=(a^2+2b^2)(c^2+2d^2)$, so $\displaystyle p=a^2+2b^2=c^2+2d^2$.