How to Generate Pythagorean Triplets

In sixth grade, I spent a lot of time exploring number theory. I'm impressed, in hindsight, at how far I got without the benefit of any algebra. My crowning achievement of the year was a method for generating Pythagorean triplets—that is, sets of three integers such that the sum of the squares of the first two is the square of the third; a triangle with sides these lengths will be, per the Pythagorean theorem, a right triangle.

My original formulas were a mixture of verbal description and geometric drawings. HTML may not be the best format for algebra, but it's even worse at drawing, so in this recreation of the method, I'll use algebra in the place of diagrams. Besides, it's more powerful. Specifically, at the time, I suspected the method produced all possible triplets, but couldn't prove it. Using algebra, I can show that it does doesn't. In addition, I can show which results are unique triplets—that is to say, those that are relatively prime (such as {3,4,5}) and not multiples of smaller triplets (such as {6,8,10}).

The method is built around the observation that the series of squares of integers is successive sums of the odd integers. Specifically, the square of n is the sum of all odd numbers less than 2n: 52 = 25 = 1 + 3 + 5 + 7 + 9. To get the next square, add the next odd number 2n+1: 36 = 25 + 11. I noticed this fact using graph paper: draw a square; to get the square with the next largest size, add a row of unit squares to two adjacent sides, then one more to get the corner.

If that odd number is itself a square, you've got a Pythagorean triplet: {sqrt(2n+1), n, n+1}.

The Method, part 1:

March through successive odd squares, and derive the associated triplet of each.

This means march through the odd numbers, as the squares of these are the odd squares. (Remember, odd times odd = odd, even times either even or odd = even.) The relevant formula is, for each odd number m, n = (m2 - 1) / 2. Since m is odd, write m = 2k+1, and so

n = ((2k+1)2 - 1)/2 = (4k2 + 4k + 1 - 1)/2 = 2k2 + 2k = 2k(k+1).

Thus, for the set of integers k, the triplets {2k+1, 2k(k+1), 2k(k+1)+1} will include all triplets with a difference of 1 between the second and third numbers. Thus:

For k = 1, m = 3 and n = (9-1)/2 = 4, so {3,4,5}. Familiar. Good.

k = 2 gives m = 5 and n = (25-1)/2 = 12, so {5,12,13}. The next most common. We're on a roll.

k = 3 gives m = 7 and n = (49-1)/2 = 24, so {7,24,25}. That's new.

k = 4 gives m = 9 and n = (81-1)/2 = 40, so {9,40,41}. Woah.

k = 5 gives m = 11 and n = (121-1)/2 = 60, so {11,60,61}. Ya know, the progression (40,41) to (60,61) looks suspicious...

k = 6 gives m = 13 and n = (169-1)/2 = 84, so {13,84,85}. Dang, that's a difference of 24, not 20.

But let's look at the pattern. The differences between successive n's are 4 (if you count zeros as the first one), 8, 12, 16, 20, 24, ... —ah ha! Successive multiples of 4. Or maybe that's successive even multiples of 2: 2x2, 4x2, 6x2. And notice that that even number is 1 less than m. So for all odd m, n is the twice the sum of all even numbers less than m.

That last paragraph may look like an awkward calculation, especially after the algebraic notation given above, but if you don't have algebra, this iterative method actually easier: You can set up columns and add successive numbers to the previous. To get the next triplet, for the first number, add 2 to 13 and get 15; for the second, add twice 14 = 28 to get 112; for the third, add 1 to that to get 113. Repeat until you get sick of it—which, when you're 11 and in the excitement of discovery, can be a while.

(Notice, which I did not at the time, that the iterative method produces a very efficient computer program for generating triplets: if you've one triplet, the next triple needs only four additions and one multiplication—or if you're clever, five additions.)

