Practice Discrete Math

Algorithms / Gcd Number Theory

Least You Need to Know: GCD, Euclid's Algorithm, and Divisibility Invariants

Greatest-common-divisor questions are really about what divisibility information survives after replacing one number by a remainder. Euclid's algorithm, linear combinations, and the gcd/lcm identity are the interview patterns that matter most.

The least you need to know

Key notation

gcd(a, b) largest positive integer dividing both `a` and `b`
lcm(a, b) least positive common multiple
a = bq + r division with remainder `0 <= r < b`

Tiny worked example

  • To compute `gcd(84, 30)`, replace the pair by `(30, 24)`, then `(24, 6)`, then `(6, 0)`.
  • The last nonzero remainder is `6`, so the gcd is `6`.
  • Because the gcd is preserved at each remainder step, Euclid's algorithm is both correct and fast.

Common mistakes

How to recognize this kind of problem

Start practice