Maths in Real Life

Bézout’s Identity: How Euclid’s Algorithm Solves Diophantine Equations

EuclidCurve Smart Team · 2026-11-19 · 8 min read

For young mathematicians in the UK, USA, UAE, Singapore and Hong Kong

A sweet shop sells sweets in bags of 7 and bags of 11. Can you buy exactly 100 sweets, using only whole bags? Make your guess before you read on.

Guess first: Can you buy exactly 100 sweets using only bags of 7 and bags of 11?

  • No, because 7 and 11 do not divide 100
  • Yes, with 8 bags of 7 and 4 bags of 11
  • Only with 100 bags of 1

Yes: 8 × 7 + 4 × 11 = 56 + 44 = 100. Neither 7 nor 11 divides 100, but you do not need them to. You need a combination of them that lands on 100.

[Interactive tool: open this page in a browser to try it.]

Play with the target above. Some numbers have several answers, and some have none. Which ones? Keep that question in mind, because Bézout’s identity answers it.

Real life: where this shows up

Exact change. A cashier who has only 2p and 5p coins can pay exactly 9p, but not 1p. Which amounts are possible is a Bézout question: they are the multiples of the HCF, 1, once the amount is large enough.

Guess first: You have only 6p and 9p coins. Can you pay exactly 10p?

  • Yes, one 6p and one 9p coin
  • No, because the HCF of 6 and 9 is 3 and 3 does not divide 10
  • Yes, with two 5p coins

No. Every total made from 6p and 9p is a multiple of 3, and 10 is not. The gcd test settles it before you try any coins.

Online payments. The RSA code that protects card payments needs a secret number d with e × d \equiv 1 \pmodm. Finding d is exactly a Bézout problem, and the extended Euclid algorithm finds it very quickly, even for numbers with hundreds of digits.

Guess first: Why do computers use Euclid’s algorithm to find the secret number in RSA?

  • It guesses random numbers until one works
  • It finds the answer quickly, even for huge numbers
  • It only works for numbers smaller than 100

Trying every possibility is hopeless for huge numbers. The Euclid algorithm takes only a few steps, roughly proportional to the number of digits, so it stays fast.

Bézout’s identity

Take two whole numbers a and b, and let d = \gcd(a, b) be their highest common factor. Bézout’s identity says there are whole numbers x and y with ax + by = d. In words: the HCF can always be built from multiples of a and b. It is also the smallest positive number that can be built this way.

Example: \gcd(39, 15) = 3, and 2 × 39 - 5 × 15 = 78 - 75 = 3. So x = 2 and y = -5 work. Notice that x and y can be negative.

Euclid’s algorithm, as a diagram

Step 1 is the ordinary Euclid algorithm: divide, keep the remainder, repeat until the remainder is 0. Step 2 is the new part: climb back up and rewrite each remainder. Use the diagram below. Try 39 and 15 first, then pick your own pair.

[Interactive tool: open this page in a browser to try it.]

Here is the same method for 11 and 7, written out by hand:

11 = 1 × 7 + 4

7 = 1 × 4 + 3

4 = 1 × 3 + 1

The last remainder is 1, so \gcd(11, 7) = 1. Climbing back up: 1 = 4 - 3 = 4 - (7 - 4) = 2 × 4 - 7 = 2 × (11 - 7) - 7 = 2 × 11 - 3 × 7. Check: 22 - 21 = 1.

Your turn

Using 1 = 2 × 11 - 3 × 7, what is y in 7x + 11y = 1 when x = -3?

Solution: 2 × 11 - 3 × 7 = 1, so y = 2 (and x = -3).

From 1 to 100

Multiply the identity by 100: 7 × (-300) + 11 × 200 = 100. So x = -300 and y = 200 is one solution, but bags cannot be negative, so we need x \ge 0 and y \ge 0.

Every solution has the form x = -300 + 11t and y = 200 - 7t for a whole number t. Adding 11 to x and taking 7 from y does not change 7x + 11y, because 7 × 11 - 11 × 7 = 0. Now we need both numbers to be 0 or more. That forces t = 28, giving x = 8 and y = 4.

When can a Diophantine equation be solved?

Take ax + by = c with whole numbers a, b, c, and let d = \gcd(a, b). The equation has whole-number solutions exactly when d divides c.

Why? The left side is always a multiple of d, because d divides both a and b. If d does not divide c, no solution exists. If it does, Bézout’s identity gives ax + by = d, and multiplying by c / d gives a solution.

