# Sum of two squares theorem

In number theory, the sum of two squares theorem relates the prime decomposition of any integer n > 1 to whether it can be written as a sum of two squares, such that n = a$^2$ + b$^2$ for some integers a, b.
   An integer greater than one can be written as a sum of two squares if and only if its prime decomposition contains no factor p$^k$, where prime $p\equiv 3{\pmod {4}}$ and k is odd.
In writing a number as a sum of two squares, it is allowed for one of the squares to be zero, or for both of them to be equal to each other, so all squares and all doubles of squares are included in the numbers that can be represented in this way. This theorem supplements Fermat's theorem on sums of two squares which says when a prime number can be written as a sum of two squares, in that it also covers the case for composite numbers.
A number may have multiple representations as a sum of two squares, counted by the sum of squares function; for instance, every Pythagorean triple $a^{2}+b^{2}=c^{2}$ gives a second representation for $c^{2}$ beyond the trivial representation $c^{2}+0^{2}$.

## Examples

The prime decomposition of the number 2450 is given by 2450 = 2 · 5$^2$ · 7$^2$. Of the primes occurring in this decomposition, 2, 5, and 7, only 7 is congruent to 3 modulo 4. Its exponent in the decomposition, 2, is even. Therefore, the theorem states that it is expressible as the sum of two squares. Indeed, 2450 = 7$^2$ + 49$^2$.
The prime decomposition of the number 3430 is 2 · 5 · 7$^3$. This time, the exponent of 7 in the decomposition is 3, an odd number. So 3430 cannot be written as the sum of two squares.

## Representable numbers

The numbers that can be represented as the sums of two squares form the integer sequence
    0, 1, 2, 4, 5, 8, 9, 10, 13, 16, 17, 18, 20, 25, 26, 29, 32, ...
They form the set of all norms of Gaussian integers; their square roots form the set of all lengths of line segments between pairs of points in the two-dimensional integer lattice.
The number of representable numbers in the range from 0 to any number $n$ is proportional to ${\frac {n}{\sqrt {\log n}}}$, with a limiting constant of proportionality given by the Landau–Ramanujan constant, approximately 0.764.
The product of any two representable numbers is another representable number. Its representation can be derived from representations of its two factors, using the Brahmagupta–Fibonacci identity.

## Jacobi's two-square theorem

Two-square theorem — Denote the number of divisors of $n$ as $d(n)$, and write $d_{a}(n)$ for the number of those divisors with $d\equiv a{\bmod {4}}$. Let $n=2^{f}p_{1}^{r_{1}}p_{2}^{r_{2}}\cdots q_{1}^{s_{1}}q_{2}^{s_{2}}\cdots$ where $p_{i}\equiv 1{\bmod {4}},\ q_{i}\equiv 3{\bmod {4}}$.
Let $r_{2}(n)$ be the number of ways $n$ can be represented as the sum of two squares.
Then, $r_{2}(n)=0$ if any of the exponents $s_{j}$ are odd. If all $s_{j}$ are even, then $r_{2}(n)=4d(p_{1}^{r_{1}}p_{2}^{r_{2}}\cdots )=4(d_{1}(n)-d_{3}(n))$
Proved by Gauss using quadratic forms and Jacobi using elliptic functions. An elementary proof is based on the unique factorization of the Gaussian integers. Hirschhorn gives a short proof derived from the Jacobi triple product.