Pythagorean Triple Generator

Integer right triangles from Euclid's formula.

  • Euclid's m and n
  • Search by any side
  • Primitive or all
Soft clay sticks laid out as right-angled triangles of several sizes on a small rounded clay table in daylight.

The guide

Every whole-number right triangle grows from two numbers, and a circle explains why

Euclid's formula turns any two whole numbers m and n into a right triangle with whole sides, and two plain conditions say exactly which of those triangles have no common factor.

  • 16 of 52 primitive triples among all triples up to a hypotenuse of 100
  • 13 triples that contain a leg of 60
  • 65 the first hypotenuse shared by more than two triples
  • 120 the first perimeter shared by three triples
Read the full guide Fold the guide 7 min · 12 sources

A Pythagorean triple is three positive whole numbers a, b and c with a squared plus b squared equal to c squared. The best known is (3, 4, 5), since 9 plus 16 is 25. Double every side and (6, 8, 10) works too, and so does every other multiple.

That doubling is the first thing to get straight. Wikipedia’s article on the subject calls a triple primitive when its three numbers share no divisor larger than 1. So (3, 4, 5) is primitive and (6, 8, 10) is not. Every triple is some whole number of copies of a primitive one, which means the primitive ones are the real inventory. Up to a hypotenuse of 100 there are 52 triples once the multiples are counted, and 16 of 52 are primitive.

The oldest known record of such triples is a Babylonian clay tablet, Plimpton 322, believed to be from around 1800 BC. Its date is not known accurately, and the MacTutor archive puts it between 1800 BC and 1650 BC. One row of the table relates to the triple (12709, 13500, 18541), and you can check by hand that the squares of the first two add up to the square of the third. Numbers that large are unlikely to have been found by trial, so scholars have suggested the scribes had a method.

The method that survives in the textbooks has a different author.

Two numbers make a triangle

Pick two whole numbers m and n with m larger than n. Set a equal to m squared minus n squared, b equal to 2mn, and c equal to m squared plus n squared. Then a squared plus b squared equals c squared, because both sides expand to m to the fourth plus 2 m squared n squared plus n to the fourth. With m = 2 and n = 1 the formula gives (3, 4, 5). With m = 5 and n = 2 it gives (21, 20, 29).

The recipe carries Euclid’s name, and Wikipedia ties it to Book X of his Elements. Little is known of Euclid’s life except that he taught at Alexandria, according to the MacTutor biography. What matters here is that the recipe is exact. It never produces a near miss or a triangle with a fractional side, however large m and n become.

Now the conditions. The triple is primitive exactly when m and n are coprime and one of them is even and the other odd. Each condition earns its place with a counterexample. Take m = 3 and n = 1, both odd. The formula returns (8, 6, 10), and every one of those sides is even, so the triple is twice (4, 3, 5). Take m = 4 and n = 2, which share the factor 2. The formula returns (12, 16, 20), which is four times (3, 4, 5).

Notice what the formula does not promise. A pair that breaks a condition still yields a genuine triple, just not a primitive one. When m and n are both odd and coprime, halving the sides repairs it, and Wikipedia gives the equivalent form a = mn, b = (m squared minus n squared) over 2, c = (m squared plus n squared) over 2.

The converse is the powerful half. Every primitive triple arises from exactly one pair of coprime m and n of opposite parity, after swapping the two legs if the odd one came second. That one statement implies there are infinitely many primitive triples. Exactly one leg is divisible by 3, exactly one is divisible by 4, and exactly one of a, b and c is divisible by 5. The product abc is always a multiple of 60. The hypotenuse is always odd. An even leg such as 6 can never belong to a primitive triple, because 2mn with one of m and n even is a multiple of 4.

Multiples, legs and perimeters

A primitive triple with hypotenuse h turns into a triple with hypotenuse N whenever h divides N. So the count of all triples up to a limit N is the sum, over every primitive triple, of N divided by its hypotenuse rounded down. Run that way, the formula gives 158 primitive triples and 881 in all up to a hypotenuse of 1,000, and 15,919 and 161,436 up to 100,000, counted by running it.

Searching from the other direction is just as exact. Take every triple that contains the leg 20. Write c minus b as d and c plus b as e. Then d times e equals 20 squared, and d and e must have the same parity so that b and c come out whole. Wikipedia’s article on generating formulas describes this method. For an even leg, halve it first and factor 10 squared: the divisors 1, 2, 4 and 5 below 10 give (20, 99, 101), (20, 48, 52), (20, 21, 29) and (20, 15, 25). A leg of 60 sits in 13 triples, the count the OEIS lists for it.

