Factoring two large numbers into primes can be slow. Euclid (in the Elements, Book VII) describes a method for the GCD that requires no factorisation at all: only divisions with remainder. It is one of the oldest algorithms still in use.

Method — Euclid's algorithm (successive divisions)

To compute MCD(a,b)\text{MCD}(a,b) with ab>0a\ge b>0:

  1. divide aa by bb: a=qb+ra = q\cdot b + r, with 0r<b0\le r < b;
  2. if r=0r=0, the GCD is bb;
  3. otherwise repeat with bb and rr in place of aa and bb.

The GCD is the last non-zero remainder (that is, the last divisor).

The idea is that MCD(a,b)=MCD(b,r)\text{MCD}(a,b) = \text{MCD}(b,r): every common divisor of aa and bb also divides r=aqbr = a - q b, and vice versa. Replacing the pair with a “smaller” one leaves the common divisors unchanged, but the numbers shrink until a remainder is zero.

Example: GCD(48, 32)

48=132+16,32=216+0.48 = 1\cdot 32 + 16, \qquad 32 = 2\cdot 16 + 0.

The last non-zero remainder is 1616: hence MCD(48,32)=16\text{MCD}(48,32)=16 — the same value found with the factorisations.

Example: GCD(1071, 462)

1071=2462+147,462=3147+21,147=721+0.1071 = 2\cdot 462 + 147, \quad 462 = 3\cdot 147 + 21, \quad 147 = 7\cdot 21 + 0.

Last non-zero remainder: 2121. Therefore MCD(1071,462)=21\text{MCD}(1071,462)=21. Trying to factor 10711071 and 462462 into primes would have been much more laborious.

The "subtraction" version of Euclid's algorithm

In its original form Euclid did not use division but repeated subtractions: you subtract the smaller from the larger until the two become equal, and that value is the GCD. Division with remainder is simply “many subtractions in one go”.

Topics: Numeri e operazioni
Concepts: Massimo comun divisore (MCD)
Skills: Scomporre
People: Euclide