Example: 39x + 15y = 12. Since 12 = 4 × 3 = 4 × (2 × 39 - 5 × 15) = 8 × 39 - 20 × 15, one solution is x = 8, y = -20. All solutions are x = 8 + 5t and y = -20 - 13t.

Guess first: Does 6x + 9y = 10 have any whole-number solutions?

  • Yes, many
  • Yes, exactly one
  • No

No. The left side is always a multiple of 3, and 10 is not a multiple of 3.

Knowledge check

Answer these without looking back. Each one tests a different idea from this post.

Guess first: Check 1: Which is a correct way to write \gcd(15, 35) = 5 in Bézout form?

  • 5 = 2 × 15 + 35
  • 5 = 35 - 2 × 15
  • 5 = 15 × 35

35 - 2 × 15 = 35 - 30 = 5. The first gives 65 and the last gives 525.

Your turn

Check 2: Find x with 0 \le x < 11 such that 7x \equiv 1 \pmod11.

Solution: 7 × 8 = 56 = 55 + 1, so x = 8. This is the modular inverse of 7 mod 11.

Guess first: Check 3: Can 4x + 8y = 6 be solved in whole numbers?

  • Yes
  • No

No. \gcd(4, 8) = 4 and 4 does not divide 6.

Your turn

Check 4: How many pairs of whole numbers x, y \ge 0 satisfy 3x + 5y = 14?

Solution: Only y = 1 works: 14 - 5 = 9 = 3 × 3, so x = 3. Check: 9 + 5 = 14.

Where this shows up in exams: IB Maths AA

Integer solutions, the HCF and Euclid-style working appear in number theory questions in IB Maths AA. Write every step of the algorithm clearly, because examiners mark the working.

Explore IB Maths AA

Try it at home: the sweet shop

  1. Pick two bag sizes with no common factor, for example 5 and 8.
  2. Run the Euclid algorithm on them until the remainder is 1.
  3. Climb back up to write 1 as a combination of 5 and 8.
  4. Multiply through to make a target, for example 37 sweets. Find a non-negative solution.
  5. Bonus: find a target that cannot be made with bags of 5 and 8, and explain why.

Practice: master Bézout

Guess first: Practice 1: What is \gcd(12, 18)?

  • 2
  • 3
  • 6

12 = 2 × 6 and 18 = 3 × 6, so 6 divides both. Nothing larger does, so the HCF is 6.

Your turn

Practice 2: What is \gcd(252, 105)?

Solution: 252 = 2 × 105 + 42, 105 = 2 × 42 + 21, 42 = 2 × 21 + 0. The last non-zero remainder is 21.

Guess first: Practice 3: Does 15x + 10y = 7 have a whole-number solution?

  • Yes
  • No

No. \gcd(15, 10) = 5 and 5 does not divide 7.

Your turn

Practice 4: Find the unique whole number x with 0 \le x < 4 such that 9x + 4y = 1 has a whole-number y.

Solution: x = 1 gives 9 - 1 = 8 = 4 × 2, so y = -2. Check: 9 - 8 = 1.

Your turn

Practice 5: For 7x + 11y = 100 with x \ge 0 and y \ge 0, how many bags of 7 are needed?

Solution: 7 × 8 + 11 × 4 = 56 + 44 = 100, so 8 bags of 7 and 4 bags of 11.

Quick questions

What is Bézout’s identity?

For whole numbers a and b with HCF d, there are whole numbers x and y such that ax + by = d. The numbers x and y may be negative.

What is a Diophantine equation?

An equation whose solutions must be whole numbers, named after Diophantus of Alexandria. A linear one has the form ax + by = c.

When does ax + by = c have whole-number solutions?

Exactly when gcd(a, b) divides c. The left side is always a multiple of the gcd, so otherwise it can never equal c.

Why is it called Bézout’s identity?

After Étienne Bézout, an 18th-century French mathematician who published it in 1779. Similar results were known earlier, so it is sometimes called the Bachet-Bézout theorem.

Can it be used with more than two numbers?

Yes. The HCF of several numbers can always be written as a whole-number combination of all of them.

Written by the EuclidCurve Smart Team. We are the maths and physics tutors behind EuclidCurve. We teach IB, AMC, ESAT and TMUA students in the UK, USA, UAE, Singapore and Hong Kong, and we write about the maths hiding in everyday life.

Keep exploring

Book a free class · Try today's IMO problem