How to find the GCD with the Euclidean algorithm

The Euclidean algorithm computes the greatest common divisor (GCD) of two integers by repeatedly applying the division algorithm until a remainder of zero is reached. It applies to any pair of integers where at least one is non-zero, relying on the identity GCD(a,b)=GCD(b,amodb)GCD(a, b) = GCD(b, a \bmod b).

The setup

Identify the two integers aa and bb. Ensure ab>0a \ge b > 0. If they are negative, use their absolute values since GCD(a,b)=GCD(a,b)GCD(a, b) = GCD(|a|, |b|). If b=0b=0, the GCD is aa and the process is complete.

The steps

  1. Divide aa by bb to find the integer quotient qq and remainder rr, writing it in the form a=bq+ra = bq + r where 0r<b0 \le r < b. 2. If r=0r = 0, the algorithm terminates and bb is the GCD. 3. If req0r eq 0, replace aa with bb, and replace bb with rr. 4. Repeat step 1 with the new values of aa and bb.

Checking the result

Verify that your final non-zero remainder cleanly divides both of your starting integers aa and bb. To ensure it is the greatest divisor, you can also substitute backwards using the Extended Euclidean Algorithm to express the GCD as a linear combination of aa and bb.

Common errors

A frequent error is substituting the quotient instead of the remainder into the next step. Another standard mistake is concluding the algorithm one step early or outputting zero as the GCD; the GCD is always the last non-zero remainder, never zero.

Worked example

Find the GCD of 252 and 105 using the Euclidean algorithm.

Let a=252a = 252 and b=105b = 105. Step 1: Divide 252 by 105. 252=105(2)+42252 = 105(2) + 42. The remainder is 42. Step 2: Replace aa with 105 and bb with 42. Divide 105 by 42. 105=42(2)+21105 = 42(2) + 21. The remainder is 21. Step 3: Replace aa with 42 and bb with 21. Divide 42 by 21. 42=21(2)+042 = 21(2) + 0. The remainder is 0, so the algorithm terminates. The last non-zero remainder is 21. Therefore, GCD(252,105)=21GCD(252, 105) = 21.

FAQ

Run your own problem

References: Discrete Mathematics and Its Applications by Kenneth H. Rosen · OpenStax Contemporary Mathematics, Number Theory

See also