DISCLAIMER: This series of articles is a work in progress, and was primarily created to revitalize my blog and give it a new aesthetic. My goal is to find interesting, informative, and interactive ways to convey mathematics with emphasis on abstract algebra and number theory.

I wish to explore a very simple question from number theory with a not-so-simple and fascinating answer.

Given any positive integer $n$, can we determine if it is a sum of two squares?

That is, do there exist positive integers $x,y$ such that $n=x^2+y^2$? If so, how many solutions are there?

This question naturally arises in geometry. The Pythagorean Theorem tells us that if $a$ and $b$ are the legs of a right triangle and $c$ its hypotenuse, then $a^2+b^2=c^2$. Euclid showed that we can parameterize all Pythagorean triples via:

$(a, b, c) = (m^2-n^2, 2mn, m^2+n^2) \quad \{m > n\geq 1\}$

Suppose we want to know if we can construct a right triangle with an arbitrary hypotenuse $c$. We can only do so if $c=m^2+n^2$ for some integers $m$ and $n$. Thus, the problem of constructing a right triangle with integral sides and a given hypotenuse $c$ is reduced to writing $c$ as a sum of squares.

As a quick aside, you can find a short interactive proof of Euclid’s formula below. It is optional, but makes use of an important technique for solving Diophantine equationsPolynomial equations with integer coefficients for which we care only about integer solutions.
$2x^2+3xy-y^2=5$ where $x,y\in \mathbb{Z}$, for instance.
. Whenever you see an accordion element like this throughout the text, it indicates a proof. You are free to read as many or as few of the proofs as you want.

We wish to find all possible solutions to $a^2+b^2=c^2$. Since $c^2$ is a positive integer, we can divide both sides by $c^2$ and substitute $x=a/c$ and $y=b/c$. Note that $x$ and $y$ are rational numbers, not necessarily integers.

We now arrive at the equation $x^2+y^2=1$, whose solutions are rational points on a unit circle centered at the origin. We immediately know one rational point that lies on this circle: $(1, 0)$. Label this point as $\textcolor{red}{A}$. We can then draw a line $\textcolor{purple}{l}$ with rational slope $\beta=m/n$ (where $m,n\in \mathbb{N}$) extending out from $\textcolor{red}{A}$. The equation of this line is given by.

$y=\beta(1-x)$

So long as $\beta$ is positive, it follows that $\textcolor{purple}{l}$ must intersect at a point $\textcolor{blue}{B}=(x, y)$ on the circle. Importantly, since $\textcolor{red}{A}$ is a rational point and $\beta$ is a rational number, it follows that $\textcolor{blue}{B}$ must also be a rational point!

Below you may interact with an interactive Desmos graph demonstrating this.

Now let's explicitly compute the coordinates of $\textcolor{blue}{B}$. Line $\textcolor{purple}{l}$ intersects with the circle, so we may substitute $y=\beta(1-x)$ into $x^2+y^2=1$. \begin{align*} x^2+y^2&=1\\ x^2+(\beta(1-x))^2&=1\\ x^2 + \beta^2 - 2\beta^2 x + \beta^2x^2 &= 1\\ (\beta^2+1)x^2 - 2\beta^2 x + (\beta^2 - 1) &= 0\\ (\beta^2+1)\left(x^2 - \dfrac{2\beta^2}{\beta^2+1} x + \dfrac{\beta^2 - 1}{\beta^2+1}\right) &= 0\\ \end{align*} We know that one of the roots of this quadratic must be $x=1$, since it is the $x$-coordinate of $\textcolor{red}{A}$, and $\textcolor{red}{A}$ lies on both the line $\textcolor{purple}{l}$ and the unit circle. Vieta's formulas tell us that the product of the roots equals $\dfrac{\beta^2 - 1}{\beta^2+1}$, hence the other root must be $x=\dfrac{\beta^2-1}{\beta^2+1} = \dfrac{m^2-n^2}{m^2+n^2}$.

We can then substitute $x$ into the equation for $\textcolor{purple}{l}$ to obtain the $y$-component of $\textcolor{blue}{B}$. \begin{align*} y&=\beta(1-x)\\ &=\dfrac{m}{n}\left(1-\dfrac{m^2-n^2}{m^2+n^2}\right)\\ &=\dfrac{2mn}{m^2+n^2} \end{align*}

Finally, recall that $x=a/c$ and $y=b/c$. Since $m^2+n^2$ is a common denominator to both $x$ and $y$, $c=m^2+n^2$.

Thus, $(a, b, c) = (m^2-n^2,2mn,m^2+n^2)$. One can confirm that this formula does indeed yield valid Pythagorean triples. $\Box$

The first known solution to the sum of squares problem dates as far back as the 17th century due to an observation by French mathematician Albert Girard, then later a theorem by Pierre de Fermat (of eponymous Last TheoremFor an integer $n>2$, there are no integer solutions $(a, b, c)$ to $a^n+b^n=c^n$. fame). It is this theorem we will try to prove first.

TheoremFermat's Theorem on the Sum of Two Squares
  • If $p=4k+1$ is a prime integer for some integer $k$, then we can always write $p$ as a sum of squares.
  • If $p=4k+3$, then it is impossible for $p$ to be written as a sum of squares.

As an example, $5=2^2+1^2, 13=2^2+3^2,$ and $89=8^2+5^2$, but neither $3$ nor $7$ can be written as a sum of two squares. Note that $2$, which does not satisfy either constraint, may be written as $2=1^2+1^2$.

We will first develop the tools needed to prove this conjecture, and then we will see how we may apply these tools to answer other similar questions, such as:

  • Can we write $n$ as the sum of three squares? What about four squares?
  • For which $n$ can we write $n=x^2+2y^2$ or $n=x^2+3y^2$. More generally, for which $n$ can we write $n=x^2+my^2$ for any integer $m$?
  • Can $n$ be written as the sum of two triangular numbers? Three triangular numbers? What about pentagonal numbers or figurate numbers in general?

I want to explore these questions and provide intuition for the mathematics underpinning their proofs. While it may not appear to be a “useful” question, solving these types of Diophantine equations can teach us powerful insights about numbers which paved the development of Ring and Field Theory — all from a simple arithmetic question.

Below you will find a table of contents for this text, as it is decently long.

  1. $n=x^2+y^2$ and Fermat's Theorem
  2. $n=x^2+my^2$ and Generalizations of Fermat's Theorem (WIP)
  3. Sums of Polygonal Numbers (WIP)
  4. References

AI Disclosure:

No Large Language Models were used to write the text content of these pages. However, Anthropic’s Claude was used to assist with writing some of the code for the interactive HTML/JS elements.