Greatest common divisor

Use the Euclidean algorithm to find the gcd of two positive integers efficiently.

0/5 marked as readthis browser

What’s happening here?

A common divisor of a and b also divides a − qb for any integer q. Replacing a by its remainder therefore preserves the common divisors while making the numbers smaller. Repeating this process leads to the greatest common divisor.

KEEP THIS RELATIONSHIP

The Euclidean algorithm preserves the common divisors by replacing (a, b) with (b, a mod b).

A worked example.

gcd(48, 18)
  1. 1

    48 = 18 × 2 + 12.

  2. 2

    18 = 12 × 1 + 6.

  3. 3

    12 = 6 × 2 + 0. The last nonzero divisor is 6.

A common wrong turn.

The final remainder is zero; the gcd is the divisor from that last division, not zero.

Why this works.

If a=bq+r, a number divides both a and b exactly when it divides both b and r. This is why replacing a pair by the divisor and remainder preserves the gcd. Repeating gives the Euclidean algorithm and ends at the last nonzero remainder.

Check it. Apply it. Explain it.

Three questions, with hints and worked feedback. Your score counts your first submitted answer. Practice only · scores stay in this session and do not enter your account history.

Question 1 of 30 correct / 0 answered

Greatest common divisor

What is gcd(30, 18)?

Videos and further reading

These resources open in a new tab on the publisher’s website.

Finished reading?Stored in this browser; separate from practice grades.

Put the idea to work.

Try a generated question and get a worked explanation after checking your answer.

Go to practice