What's a Pythagorean Triple?
A Pythagorean Triple is a set of three integers which would fit as sides of a right triangle. In other words, three numbers a, b, and c such that a^{2} + b^{2} = c^{2}.
Finding triples  the formula
The short story goes like this:
 Pick an integer n.
 Pick another integer "gap" between 1 and n. Call it g.
 If 2g divides n^{2} + g^{2}, then you have a Pythagorean triple, namely

 a = n
 b = (n^{2}  g^{2}) / 2g
 c = (n^{2} + g^{2}) / 2g
Examples
 If n = 5 and g = 1, you get the triple 5, 12, 13.
 If n = 8 and g = 2, you get the triple 8, 15, 17.
You can verify algebraically that a^{2} + b^{2} = c^{2 }in all cases.
Foundations  adding odd integers
So how does this work?
Somewhere around 8th grade I learned that the sum of the first n odd integers is equal to the square of n.
 1 = 1 = 1^{2}
 1 + 3 = 4 = 2^{2}
 1 + 3 + 5 = 9 = 3^{2}
 1 + 3 + 5 + 7 = 16 = 4^{2}
 1 + 3 + 5 + ... + (2n  1) = n^{2}
Note that (2n  1) is the nth odd integer.
So the difference between any two consecutive squares is an odd number, and the difference between any two squares is the sum of consecutive odd numbers
 5^{2}  4^{2} = 25  16 = 9 = (2*5  1)
 6^{2}  5^{2} = 36  25 = 11 = (2*6  1)
 n^{2}  (n  1)^{2} = (2n  1)
 17^{2}  15^{2} = 289  225 = 64 = 31 + 33
 29^{2}  21^{2} = 841  441 = 400 = 93 + 95 + 97 + 99 + 101 + 103 + 105 + 107
Finding the triples  odd numbers
We're trying to find three numbers  a, b, and c  such that a^{2} + b^{2} = c^{2}.
Let's pick any number for a. If a is an odd number, then a^{2 }is an odd integer and is also the difference between two squares. For example, 3^{2} = 9. Since 9 is the 5th odd integer, it's the difference between the 4th and 5th squares  16 and 25. So (3, 4, 5) is a Pythagorean triple.
Pick an odd number. This will be your first number  a. 
3 
5 
n 
Square it. 
9 
25 
n^{2} 
Find out where the square comes among the odd numbers  add one and divide by two. This will be your highest number, c. 
(9 + 1) / 2 = 5 
(25 + 1) / 2 = 13 
(n^{2} + 1) / 2 
Subtract one to find the other number, b. 
4 
12 
[(n^{2} + 1) / 2]  1 = (n^{2}  1) / 2 
Triple 
3, 4, 5 
5, 12, 13 

So we get the formulas:
 a = n
 b = c  1 = (n^{2}  1) / 2
 c = (n^{2} + 1) / 2
Finding the triples  even numbers (gap of two)
If a is an even number, a^{2} is not odd, but it is the sum of two odd numbers. So a^{2} will be the difference between squares that are two apart.
Pick an even number. This will be our first number  a. 
6 
8 
n 
Square it. 
36 
64 
n^{2} 
The two odd numbers that add up to the square will average half the square. 
18 
32 
(n^{2} / 2) 
Add and subtract one to get the two odd numbers. 
17 + 19 
31 + 33 
((n^{2} / 2)  1) + (n^{2} / 2) + 1) 
Add one to the higher number and divide by two. This will be the highest number, c. 
(19 + 1) / 2 = 10 
(33 + 1) / 2 = 17 
(((n^{2} / 2) + 1) + 1) / 2 = (n^{2} + 4) / 4 
Subtract two to get the third number, b. 
8 
15 
((n^{2} + 4) / 4)  2 = (n^{2}  4) / 4 
In general...
Let's try a general case. For the example we'll use 9^{2} = 81 = 25 + 27 + 29. Since 29 is the 15th odd number, our high number will be 15. And since there were three odd numbers the other number for our triple will be three less, i.e. 12. So we get the triple (9, 12, 15).
Pick any positive integer. 
9 
n 
Square it. 
81 
n^{2} 
Guess how many odd numbers will add up to the square. Call it our "gap". 
3 
g 
Divide the square by the gap to find the average value. 
27 
n^{2} / g 
The range  difference between the highest and lowest of the odd numbers  will be two less than twice the gap. 
4 
2g  2 
Half of that will be above the average, so the add half the range to the average to get the highest of the odd numbers. 
27 + 2 = 29 
(n^{2} / g) + (2g  2) / 2 = (n^{2} / g) + (g  1) 
Add one and divide by two to find the highest number in our triple. 
(29 + 1) / 2 = 15 
(((n^{2} / g) + (g  1)) + 1) / 2 = ((n^{2} / g) + g) / 2 = (n^{2} + g^{2}) / 2g 
Subtract the gap to get the other number for the triple. 
15  3 = 12 
((n^{2} + g^{2}) / 2g)  g = (n^{2} + g^{2}  2g^{2}) / 2g = (n^{2}  g^{2}) / 2g 
So our final result is the formula from the top:
 a = n
 b = (n^{2}  g^{2}) / 2g
 c = (n^{2} + g^{2}) / 2g
Limiting the choices
We can pick any number for n, but what should be use for our gap, g?
 Obviously g has to be less than n, since otherwise our value for b would be zero or negative.
 Since (n^{2} + g^{2}) / 2g = ((n^{2} / g) + g) / 2, we know n^{2} / g must be an integer, so g must be a factor of n^{2}.
 If n is odd, n^{2} will also be odd, so b will be odd and c will be even, or vice versa. So g will have to be odd. Likewise if n is even, g will also be even.
Try it out!
We'll find all the Pythagorean triples that have 20 as one of the smaller numbers (a or b). Since 20 is even, we'll try all even values for g that are less than 20 and factors of 20^{2} = 400. If (n^{2} + g^{2}) / 2g is an integer, we've found a triple.
g 
n 
(n^{2} + g^{2}) / 2g 
Triple 
Reduced Triple 
2 
20 
404 / 4 = 101 
20, 99, 101 
20, 99, 101 
4 
20 
416 / 8 = 52 
20, 48, 52 
5, 12, 13 
6 
20 
6 not a factor of 400 


8 
20 
464 / 16 = 29 
20, 21, 29 
20, 21, 29 
10 
20 
500 / 20 = 25 
15, 20, 25 
3, 4, 5 
12 
20 
12 not a factor of 400 


14 
20 
14 not a factor of 400 


16 
20 
656 / 32, not an integer 


18 
20 
18 not a factor of 400 