It is readily apparent that this will generate all Pythagorean triplets where two numbers are different by 1 (because, after all, we're stepping through all possibilities). Which is all very well if all you're interested in is very narrow triangles. What about triplets where two numbers are different by 2?

Method 2:

By the Pythagorean theorem, the squares of two numbers n and n+2 are different by an even square. That even square is the sum of two successive odd numbers—and those odd numbers are related to n by the formulas used above:

m2 = 2n+1 + (2n+1 + 2) = 4n + 4 = 4(n+1), where m is even, or n = (m/2)2 - 1. Setting m = 2k gives n = (2k/2)2 - 1 = k2 - 1 = (k+1)(k-1). So for the set of integers k, the triplets {2k, k2-1, k2+1} will include all triplets where two numbers are different by 2.

k = 1 gives m = 2 and n = 0. {2,0,2} does fit the Pythagorean theorem, but it's not a triangle but a line segment. Discard.

k = 2 gives m = 4 and n = 22 - 1 = 3, or {4,3,5}. Which is unique, but we already had it. Gee.

k = 3 gives m = 6 and n = 32 - 1 = 8, or {6,8,10}. Which is not unique: it's a multiple of {3,4,5}. But at least it worked.

k = 4 gives m = 8 and n = 42 - 1 = 15, or {8,15,17}. Hey! A new one! It works!

k = 5 gives m = 10 and n = 52 - 1 = 24, or {10,24,26}, a multiple of {5,12,13}.

k = 6 gives m = 12 and n = 62 - 1 = 35, or {12,35,37}. Another new one.

k = 7 gives m = 14 and n = 72 - 1 = 48, or {14,48,50}, a multiple of {7,24,25}.

k = 8 gives m = 16 and n = 82 - 1 = 63, or {16,63,65}. Another new one. Say, I'm beginning to see a pattern here. Lemme do two more.

k = 9 gives m = 18 and n = 92 - 1 = 80, or {18,80,82}, which, yup, is a multiple of {9,40,41}.

k = 10 gives m = 20 and n = 102 - 1 = 99, or {20,99,101}. Ha! We're getting new ones every other k.

Which, if we'd given a moment's thought, makes sense. If m is twice an odd number, then (m/2)2 - 1 is even, so all three numbers are even and so obviously the triplet is a multiple. So now we have a choice—if we're aiming to generate all Pythagorean triplets, keep marching; if only unique ones, do only odd k.

Meanwhile, we can find an iterative formula. The differences between successive n's are 3, 5, 7, 9, 11, 13, 15, 17, 19. That's a clear enough pattern. So, iterative: for the first, add 2 to get 22; for the second, add 22-1 to get 120; for the third, add 2 to that to get 122. {22,120,122}, a multiple of {11,60,61} as we expected. And so on.

Again, this generates all Pythagorean triplets where two numbers are different by 2.

Method 3:

Once more with feeling. How differences of 3?

Okay, so we're looking for odd squares that are the sum of three successive odd numbers. This is trickier.

m2 = 2n+1 + 2n+3 + 2n+5 = 6n + 9, where m is odd. Solving for n, we get n = (m2-9)/6. This is an integer only if m2 is a multiple of 3; since m must be odd, that means it's an odd multiple of 3. Substituting in m = 3(2k+1), we get

n = (9(2k+1)2 - 9)/6 = (36k2 + 36k + 9 - 9)/6 = 6k2 + 6k = 6k(k+1).

The triplet {m,n,n+3} is {3(2k+1), 6k(k+1), 6k(k+1)+3} for all integers k.

k = 1 gives m = 9 and n = 6x1x2, or {9,12,15}. Which is a multiple of {3,4,5}, but it shows we're on the right track.

k = 2 gives m = 15 and n = 6x2x3, or {15,36,39}, a multiple of {5,12,13}.

k = 3 gives m = 21 and n = 6x3x4, or {21,72,75}, a multiple of {7,24,25}.

k = 4 gives m = 27 and n = 6x4x5, or {27,120,123}, a multiple of {9,40,41}.

k = 5 gives m = 33 and n = 6x5x6, or {33,180,183}, a multiple of {11,60,61}.

As you can tell, this is depressing. In fact, looking at the formula for the triplet, the three terms have a common factor: 3. Divide it out, and you get the formula for Method 1. In other words, you can never have a unique triplet with a difference of 3. This was a conclusion I was never able to prove with my geometric method, although I suspected it—this shows the power of algebra.

It will, however, produce all triplets with two numbers with a difference of 3.

Method 4:

At the time, I never got as far as doing a diff of 4—I got distracted with the power of logarithms to calculate easy square roots (or any roots). But let's continue anyway and see if we can get a general formula.

The attack should be obvious. We're looking for squares that are the sum of 4 successive odd numbers (which must be even). Take m2 = 2n+1 + 2n+3 + 2n+5 + 2n+7 = 8n + 16, where m is even, so n = (m2 - 16)/8. Write m = 2j, and we get n = (j2 - 4)/2. For this to be an integer, j must be even, or m a multiple of 4. So write m = 4k, and we get get n = 2k2 - 2 = 2(k+1)(k-1). Thus a triplet of {4k, 2(k2-1), 2(k2+1)}.

There's a common factor in each term of the triplet: 2. Divide out, and you get Method 3. This will get you all triplets with diff(4), but no unique ones.

Method 5:

For diff(5), we search for squares the sum of 5 successive odd numbers, which must be odd. So m2 = 2n+1 + 2n+3 + 2n+5 + 2n+7 + 2n+9 = 10n + 25, so ——

Hey, wait a minute. Pattern recognition time. For diff(1), m2 = 2n+1; diff(2), m2 = 4n+4; diff(3), m2 = 6n+9; diff(4), m2 = 8n + 16. Obviously (and you can work out the algebra to prove it), for diff(j), m2 = 2jn + j2. But that's for the general method. We were doing diff(5):

Since m is odd, write m = 2k+1, and ——

More patterns. Did you notice that for diff(2), m was a multiple of 2, for diff(3), m was a multiple of 3, for diff(4) a multiple of 4? (And, for that matter, for diff(1) a multiple of 1. Duh.) Yes, you can show m must be a multiple of 5 in order to get an integer n—exercise for the reader. And, in general, for diff(j), m must be a multiple of j—for j even, any multiple; for odd j, odd multiples. Back to 5:

Set m = 5(2k+1), which gives 25(4k2 + 4k + 1) = 10n + 25, which with a little work, becomes n = 10k(k+1). The triplet is {5(2k+1), 10k(k+1), 10k(k+1)+5}. With a common factor, that reduces to multiples of diff(1).

Method J:

After all that, it's easy to show that, for j odd, the triplet is

{j(2k+1), 2jk(k+1), j(2k(k+1)+1)},

and for j even, the triplet is

{(j/2)2k, (j/2)(k2-1), (j/2)(k2+1)}.

For j and k over all positive integers, these formulas will produce all Pythagorean triplets. And all unique triplets will have j = 1 or 2.

Obviously, the best use of this is for generating random Pythagorean triplets.

But Wait, There's More!

Looks good, doesn't it? Except ...

There's this little problem that such triplets as {20,21,29} and {33,56,65} exist, for they can't be generated by these formulas. Oops. What can I say — I was 12.

Still, it makes for an interesting exercise: spot the flaw that lay in the theorem that Jack built. I'll leave this as is for the moment, until I get the time for a rigorously update. Exercise for the reader, and all that. Have fun.

Appendix A: Table of unique triplets derived above:

  {3,4,5}
  {4,3,5}
  {5,12,13}

  {7,24,25}
  {8,15,17}
  {9,40,41}

  {11,60,61}
  {12,35,37}
  {13,84,85}

  {15,112,113}
  {16,63,65}
  {17,144,145}

  {19,180,181}
  {20,99,101}
  ...