A given hypotenuse works through its prime factors. Each prime factor of a primitive hypotenuse has the form 4t plus 1, and the number of primitive triples with a hypotenuse c is 2 to the power k minus 1, where k counts the distinct prime factors of c, according to Wikipedia. Take 65, which is 5 times 13. That gives two primitive triples, (16, 63, 65) and (33, 56, 65), plus the multiples 13 times (3, 4, 5) and 5 times (5, 12, 13). Four in all, and 65 is the first hypotenuse where the count passes two.

Perimeters are the quirk. A primitive triple has perimeter 2m(m + n), so every perimeter is even and an odd one has no triple at all. Set the perimeter to 120 and you get three triangles, (20, 48, 52), (24, 45, 51) and (30, 40, 50). A scan of the even perimeters up to 400 shows that 120 is the first one that three triples share.

The circle behind the formula

Why should two numbers be enough? Divide a squared plus b squared equal to c squared by c squared and it becomes (a over c) squared plus (b over c) squared equal to 1. That is the equation of the unit circle, and a over c and b over c are fractions. A triple is a point on the circle with rational coordinates, and (3, 4, 5) is the point (4 over 5, 3 over 5).

Wikipedia states the correspondence cleanly: the rational points on the circle match the primitive triples one to one. So a list of primitive triples is a list of rational points on a circle, and the question becomes how to produce all of them.

Draw a line from the top of the circle to a point on the horizontal axis. Where it meets the circle again is the image of that axis point under stereographic projection. If the axis point is rational, the circle point is rational, and the reverse holds too. Writing the rational slope as n over m in lowest terms produces Euclid’s numbers. In the triangle the formula builds, the tangent of half the angle between the leg m squared minus n squared and the hypotenuse is exactly n over m.

That picture also explains the conditions. Coprime means the fraction is in lowest terms. The parity condition rules out pairs of two odd numbers, whose triples are twice a smaller primitive one.

There is a second way to reach every primitive triple. In 1934 B. Berggren showed that the primitive triples form a tree rooted at (3, 4, 5), where three fixed linear rules turn any triple into three new ones. From (3, 4, 5) they give (5, 12, 13), (21, 20, 29) and (15, 8, 17). The generator lists by Euclid’s parameters instead, because m and n are the more useful handle.

One more fact belongs here. Fermat’s right triangle theorem says that the area of a Pythagorean triangle is never a perfect square, which is why the area column in the list never lands on a square.

What the generator does and does not do

Every number on the page is an integer. Ordinary floating point loses exactness above 2 to the power 53, so the generator uses BigInt, which MDN describes as the right type when values past that size are expected. With m = 10 to the 19 plus 3 and n = 10 to the 18 plus 8, the squares of a and b add to the square of c with no rounding. The long searches run in a Web Worker, a background thread, so the page stays responsive while they work.

The caps are stated on the page. The hypotenuse list stops at 100,000, and a list of all triples stops at 100,000 rows even though 161,436 exist. A leg may reach 10 to the 12 and is factored by trial division. A hypotenuse search goes to 10 to the 9, and a perimeter to 10 to the 7. Coprimality is tested with the greatest common divisor, which Euclid’s algorithm computes quickly.

To see it all work, set the limit to 1,000 and ask for primitive triples: 158. Switch to all triples: 881. Search for the leg 20, then the hypotenuse 65, then the perimeter 120, and compare with the numbers above. Then enter m = 4 and n = 2 and watch the card explain why (12, 16, 20) is not primitive.

The generator cannot find triples of other powers, rational right triangles whose sides are not whole numbers, or the sides of a triangle from angles. A triangle with two sides of 1 has a hypotenuse of the square root of 2, which is not an integer, so it is a right triangle but not a Pythagorean triple.

Sources

Every source consulted for this guide. Links checked November 10, 2026.

  1. Pythagorean triple Wikipedia
  2. Formulas for generating Pythagorean triples Wikipedia
  3. Tree of primitive Pythagorean triples Wikipedia
  4. Fermat's right triangle theorem Wikipedia
  5. Plimpton 322 Wikipedia
  6. Euclidean algorithm Wikipedia
  7. Babylonian Pythagoras MacTutor History of Mathematics, University of St Andrews
  8. Euclid MacTutor History of Mathematics, University of St Andrews
  9. A046079: Number of Pythagorean triangles with leg n The On-Line Encyclopedia of Integer Sequences
  10. A046080: number of integer-sided right triangles with hypotenuse n The On-Line Encyclopedia of Integer Sequences
  11. BigInt MDN Web Docs
  12. Web Workers API MDN Web Docs