 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: 32 + 42 = 52. 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 a2 + b2 = c2. 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*L1 = (5, 12, 13). You can multiply on the right again to get another new triple, such as v*L1*L3 = (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:

• Apply L1, L2, and L3 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 ith generation will contain 3i 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 a3 + b3 = c3? Don't bother: Fermat's Last Theorem states that this equation has no integer solutions!

Share Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of PROC IML and SAS/IML Studio. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.

1. Absolutely beautiful visual. Reading about pythagrean triples suddenly brought me back to my math team days in high school ! We did pass a lot of time playing with these and learning a bit of how they were generalized into something called number theory.

In fact, something for geometry teachers to consider. At the very least, the graphics alone would capture more than a few imaginations - and some kids may ask why ?

2. Terence Coates on

I wonder if I have found something new which is, that the number of primitive triples from 3,4,5 up to Hypotenuse N, is close to 16% of N. +/-3. (Up to N = 2000 in my trials)
N falls below this when N has only one prime factor of form 4n+1, and rises when it has more than one (different) prime factor, since the number of triples with a particular Hypotenuse = 2^(p-1) where p = the number of different prime factors of form 4n+1

3. If you project (in the limit) these (possibly including the non-primitves) onto a line [a,b]/(a^2+b^2) are they dense on some or all of that line? By dense I mean that for any chosen interval on the dense part, there is at least one projection there. E.g., the rational numbers are dense on R.