HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →
Live examsInternational live exams are comingTell us which exam you want first. Timed practice papers are open now.See what is coming →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Space Science and Astronomy
  • Earth and Environmental Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
Diophantine Equations and Pythagorean Triples
  1. 1Linear Diophantine equations
  2. 2Pythagorean triples
  3. 3Factoring, bounding and Pell's equation
  1. Mathematics
  2. /Diophantine Equations and Pythagorean Triples
  3. /Pythagorean triples

Course 28.3·Lesson 2 of 3

Pythagorean triples

A Pythagorean triple is three positive integers with a2+b2=c2a^2 + b^2 = c^2a2+b2=c2

Lesson 2 of 3Show all lessonsHide
Diophantine Equations and Pythagorean Triples
  1. 1Linear Diophantine equations
  2. 2Pythagorean triples
  3. 3Factoring, bounding and Pell's equation

Try the code and the simulations beside the lesson.

What you will be able to do

  • generate every primitive Pythagorean triple from two numbers mmm and nnn, and prove that the formula gives them all;
  • use the divisibility facts about triples;
  • find every triple with a given leg or a given hypotenuse.

Rope with knots

Builders in many old cultures laid out a right angle with a rope knotted into 121212 equal parts, pulled into a 333, 444, 555 triangle. The numbers 3,4,53, 4, 53,4,5 satisfy 32+42=523^2 + 4^2 = 5^232+42=52. So do 5,12,135, 12, 135,12,13 and 8,15,178, 15, 178,15,17. A tablet from Babylon, called Plimpton 322, lists many such triples, some with numbers in the thousands.

Three positive integers with a2+b2=c2a^2 + b^2 = c^2a2+b2=c2 form a Pythagorean triple. It is primitive when gcd⁡(a,b,c)=1\gcd(a, b, c) = 1gcd(a,b,c)=1. Any triple is a multiple of a primitive one: 6,8,106, 8, 106,8,10 is 2×(3,4,5)2 \times (3, 4, 5)2×(3,4,5).

The formula

Every primitive triple (with bbb the even leg) is

a=m2−n2,b=2mn,c=m2+n2,\displaystyle a = m^2 - n^2, \qquad b = 2mn, \qquad c = m^2 + n^2,a=m2−n2,b=2mn,c=m2+n2,

where m>n>0m > n > 0m>n>0, gcd⁡(m,n)=1\gcd(m, n) = 1gcd(m,n)=1, and one of mmm, nnn is even.

Checking that these numbers work is algebra: (m2−n2)2+(2mn)2=m4+2m2n2+n4=(m2+n2)2(m^2 - n^2)^2 + (2mn)^2 = m^4 + 2m^2n^2 + n^4 = (m^2 + n^2)^2(m2−n2)2+(2mn)2=m4+2m2n2+n4=(m2+n2)2.

Why the formula gives every primitive triple

  1. Parity. In a primitive triple aaa and bbb are not both even. They are not both odd either: then c2=a2+b2≡2(mod4)c^2 = a^2 + b^2 \equiv 2 \pmod 4c2=a2+b2≡2(mod4), which no square is. So one leg is even. Call it bbb. Then aaa and ccc are odd.
  2. Factor. b2=c2−a2=(c−a)(c+a)b^2 = c^2 - a^2 = (c - a)(c + a)b2=c2−a2=(c−a)(c+a). Both factors are even. Write c+a=2uc + a = 2uc+a=2u and c−a=2vc - a = 2vc−a=2v, so (b2)2=uv\left(\tfrac{b}{2}\right)^2 = uv(2b​)2=uv.
  3. Coprime. Any common factor of uuu and vvv divides u+v=cu + v = cu+v=c and u−v=au - v = au−v=a. Since gcd⁡(a,c)=1\gcd(a, c) = 1gcd(a,c)=1, we get gcd⁡(u,v)=1\gcd(u, v) = 1gcd(u,v)=1.
  4. Squares. Two coprime numbers whose product is a square are both squares. So u=m2u = m^2u=m2, v=n2v = n^2v=n2.
  5. Read off. c=u+v=m2+n2c = u + v = m^2 + n^2c=u+v=m2+n2, a=u−v=m2−n2a = u - v = m^2 - n^2a=u−v=m2−n2, b=2mnb = 2mnb=2mn. Since aaa is odd, mmm and nnn have opposite parity.
A scatter plot of the primitive Pythagorean triples with hypotenuse below 100, with the odd leg across and the even leg upwards. Each of the sixteen dots is a triple such as (3, 4), (5, 12), (8, 15) drawn at (15, 8), and (65, 72). Dots with the same m lie on a curve labelled by m, from m = 2 to m = 9, and the quarter circle of radius 100 marks the limit.

Facts about every triple

In every Pythagorean triple:

  • one leg is a multiple of 333. A square is 000 or 111 modulo 333. If neither leg were a multiple of 333, then c2≡2c^2 \equiv 2c2≡2.
  • one leg is a multiple of 444. In a primitive triple b=2mnb = 2mnb=2mn, and one of m,nm, nm,n is even.
  • one of the three sides is a multiple of 555. A square is 000, 111 or 444 modulo 555, and the sums that are squares force a 000 somewhere.

