Binary Euclid's Algorithm
Euclid's algorithm is tersely expressed by the recursive formula
(1)  gcd(N,M) = gcd(M, N mod M), 
where (N mod M) is the remainder of division of N by M. We postulate gcd(N,0) = N in accordance with the end condition of Euclid's algorithm. Our example appears as
gcd(2322,654) = gcd(654,360) = gcd(360,294) = gcd(294,66) = gcd(66,30) = gcd(30,6) = gcd(6,0) = 6.
Other properties of gcd are expressed in such a similarly concise form
 gcd(KN, KM) = K gcd(N, M)
 If gcd(N,M) = 1 then gcd(N,MK) = gcd(N,K)
 gcd(N, M) = gcd(N  M, M)
There are many ways to prove these. For instance, the first and second follow from the Fundamental Theorem of Arithmetic; the second (in a more direct manner) is also a consequence of a generalization of Proposition VII.30 the third one follows from the basic properties of modular arithmetic and division. A binary algorithm for finding gcd(N,M) is based on the following
 If N and M are even, gcd(N, M) = 2 gcd(N/2, M/2),
 If N is even while M is odd, then gcd(N, M) = gcd(N/2, M),
 If both N and M are odd, then (since NM is even) NM < max(N,M). Replace the largest of the two with NM.
The algorithm is known as binary because, unlike the original one, it does not use general division of integers but only division by 2. Since in a computer numbers are represented in the Binary system anyway, you should not be surprised to learn that there is a special machine instruction that implements division by 2 in a highly efficient manner. This is known as the right shift wherein the rightmost bit is discharged, the remaining bits are shifted one place to the right and the leftmost bit is set to 0.
Another handy operation is a bitwise conjunction &. N & 1 is either 1 or 0 for any integer N. It's 1 iff N is odd. Bitwise conjunction is also implemented in hardware, i.e., as a machine instruction.
The
Using our example:


The last nonzero term is 3 which, when combined with the saved factor 2, gives gcd(2322,654) = 6.
The algorithm is a slight improvement over the original method. What's most remarkable about it is that it's the first such improvement of Euclid's algorithm suggested in more than 2000 years.
References
 D.Knuth, Seminumerical Algorithms, AddisonWesley, 1969.
 Modular Arithmetic
 Chinese Remainder Theorem
 Euclid's Algorithm
 Pick's Theorem
 Fermat's Little Theorem
 Wilson's Theorem
 Euler's Function
 Divisibility Criteria
 Examples
 Equivalence relations
 A real life story
Contact Front page Contents Algebra
Copyright © 19962018 Alexander Bogomolny
68843471