I was reading on proof wiki about the derivation of the general solution to the pythagorean triple diophantine equation: $$ x^2 + y^2 = z^2, $$ where $x,y,z > 0$ are integers. I came across the following general solution to the primitive function: \begin{align*} x &= 2mn\\ y &= (m^2 - n^2)\\ z &= (m^2 + n^2)\\ \end{align*} for coprime $m,n$.
I looked at the proof of it working (if you square $x$ and $y$ and add it it does indeed equal $z^2$)
My one qualm was, how the hell did they start with that? For example, is there a natural way by starting with the original problem that you end up with the expression above?
I noticed that when attempting to derive the general solution myself, from start to finish,
I would begin by noting I can find all pairs such that $z$ and $y$ differ by a constant $k$... but I cannot make that final leap to end up with the equation above so that for any given $k$ you can find a solution.
Thanks ahead of time!
There are a number of ways to find those expressions organically. One way is via complex numbers (and this method can be found in the first chapter of Marcus' Number Fields).
Suppose $x^2 + y^2 = z^2$, with $x,y,z\in\Bbb N$, and also suppose that $x$, $y$, and $z$ have no common factor. Looking mod $4$, we can tell that $z$ must be odd: the only squares mod $4$ are $0$ and $1$, so the only possible equations are $0^2 + 0^2\equiv 0^2\mod 4$ and $0^2 + 1^2 \equiv 1^2\mod 4$. But in the first case, everything is divisible by $4$, so we don't have a primitive triple.
Proceeding, we can factor the left hand side and get $(x + iy)(x - iy) = z^2$. So, we now have a problem concerning $\Bbb Z[i] = \{a + bi\mid a,b\in\Bbb Z\}$. One can show that $\Bbb Z[i]$ has unique factorization of elements into primes (for a precise definition of all this, look to any abstract algebra text), so suppose some prime $p$ divides $x + iy$. $p$ clearly divides $z^2$ an even number of times, so we want to show that $p$ does not divide $x - iy$. If it did, we would have $p\mid (x + yi) + (x - yi)$, or $p\mid 2x$. Since $p$ divides $z$ as well, and $x$ and $z$ are relatively prime, we know that we can find $n,m\in\Bbb{Z}$ such that $2xm + zn = 1$ (recall that $z$ is odd). However, this implies that $p$ divides $1$, but the only elements dividing $1$ in $\Bbb{Z}[i]$ are $\pm 1$, $\pm i$, none of which are primes - so we have a contradiction. Therefore, by unique factorization, we must have $x + iy = u\alpha^2$, where $u\in\{\pm1,\pm i\}$. Writing $\alpha = m + in$, we have $\alpha^2 = m^2 - n^2 + i2mn $, so $x = \pm \left(m^2 - n^2\right)$, $y = \pm 2mn$, and solving for $z$, we get $z = \pm\left(m^2 + n^2\right)$.
If $m$ and $n$ aren't coprime, we don't obtain a primitive triple, as each of $x$, $y$, and $z$ will have a factor in common.
There are numerous other ways of deriving these ways of representing $x$, $y$, and $z$ as well, some of which include parameterizing the unit circle or moving $y^2$ to the other side and writing $x^2 = (z + y)(z - y)$.