So the product abcabcabc is always a multiple of 3×4×5=603 \times 4 \times 5 = 603×4×5=60, and 3,4,53, 4, 53,4,5 shows that 606060 is best possible.

Triples with a given side

A given leg. Find every triple with a leg 121212. Write 122=144=(c−b)(c+b)12^2 = 144 = (c - b)(c + b)122=144=(c−b)(c+b). The two factors have the same parity and c−b<c+bc - b < c + bc−b<c+b. The pairs of even factors are 2×722 \times 722×72, 4×364 \times 364×36, 6×246 \times 246×24 and 8×188 \times 188×18. They give (c,b)=(37,35)(c, b) = (37, 35)(c,b)=(37,35), (20,16)(20, 16)(20,16), (15,9)(15, 9)(15,9) and (13,5)(13, 5)(13,5). So there are 444 triples: (12,35,37)(12, 35, 37)(12,35,37), (12,16,20)(12, 16, 20)(12,16,20), (9,12,15)(9, 12, 15)(9,12,15) and (5,12,13)(5, 12, 13)(5,12,13).

A given hypotenuse. A prime ppp is the hypotenuse of a primitive triple exactly when p≡1(mod4)p \equiv 1 \pmod 4p≡1(mod4). For example 13=32+2213 = 3^2 + 2^213=32+22 gives (5,12,13)(5, 12, 13)(5,12,13). In general, ccc is the hypotenuse of a primitive triple exactly when every prime factor of ccc is 111 modulo 444. If ccc has kkk different such primes, there are 2k−12^{k-1}2k−1 primitive triples with hypotenuse ccc.

Practise with new numbers

Try it in Python

The program lists the primitive triples with hypotenuse below 606060 from the formula, and checks the list against a search over every pair of legs.

Read it first. Then run it. Do the formula and the search give the same list?

1. Change limit = 60 to limit = 130. How many primitive triples are there now?

2. Remove the test (m - n) % 2 == 1. Which triples now appear twice, or not primitive?

Common mistakes

  • Forgetting the conditions on mmm and nnn. m=3m = 3m=3, n=1n = 1n=1 gives (8,6,10)(8, 6, 10)(8,6,10), which is not primitive: mmm and nnn are both odd.
  • Missing the non-primitive triples. A question about "all triples" includes multiples such as (9,12,15)(9, 12, 15)(9,12,15).
  • Taking factor pairs of different parity. In (c−b)(c+b)=a2(c - b)(c + b) = a^2(c−b)(c+b)=a2 the two factors must be both even or both odd.
  • Assuming the even leg is always the larger one. In (20,21,29)(20, 21, 29)(20,21,29) the even leg is the smaller.

Remember these

  • Primitive triples: m2−n2m^2 - n^2m2−n2, 2mn2mn2mn, m2+n2m^2 + n^2m2+n2, with m>nm > nm>n coprime and of opposite parity.
  • Every triple is kkk times a primitive one.
  • One leg is a multiple of 333, one leg of 444, one side of 555; so 60∣abc60 \mid abc60∣abc.
  • Given leg aaa: factor a2=(c−b)(c+b)a^2 = (c - b)(c + b)a2=(c−b)(c+b) into two factors of the same parity.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. Which primitive triple comes from m=5m = 5m=5, n=2n = 2n=2?
  1. Is (9,40,41)(9, 40, 41)(9,40,41) primitive? Which mmm and nnn give it?
  1. Why is there no right triangle with integer sides and both legs odd?
  1. Find a triple with hypotenuse 171717.
  1. How many triples have the leg 888?

References

  • David M. Burton, Elementary Number Theory, Chapter 12, section on the equation x2+y2=z2x^2 + y^2 = z^2x2+y2=z2.

Enroll free in Diophantine Equations and Pythagorean Triples

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Take the tests of this lesson and its course.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (3)

A carpenter has a straight piece of length 24 cm for one leg of a right triangle…Hard · High School (11–12) · by StemPy Lab
Prove that the product abc of the three sides of every Pythagorean triple is a multiple of 60…Advanced · High School (11–12) · by StemPy Lab
A teacher wants a list of every primitive Pythagorean triple whose hypotenuse is less than 100…Advanced · High School (11–12) · by StemPy Lab
Next: lesson 3Factoring, bounding and Pell's equation
← Diophantine Equations and Pythagorean Triples
← Linear Diophantine equationsFactoring, bounding and Pell's equation →

Discussion (0)

Log in to join the discussion.

Loading…

; it is primitive when the three share no factor. Every primitive triple is
m2−n2m^2 - n^2m2−n2
,
2mn2mn2mn
,
m2+n2m^2 + n^2m2+n2
for coprime
m>nm > nm>n
of opposite parity, and every triple is a multiple of a primitive one. One leg is a multiple of
333
, one of
444
, and one side is a multiple of
555
.
about 25 min1 simulation1 practice question1 Python program5 questions to check yourself

This lesson is free to read. Enroll free to keep your place and take the tests.

Python Labs (1)

Pythagorean triples by formula and by searchPython Lab · by StemPy Lab