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 with :
- divide by : , with ;
- if , the GCD is ;
- otherwise repeat with and in place of and .
The GCD is the last non-zero remainder (that is, the last divisor).
The idea is that : every common divisor of and also divides , 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)
The last non-zero remainder is : hence — the same value found with the factorisations.
Example: GCD(1071, 462)
Last non-zero remainder: . Therefore . Trying to factor and 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”.
Links
Topics: Numeri e operazioni
Concepts: Massimo comun divisore (MCD)
Skills: Scomporre
People: Euclide