Sum of squares function

In number theory, the sum of squares function is an arithmetic function that gives the number of representations for a given positive integer $n$ as the sum of $k$ squares, where representations that differ only in the order of the summands or in the signs of the numbers being squared are counted as different. It is denoted by $r_{k}(n)$.

Definition
The function is defined as


 * $$r_k(n) = |\{(a_1, a_2, \ldots, a_k) \in \mathbb{Z}^k \ : \ n = a_1^2 + a_2^2 + \cdots + a_k^2\}|$$

where $$|\,\ |$$ denotes the cardinality of a set. In other words, $r_{k}(n)$ is the number of ways $n$ can be written as a sum of $k$ squares.

For example, $$r_2(1) = 4$$ since $$1 = 0^2 + (\pm 1)^2 = (\pm 1)^2 + 0^2$$ where each sum has two sign combinations, and also $$r_2(2) = 4$$ since $$ 2 = (\pm 1)^2 + (\pm 1)^2$$ with four sign combinations. On the other hand, $$r_2(3) = 0$$ because there is no way to represent 3 as a sum of two squares.

k = 2
[[File:Sum_of_two_squares_theorem.svg|thumb|upright=1.25|Integers satisfying the sum of two squares theorem are squares of possible distances between integer lattice points; values up to 100 are shown, with

The number of ways to write a natural number as sum of two squares is given by $r_{2}(n)$. It is given explicitly by


 * $$r_2(n) = 4(d_1(n)-d_3(n))$$

where $d_{1}(n)$ is the number of divisors of $n$ which are congruent to 1 modulo 4 and $d_{3}(n)$ is the number of divisors of $n$ which are congruent to 3 modulo 4. Using sums, the expression can be written as:


 * $$r_2(n) = 4\sum_{d \mid n \atop d\,\equiv\,1,3 \pmod 4}(-1)^{(d-1)/2}$$

The prime factorization $$n = 2^g p_1^{f_1}p_2^{f_2}\cdots q_1^{h_1}q_2^{h_2}\cdots $$, where $$p_i$$ are the prime factors of the form $$p_i \equiv 1\pmod 4,$$ and $$q_i$$ are the prime factors of the form $$q_i \equiv 3\pmod 4$$ gives another formula
 * $$r_2(n) = 4 (f_1 +1)(f_2+1)\cdots $$, if all exponents $$h_1, h_2, \cdots$$ are even. If one or more $$h_i$$ are odd, then $$r_2(n) = 0$$.

k = 3
Gauss proved that for a squarefree number $n > 4$,
 * $$r_3(n) = \begin{cases}

24 h(-n), & \text{if } n\equiv 3\pmod{8}, \\ 0 & \text{if } n\equiv 7\pmod{8}, \\ 12 h(-4n) & \text{otherwise}, \end{cases}$$ where $h(m)$ denotes the class number of an integer $m$.

There exist extensions of Gauss' formula to arbitrary integer $n$.

k = 4
The number of ways to represent $n$ as the sum of four squares was due to Carl Gustav Jakob Jacobi and it is eight times the sum of all its divisors which are not divisible by 4, i.e.
 * $$r_4(n)=8\sum_{d\,\mid\,n,\ 4\,\nmid\,d}d.$$

Representing $n = 2^{k}m$, where m is an odd integer, one can express $$r_4(n)$$ in terms of the divisor function as follows:
 * $$r_4(n) = 8\sigma(2^{\min\{k,1\}}m).$$

k = 6
The number of ways to represent $n$ as the sum of six squares is given by
 * $$r_6(n) = 4\sum_{d\mid n} d^2\big( 4\left(\tfrac{-4}{n/d}\right) - \left(\tfrac{-4}{d}\right)\big),$$

where $$\left(\tfrac{\cdot}{\cdot}\right)$$ is the Kronecker symbol.

k = 8
Jacobi also found an explicit formula for the case $k = 8$:
 * $$r_8(n) = 16\sum_{d\,\mid\,n}(-1)^{n+d}d^3.$$

Generating function
The generating function of the sequence $$r_k(n)$$ for fixed $k$ can be expressed in terms of the Jacobi theta function:


 * $$\vartheta(0;q)^k = \vartheta_3^k(q) = \sum_{n=0}^{\infty}r_k(n)q^n,$$

where


 * $$\vartheta(0;q) = \sum_{n=-\infty}^{\infty}q^{n^2} = 1 + 2q + 2q^4 + 2q^9 + 2q^{16} + \cdots.$$

Numerical values
The first 30 values for $$r_k(n), \; k=1, \dots, 8$$ are listed in the table below: