# Determining the number of ways a number can be written as sum of three squares

I was going through Erich Friedman's "What's Special About This Number?" and there some numbers are classified based on the number of ways we can write them as sum of squares. I want to prove the following claim by Friedman:

129 is the smallest number that can be written as the sum of 3 squares in 4 ways.

Indeed, as given in Wikipedia, $$11^2+2^2+2^2 = 10^2+5^2+2^2 = 8^2+8^2+1^2 = 8^2+7^2+4^2 = 129$$ So what remains to prove is that this is the smallest such number.

Is it possible to write a proof for this fact using some insights along with brute force/cases? How can we solve this problem using only brute-force?

Also, since I know the proof of Legendre's three-square theorem. I am also curious to know:

How can we determine the number of ways we can write a non-negative integer which satisfies Legendre's three-square theorem as sum of three squares?

Edit1: Related discussions on MathOverflow:

Edit2: Related discussions on ComputerScience.SE

Edit3: Related discussions on Mathematics.SE

• we can find some solutions, if not all, by solving some quadratic equations using triangular numbers as explained in the link below but I don't have the proof that we will find all of them: math.stackexchange.com/questions/2070691/… Dec 24, 2016 at 16:39
• @user25406 this approach appears to be promising. Dec 24, 2016 at 18:27
• There is the following formula for the number of representations of $n$ as a sum of two squares: $4 \sum_{m | n} \chi_4(n)$, where $\chi_4$ is the conductor of order 4. I wonder if this could be generalized to your question. Feb 20, 2017 at 7:28
• I read this in a book on Sieve theory, though I can't seem to find the book. The formula comes from the connection between Dirichlet characters (which are related to the Legendre/Jacobi symbol) and sums of squares. Feb 20, 2017 at 11:09
• Link to Erich Friedman's work is now broken. Updated link: erich-friedman.github.io/numbers.html Jan 13 at 12:22

Legendre gave the following answer, which he could not prove (Gauss gave the first proof of the 3-squares theorem). For the sake of simplicity I will only cover the case of numbers $$c \equiv 5 \bmod 8$$. In this case consider the equivalence classes of forms with discriminant $$-4c$$; like Legendre, we will consider equivalence with respect to the action of GL$$_2({\mathbb Z})$$. Let $$P$$ denote the form classes that represent primes $$p \equiv 1 \bmod 4$$, and $$Q$$ those that represent primes $$q \equiv 3 \bmod 4$$. Then the number of classes $$P$$ is equal to that of classes $$Q$$, and to the number of ways in which $$c$$ can be written as a sum of three squares.

If $$c = 29$$, the classes $$P$$ are $$x^2 + 29y^2$$ and $$5x^2 + 2xy + 6y^2$$, and these correspond to the two distinct ways of writing $$29$$ as a sum of three squares: $$29 = 0^2 + 2^2 + 5^2 = 2^2 + 3^2 + 4^2$$.

Legendre conjectured similar formulas for other values of $$c$$, which were later proved by Gauss, but using classes with respect to the action of SL$$_2({\mathbb Z})$$.

For the system of equations.

$$x_1^2+x_2^2+x_3^2=x_4^2+x_5^2+x_6^2=x_7^2+x_8^2+x_9^2=x_{10}^2+x_{11}^2+x_{12}^2$$

Solutions can be parameterized.

$$x_1=a(y^2+z^2+n^2)(p^2+s^2+n^2)(k^2+t^2+n^2)$$

$$x_2=b(y^2+z^2+n^2)(p^2+s^2+n^2)(k^2+t^2+n^2)$$

$$x_3=c(y^2+z^2+n^2)(p^2+s^2+n^2)(k^2+t^2+n^2)$$

$$x_4=(ay^2-2byn+az^2-2czn-an^2)(p^2+s^2+n^2)(k^2+t^2+n^2)$$

$$x_5=(bz^2-2cyz-by^2-2ayn+bn^2)(p^2+s^2+n^2)(k^2+t^2+n^2)$$

$$x_6=(cy^2-2byz-cz^2-2azn+cn^2)(p^2+s^2+n^2)(k^2+t^2+n^2)$$

$$x_7=(ap^2-2bpn+as^2-2csn-an^2)(y^2+z^2+n^2)(k^2+t^2+n^2)$$

$$x_8=(bs^2-2cps-bp^2-2apn+bn^2)(y^2+z^2+n^2)(k^2+t^2+n^2)$$

$$x_9=(cp^2-2bps-cs^2-2asn+cn^2)(y^2+z^2+n^2)(k^2+t^2+n^2)$$

$$x_{10}=(ak^2-2bkn+at^2-2ctn-an^2)(y^2+z^2+n^2)(p^2+s^2+n^2)$$

$$x_{11}=(bt^2-2ckt-bk^2-2akn+bn^2)(y^2+z^2+n^2)(p^2+s^2+n^2)$$

$$x_{12}=(ck^2-2bkt-ct^2-2atn+cn^2)(y^2+z^2+n^2)(p^2+s^2+n^2)$$

It is interesting that such triples can be too much. The formula can be increased to any number. That is the same to write not only for 4 partitions, but for any number. The main thing that all the variables were not identical to each other.

• Can you please give at least an outline of how one should write proof for 129 being smallest such number using this parametrization? Dec 23, 2016 at 8:14
• @individ, can you please post your derivation or a link to it. I think it is important to show how you got this parametrization, especially to those among us (I am one of them) who don't have the knowledge to derive it for themselves. Dec 23, 2016 at 21:13

You can try something like:

check=new Array();
for (i=1;i<12;i++)
for (j=i;j<12;j++)
for (k=j;k<12;k++) {
n=i*i+j*j+k*k;
if (!check[n]) check[n]=0;
check[n]++;
}
for (a=0;a<check.length;a++)
if (check[a]>=4) console.log(a);


which proves $129$ is the smallest such number, followed by:

$$134=11^2+3^2+2^2=10^2+5^2+3^2=9^2+7^2+2^2=7^2+7^2+6^2$$

You can determine the number of representations by $3$ squares by subtracting squares from $N$, and using $\sum_\limits{x\le \sqrt{N}} r_2(N-x^2)$, where $r_2$ is the sum of (two) squares function.

• Did you read the Edit1 above stating that these counting functions also count the number is permutations and sign changes (which I don't want)? Feb 21, 2017 at 6:27
• somewhere there is a formula for sum of two squares where zeroes, sign and order aren't counted - for example 16 doesn't count, but I can't find it just now. $r_2$ is the best we've got at the moment I think for the number of such numbers.
– JMP
Feb 21, 2017 at 6:35