Euclid's formula generates Pythagorean triples from any two positive whole numbers m > n: a = m2 − n2, b = 2mn and c = m2 + n2. With m = 2 and n = 1 it gives 3, 4, 5. When m and n share no factor and are not both odd, the triple is primitive, and every primitive triple appears exactly once.
How to use Euclid's formula
a = m2 − n2, b = 2mn, c = m2 + n2- Pick two whole numbers m and n with m > n > 0.
- Compute m2 − n2, 2mn and m2 + n2.
- Order the legs if you like: m2 − n2 can come out bigger than 2mn, as in (m, n) = (4, 1), which gives 15, 8, 17.
Example 1: m = 5, n = 2
a= 52 − 22 = 25 − 4 = 21b= 2 × 5 × 2 = 20c= 52 + 22 = 25 + 4 = 29The triple is 20-21-29. Check: 400 + 441 = 841 = 292.
Why the formula always works
Expand both sides. The cross terms −2m2n2 and +4m2n2 combine into +2m2n2, which is exactly the middle term of (m2 + n2)2:
a2 + b2= (m2 − n2)2 + (2mn)2= m4 − 2m2n2 + n4 + 4m2n2= m4 + 2m2n2 + n4= (m2 + n2)2 = c2This is an identity, true for every pair of numbers, so whole numbers m > n > 0 always give a genuine triple with three positive sides. That makes the formula ideal for writing your own practice problems.
Derivation 1: factoring a difference of squares
Where could such a formula come from? Start from a2 + b2 = c2 and move a2 across, then factor the difference of two squares:
b2 = c2 − a2 = (c − a)(c + a)We want the product (c − a)(c + a) to be a perfect square. The simplest way to arrange that is to make each factor twice a square:
c + a = 2m2, c − a = 2n2Then (c − a)(c + a) = 4m2n2 = (2mn)2, so b = 2mn. Adding the two equations and halving gives c = m2 + n2; subtracting and halving gives a = m2 − n2. That is Euclid's formula.
The choice "twice a square" looks like a lucky guess, but it is forced. For a primitive triple with a odd, c + a over 2 and c − a over 2 share no factor and multiply to a square, so each must be a square. The full proof is on the primitive Pythagorean triples page.
Derivation 2: rational points on the unit circle
Divide a2 + b2 = c2 by c2:
(a over c)2 + (b over c)2 = 1So every triple gives a point (a over c, b over c) on the unit circle x2 + y2 = 1 (the circle of radius 1 centered at the origin) whose coordinates are fractions. Conversely, a point with fraction coordinates, written over a common denominator as (p over r, q over r), gives p2 + q2 = r2. Finding triples is the same problem as finding rational points on the circle.
Here is the trick. We already know one rational point, (−1, 0). Draw a line through it with slope t, so y = t(x + 1), and see where else it meets the circle. Substituting:
x2 + t2(x + 1)2= 1(1 + t2)x2 + 2t2x + (t2 − 1)= 0One solution is x = −1, the point we started from. The two solutions of a quadratic add up to −2t2 over 1 + t2, so the other one is
x = 1 − t2 over 1 + t2, y = t(x + 1) = 2t over 1 + t2If the slope t is a fraction, these coordinates are fractions too. And if a point (x, y) on the circle has fraction coordinates, the line joining it to (−1, 0) has the fraction slope y over x + 1. So rational points and rational slopes match up one to one. Writing the slope as t = n over m and multiplying top and bottom by m2:
x = m2 − n2 over m2 + n2, y = 2mn over m2 + n2The numerators and the denominator are exactly Euclid's a, b and c.
| Slope t = n/m | Second point (x, y) | Triple |
|---|---|---|
| 1/2 | (3/5, 4/5) | 3-4-5 |
| 1/3 | (8/10, 6/10) = (4/5, 3/5) | 4-3-5 (after cancelling 2) |
| 2/3 | (5/13, 12/13) | 5-12-13 |
| 1/4 | (15/17, 8/17) | 15-8-17 |
| 3/4 | (7/25, 24/25) | 7-24-25 |
| 2/5 | (21/29, 20/29) | 21-20-29 |
| 1/6 | (35/37, 12/37) | 35-12-37 |
Look at the slope 1 over 3. It gives (8 over 10, 6 over 10), which cancels to (4 over 5, 3 over 5): the 3-4-5 triangle again, with its legs swapped. That is why pairs with m and n both odd add nothing new. The same picture explains a trigonometry formula: if the point sits at angle θ, the line makes angle θ over 2, so t = tanθ over 2 and cosθ = 1 − t2 over 1 + t2, sinθ = 2t over 1 + t2. Check them with the Pythagorean identity.
When is the triple primitive?
Key ideaEuclid's formula gives a primitive triple exactly when m > n > 0, gcd(m, n) = 1, and m and n are not both odd. Every primitive triple comes from exactly one such pair.
- If m and n share a factor d, then d2 divides all three numbers. Example: (6, 3) gives 27-36-45, which is 9 × (3-4-5).
- If m and n are both odd, all three numbers are even. Halving gives the triple from M = m + n over 2, N = m − n over 2 with the legs swapped. Example: (3, 1) gives 8-6-10, and halving gives 4-3-5, the triple from (2, 1).
- If both are even, they share the factor 2, so the first rule already rules them out.
What about non-primitive triples? Put a multiplier in front: a = k(m2 − n2), b = 2kmn, c = k(m2 + n2). With k included, the formula produces every Pythagorean triple (up to the order of the legs). Without it, some are missed: 9-12-15 cannot be written as (m2 − n2, 2mn, m2 + n2) for any whole numbers m and n.
Table of triples for m up to 8
All 28 pairs with 1 ≤ n < m ≤ 8. Exactly 15 of them give primitive triples; the rest are multiples, and the last column names the primitive triple each one is a multiple of.
| m | n | a = m² − n² | b = 2mn | c = m² + n² | Primitive? |
|---|---|---|---|---|---|
| 2 | 1 | 3 | 4 | 5 | primitive |
| 3 | 1 | 8 | 6 | 10 | both odd: 2 × (4, 3, 5) |
| 3 | 2 | 5 | 12 | 13 | primitive |
| 4 | 1 | 15 | 8 | 17 | primitive |
| 4 | 2 | 12 | 16 | 20 | both even: 4 × (3, 4, 5) |
| 4 | 3 | 7 | 24 | 25 | primitive |
| 5 | 1 | 24 | 10 | 26 | both odd: 2 × (12, 5, 13) |
| 5 | 2 | 21 | 20 | 29 | primitive |
| 5 | 3 | 16 | 30 | 34 | both odd: 2 × (8, 15, 17) |
| 5 | 4 | 9 | 40 | 41 | primitive |
| 6 | 1 | 35 | 12 | 37 | primitive |
| 6 | 2 | 32 | 24 | 40 | both even: 8 × (4, 3, 5) |
| 6 | 3 | 27 | 36 | 45 | gcd(6, 3) = 3: 9 × (3, 4, 5) |
| 6 | 4 | 20 | 48 | 52 | both even: 4 × (5, 12, 13) |
| 6 | 5 | 11 | 60 | 61 | primitive |
| 7 | 1 | 48 | 14 | 50 | both odd: 2 × (24, 7, 25) |
| 7 | 2 | 45 | 28 | 53 | primitive |
| 7 | 3 | 40 | 42 | 58 | both odd: 2 × (20, 21, 29) |
| 7 | 4 | 33 | 56 | 65 | primitive |
| 7 | 5 | 24 | 70 | 74 | both odd: 2 × (12, 35, 37) |
| 7 | 6 | 13 | 84 | 85 | primitive |
| 8 | 1 | 63 | 16 | 65 | primitive |
| 8 | 2 | 60 | 32 | 68 | both even: 4 × (15, 8, 17) |
| 8 | 3 | 55 | 48 | 73 | primitive |
| 8 | 4 | 48 | 64 | 80 | both even: 16 × (3, 4, 5) |
| 8 | 5 | 39 | 80 | 89 | primitive |
| 8 | 6 | 28 | 96 | 100 | both even: 4 × (7, 24, 25) |
| 8 | 7 | 15 | 112 | 113 | primitive |
For bigger values, or to list every triple containing a particular number, use the Pythagorean triples generator, or browse the full list of Pythagorean triples.
Other formulas for generating triples
Pythagoras' formula: start from an odd number
For any odd number a = 2k + 1, the numbers b = 2k2 + 2k and c = 2k2 + 2k + 1 complete a triple. Equivalently, b = a2 − 1 over 2 and c = b + 1. This is Euclid's formula with m = k + 1 and n = k, and it produces 3-4-5, 5-12-13, 7-24-25, 9-40-41, 11-60-61 and every other triple whose hypotenuse is one more than a leg.
The picture shows the idea. Square numbers grow by odd numbers: k2 + (2k + 1) = (k + 1)2. Whenever the odd number 2k + 1 happens to be a perfect square, say a2, you get k2 + a2 = (k + 1)2, a triple.
Plato's formula: start from an even number
For an even number a = 2k, take b = k2 − 1 and c = k2 + 1. This is Euclid's formula with m = k and n = 1. With k = 4 it gives 8-15-17 and with k = 6 it gives 12-35-37. When k is odd all three numbers come out even (k = 3 gives 6-8-10), so it is primitive only for even k. Proclus (5th century CE), in his commentary on Euclid's proof of the theorem, credits the odd-number method to Pythagoras and the even-number method to Plato.
Fibonacci's method: adding odd numbers
In his Book of Squares (Liber Quadratorum, 1225), Leonardo of Pisa, known as Fibonacci, used the fact that the first k odd numbers add up to k2. Choose an odd square, such as 81 = 92. It is the odd number 2k + 1 with k = 40. The 40 odd numbers before it add up to 402, and including 81 makes 41 odd numbers, which add up to 412:
(1 + 3 + 5 + ⋯ + 79) + 81= 1 + 3 + 5 + ⋯ + 81402 + 92= 412It is the same family as Pythagoras' formula, seen through sums of odd numbers.
Dickson's method: one formula for every triple
Leonard Eugene Dickson described a method that finds all triples, primitive or not. Choose an even number r and split r2 over 2 into two factors, r2 over 2 = st. Then (r + s, r + t, r + s + t) is a Pythagorean triple, because expanding shows (r + s)2 + (r + t)2 − (r + s + t)2 = r2 − 2st = 0.
Example: Dickson's method with r = 6
Here r2 over 2 = 18, and 18 splits as 1 × 18, 2 × 9 and 3 × 6.
(s, t) = (1, 18)→ (7, 24, 25)(s, t) = (2, 9)→ (8, 15, 17)(s, t) = (3, 6)→ (9, 12, 15)One choice of r gives three triples at once, including the non-primitive 9-12-15 that plain Euclid misses.
Comparing the methods
| Method | Formula | What it produces | Example |
|---|---|---|---|
| Euclid | (m² − n², 2mn, m² + n²) | every primitive triple (m, n coprime, not both odd) | m = 5, n = 2 → 21-20-29 |
| Pythagoras | (2k + 1, 2k² + 2k, 2k² + 2k + 1) | triples with c = b + 1 | k = 3 → 7-24-25 |
| Plato | (2k, k² − 1, k² + 1) | triples with c = b + 2 | k = 6 → 12-35-37 |
| Fibonacci | add the first k odd numbers, where 2k + 1 is an odd square | the same family as Pythagoras | 1 + 3 + … + 79 = 40², 40² + 9² = 41² |
| Dickson | (r + s, r + t, r + s + t) with r² = 2st | every triple, primitive or not | r = 6, s = 3, t = 6 → 9-12-15 |
There is one more generator with a very different flavour: three matrices that grow every primitive triple, exactly once, from 3-4-5. See the tree of primitive Pythagorean triples.
Worked examples
Example 2: every primitive triple with hypotenuse 65
We need m2 + n2 = 65. Trying m = 1, 2, …, 8 finds two pairs: 64 + 1 and 49 + 16.
(m, n) = (8, 1)→ (63, 16, 65)(m, n) = (7, 4)→ (33, 56, 65)Both pairs are coprime with one even number, so 16-63-65 and 33-56-65 are both primitive. They are the only primitive triples with hypotenuse 65.
Example 3: every triple with a leg of 20
As the even leg of a primitive triple, 2mn = 20, so mn = 10: (10, 1) gives 20-99-101 and (5, 2) gives 20-21-29. As an odd leg it is impossible, since 20 is even. Non-primitive triples come from multiples: 20 = 4 × 5 gives 4 × (5-12-13) = 20-48-52, and 20 = 5 × 4 gives 5 × (3-4-5) = 15-20-25.
The triples with a leg of 20 are 15-20-25, 20-21-29, 20-48-52 and 20-99-101, and a computer search confirms there are no others.
Example 4: find m and n for 48-55-73
The odd leg is 55. From c + a = 2m2 and c − a = 2n2:
m2 = 73 + 55 over 2 = 64, m = 8n2 = 73 − 55 over 2 = 9, n = 3So m = 8, n = 3. Check: 2 × 8 × 3 = 48.
Where the formula comes from
The formula is named after Euclid because it appears in his Elements (about 300 BCE), in Book X, as Lemma 1 attached to Proposition 29: "to find two square numbers such that their sum is also square". Euclid's version is stated with lengths rather than algebra, but it is the same construction. Much earlier, the Babylonian tablet Plimpton 322 (about 1800 BCE) records the sides of large right triangles such as 119-120-169 (its first row gives 119 and 169), which suggests its scribes had a reliable method; scholars still debate exactly what it was.
The circle method is close to how Diophantus of Alexandria (probably 3rd century CE) solved Problem 8 of Book II of his Arithmetica, "divide a given square into two squares". For the square 16 he used a line and found 16 = 256 over 25 + 144 over 25. Fermat later wrote his famous note about Fermat's Last Theorem in the margin beside this very problem.
Frequently asked questions
Does Euclid's formula give every Pythagorean triple?
It gives every primitive triple, each from exactly one pair (m, n). To get every triple you need the multiplier: k(m2 − n2), 2kmn, k(m2 + n2). Without k, triples such as 9-12-15 and 30-40-50 are missed.
What values of m and n give primitive triples?
Choose m > n > 0 with no common factor and with one of them even. For example (2, 1), (3, 2), (4, 1) and (4, 3) give 3-4-5, 5-12-13, 8-15-17 and 7-24-25. Pairs like (3, 1) or (6, 3) give multiples of smaller triples.
Who discovered Euclid's formula?
It is written down in Book X of Euclid's Elements (about 300 BCE), which is why it carries his name. Special cases were known earlier and were credited by later writers to Pythagoras and Plato, and Babylonian scribes had found large triples well over a thousand years before Euclid.
What happens if m = n, or if n is bigger than m?
If m = n, then a = 0, which is not a triangle. If n > m, then a is negative; dropping the sign gives the same triple as swapping m and n. That is why the rule is m > n > 0.
Sources and further reading
- Euclid, Elements, Book X, Lemma 1 to Proposition 29, in D. E. Joyce's online edition: Book X, Proposition 29.
- Proclus, A Commentary on the First Book of Euclid's Elements, translated by G. R. Morrow (Princeton University Press, 1970), on Proposition I.47.
- Leonardo Pisano (Fibonacci), The Book of Squares (1225), translated by L. E. Sigler (Academic Press, 1987).
- L. E. Dickson, History of the Theory of Numbers, Vol. II (Carnegie Institution of Washington, 1920).
- Formulas for generating Pythagorean triples, Wikipedia.
Keep exploring
- TriplesPythagorean TriplesWhat Pythagorean triples are, the common ones to know (3-4-5, 5-12-13, 8-15-17…), how to generate and recognize them, and the patterns every triple shares.
- TriplesPrimitive Pythagorean TriplesWhat makes a Pythagorean triple primitive, the quick gcd test, why exactly one leg is even, and a full proof that Euclid's formula gives every primitive triple.
- TriplesThe Tree of Primitive Pythagorean TriplesThe Berggren tree builds every primitive Pythagorean triple exactly once from 3-4-5 using three matrices. See the tree to depth 3, worked examples and the proof.
- CalculatorsPythagorean Triples GeneratorGenerate every Pythagorean triple up to any hypotenuse, use Euclid's formula, check whether three numbers form a triple, or find all triples with a number.
- TriplesList of Pythagorean TriplesComplete, computer-checked list of Pythagorean triples: all 52 with hypotenuse up to 100 and all 47 primitive triples up to 300, with m, n, perimeter and area.
- LearnEquation of a Circle and the Pythagorean TheoremWhy the equation of a circle, x² + y² = r², is the Pythagorean theorem, plus the center-radius form, worked examples and the inside/outside test.