When I studied high school geometry, I noticed that many homework problems involved right triangles whose side lengths were integers. The canonical example is the 3-4-5 right triangle, which has legs of length 3 and 4 and a hypotenuse of length 5. The triple (3, 4, 5) is called a
Pythagorean triple because it satisfies the Pythagorean theorem: 3^{2} + 4^{2} = 5^{2}.
Similarly, (5, 12, 13) and (7, 24, 25) are Pythagorean triples that sometimes appear in geometry textbooks.

In general, a triple of natural numbers (*a, b, c*) is a Pythagorean triple if *a*^{2} + *b*^{2} = *c*^{2}. Obviously if (*a, b, c*) is a Pythagorean triple, then so is (*ka, kb, kc*) for any natural number *k*. Geometrically, this transformation is not very interesting because it simply scales a right triangle into a similar triangle that has the same shape.
I did not learn any trick in high school that enabled me to start with a Pythagorean triple and generate a new, geometrically different, right triangle.

### Generating Pythagorean triples

Because my high school geometry book reused the same three or four triples over and over again, I assumed that it is difficult to generate Pythagorean triples. In fact, that is not the case: There are several formulas for generating Pythagorean triples, some of which were known to the ancient Greeks.

One interesting algorithm comes from the 20th-century and involves linear transformations via matrices.
Here's how it works. Write any Pythagorean triple, *v*, as a row vector. Then the linear transformation *v**L is a new Pythagorean triple, where L is any of the three following linear transformations:

For example, if you represent *v* = (3, 4, 5) as a row vector, then *v**L_{1} = (5, 12, 13). You can multiply on the right again to get another new triple, such as *v**L_{1}*L_{3} = (45, 28, 53).

In fact, more is true. A *primitive Pythagorean triple* is a triple such that the three numbers have no nontrivial divisors, that is, they are *relatively prime*.
It turns out that *all* primitive Pythagorean triples can be obtained by iteratively applying one of three linear transformations to the triple (3, 4, 5).
In other words, the triple (3, 4, 5) is the "parent" of all primitive Pythagorean triples!

That is a fabulous result. It means that you can write a program that uses matrix multiplication to produce arbitrarily many primitive Pythagorean triples, as follows:

- Start with
*v*= (3, 4, 5). - Apply L
_{1}, L_{2}, and L_{3}to create the first generation of children. - Apply the three transformations to each of the children to create a generation of nine grandchildren.
- Continue this process. The
*i*th generation will contain 3^{i}triples.

You can implement this algorithm in the SAS/IML matrix language as follows:

proc iml; start GenerateTriples(depth=3); L1 = { 1 2 2, -2 -1 -2, 2 2 3}; L2 = { 1 2 2, 2 1 2, 2 2 3}; L3 = {-1 -2 -2, 2 1 2, 2 2 3}; NumTriples = sum(3##(0:Depth)); /* total number of triples */ triples = j(NumTriples,3,.); /* allocate array for results */ triples[1,] = {3 4 5}; /* parent triple */ k = 1; n = 2; do i = 1 to Depth; /* for each generation */ do j = 1 to 3##(i-1); /* generate 3##i children */ x = triples[k,]; triples[n,] = x*L1; triples[n+1,] = x*L2; triples[n+2,] = x*L3; k = k + 1; n = n + 3; end; end; return( triples ); finish; p = GenerateTriples(); print p;

The parent and the first three generations account for 1 + 3 + 9 + 27 = 40 triples. The list contains some old familiar friends, as well as a few strangers that I haven't met before.

### The distribution of Pythagorean triples

What do the Pythagorean triples look like if you plot their location in a Cartesian coordinate system? It turns out that they make fascinating patterns!

Because the hypotenuse length is a function of the leg lengths, it suffices to plot the locations of *a* and *b* in the coordinate plane. However, it turns out that when the algorithm produces a particular triple such as (5, 12, 13), it does not produce the symmetric triple (12, 5, 13). To make the pattern more symmetric, we can to manually add
the triple (*b, a, c*) whenever the triple (*a, b, c*) appears.

The following SAS/IML statements construct many primitive Pythagorean triples and create a scatter plot of the ones for which *a* and *b* are both less than 4,500:

p = GenerateTriples(15); /* generate many triples */ m = p[ loc(p[,1]<=4500 & p[,2]<=4500), ]; /* exclude large ones */ mm = m // m[,{2 1 3}]; /* generate symmetric pairs */ ods graphics / width=600px height=600px; title "Primitive Pythagorean Triples"; title2 "Depth = 15, Symmetric"; call scatter(mm[,1], mm[,2]) procopt="aspect=1" option="markerattrs=(size=3 symbol=SquareFilled)";

The image is shown at the beginning of this article. You can see certain parabolic curves that have a high density of points. There is a similar plot in Wikipedia that includes nonprimitive triples. The nonprimitive triples "fill in" some of the apparent "gaps" in the pattern.

These interesting patterns hint at deep relationships in number theory between Diophantine equations and the geometry of the solutions. Are you tempted to generate a similar image for the generalized equation *a*^{3} + *b*^{3} = *c*^{3}? Don't bother: Fermat's Last Theorem states that this equation has no integer solutions!