Which rationals can be written as the sum of two rational squares?

Which rational numbers can be written as the sum of two rational squares?

That is, for which rational numbers $a$, are there rational numbers $x$ and $y$ such that $a = x^2 + y^2$.

It is a famous theorem that if an integer can be written as the sum of two rational squares then it can be written as the sum of two integral squares, and then the solution is the famous one by Fermat, but I didn't find anything about the general case.


Solution 1:

Let $r=\frac{p}{q}$, where $p$ and $q$ are integers. We will show that $r$ can be written as the sum of two rational squares if and only if $pq$ can be written as the sum of the squares of two integers.

Equivalently, if $p\ne 0$, then $\frac{p}{q}$ is a sum of the squares of two rationals if and only if every prime divisor of $pq$ of the form $4k+3$ occurs to an even power.

Proof: If $pq$ is the sum of the squares of two integers, it is clear that $r$ is the sum of the squares of two rationals.

For the other direction, suppose that $r$ can be written as the sum of the squares of two rationals. Without loss of generality we may assume that the rationals are $\frac{a}{c}$ and $\frac{b}{c}$ for some integers $a,b,c$. Then $$\frac{a^2+b^2}{c^2}=\frac{pq}{q^2}.$$ So $c^2pq$ is a sum of two squares. It follows that every prime of the form $4k+3$ occurs to an even degree in the prime power factorization of $c^2pq$, and hence of $pq$. It follows that $pq$ is a sum of two squares.

Solution 2:

This can be translated in a question about Hilbert symbols (or quaternion algebras). In fact you can check easily that a rational $a$ can be written as a sum of two squares if and only if the Hilbert symbol $(a,-1)_{\mathbb Q}$ is trivial. Now write $a=p/q$ and notice that $(p/q,-1)=(pq,-1)$. You can forget about all squares in the factorization of $pq$, so you can assume that $p$ and $q$ are both squarefree. Now look at the prime factorization of $p$ and $q$ and use the fact that $(x,-1)(y,-1)=(xy,-1)$ for all $x,y\in\mathbb Q$, toghether with the fact that for a prime $l$ you have that $(l,-1)$ is trivial iff $l\equiv 1,2\bmod 4$ while if $l\equiv 3\bmod 4$ then $(l,-1)$ ramifies at $2$ and $l$ to get your answer: $a$ can be written as a sum of two squares iff it is non-negative and is of the form $b^2\frac{p}{q}$ where $b\in\mathbb Q$ and $p,q$ are coprime integers divided only by primes $\equiv 1,2\bmod 4$.