Euclid's Formula for Pythagorean Triples

Choose two whole numbers m > n > 0 and Euclid's formula turns them into a Pythagorean triple. See why it works, two ways to discover it, which choices give primitive triples, and the other classic recipes.

By the PythagoreanTheorem.net teamUpdated 12 min read

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
  1. Pick two whole numbers m and n with m > n > 0.
  2. Compute m2 − n2, 2mn and m2 + n2.
  3. 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 = 29

The 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 = c2

This 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 = 2n2

Then (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 = 1

So 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)= 0

One 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 + t2
(−1, 0)11 slope t = 1/2hits (3/5, 4/5)triple 3-4-5 slope t = 2/3hits (5/13, 12/13)triple 5-12-13 slope t = 3/4hits (7/25, 24/25)triple 7-24-25 slope t = 1/4hits (15/17, 8/17)triple 15-8-17
A line through (−1, 0) with a rational slope always meets the unit circle again at a point with rational coordinates, and clearing the denominators gives a triple. The grey dots are the rational points from m ≤ 5. The green point travels round the circle twice as fast as the line turns (an inscribed angle is half the central angle).

If 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 + n2

The numerators and the denominator are exactly Euclid's a, b and c.

Slope t = n/mSecond 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.
23456781234567 mn n = m 3-4-5 8-6-10 5-12-13 15-8-17 12-16-20 7-24-25 24-10-26 21-20-29 16-30-34 9-40-41 35-12-37 32-24-40 27-36-45 20-48-52 11-60-61 48-14-50 45-28-53 40-42-58 33-56-65 24-70-74 13-84-85 63-16-65 60-32-68 55-48-73 48-64-80 39-80-89 28-96-100 15-112-113 primitive m, n both odd or both even common factor (gcd = 3)
Every pair with n < m ≤ 8 and the triple it makes (in the order m² − n², 2mn, m² + n²). The 15 solid dots are the primitive triples. Grey dots, where m and n have the same parity, form a checkerboard; the orange dot (6, 3) fails only because 6 and 3 share the factor 3.

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.

All 28 pairs with 1 ≤ n < m ≤ 8. Non-primitive rows show the primitive triple they are a multiple of (legs in the same order).
mna = m² − n²b = 2mnc = m² + n²Primitive?
21345primitive
318610both odd: 2 × (4, 3, 5)
3251213primitive
4115817primitive
42121620both even: 4 × (3, 4, 5)
4372425primitive
51241026both odd: 2 × (12, 5, 13)
52212029primitive
53163034both odd: 2 × (8, 15, 17)
5494041primitive
61351237primitive
62322440both even: 8 × (4, 3, 5)
63273645gcd(6, 3) = 3: 9 × (3, 4, 5)
64204852both even: 4 × (5, 12, 13)
65116061primitive
71481450both odd: 2 × (24, 7, 25)
72452853primitive
73404258both odd: 2 × (20, 21, 29)
74335665primitive
75247074both odd: 2 × (12, 35, 37)
76138485primitive
81631665primitive
82603268both even: 4 × (15, 8, 17)
83554873primitive
84486480both even: 16 × (3, 4, 5)
85398089primitive
862896100both even: 4 × (7, 24, 25)
8715112113primitive

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.

4² = 16 9 new squares 9 = 3² 4² + 3² = 5² 5 × 5 = 25
The odd-number trick behind Pythagoras’ formula. Going from a 4 × 4 square to a 5 × 5 square adds an L-shaped border of 2 × 4 + 1 = 9 squares. Because 9 is itself a square, 4² + 3² = 5². The same thing happens whenever the border, 2k + 1, is an odd square.

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= 412

It 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

MethodFormulaWhat it producesExample
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 + 1k = 3 → 7-24-25
Plato(2k, k² − 1, k² + 1)triples with c = b + 2k = 6 → 12-35-37
Fibonacciadd the first k odd numbers, where 2k + 1 is an odd squarethe same family as Pythagoras1 + 3 + … + 79 = 40², 40² + 9² = 41²
Dickson(r + s, r + t, r + s + t) with r² = 2stevery triple, primitive or notr = 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 = 3

So 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

  1. Euclid, Elements, Book X, Lemma 1 to Proposition 29, in D. E. Joyce's online edition: Book X, Proposition 29.
  2. Proclus, A Commentary on the First Book of Euclid's Elements, translated by G. R. Morrow (Princeton University Press, 1970), on Proposition I.47.
  3. Leonardo Pisano (Fibonacci), The Book of Squares (1225), translated by L. E. Sigler (Academic Press, 1987).
  4. L. E. Dickson, History of the Theory of Numbers, Vol. II (Carnegie Institution of Washington, 1920).
  5. Formulas for generating Pythagorean triples, Wikipedia.

Keep exploring