Cut the knot: learn to enjoy mathematics
A math books store at a unique math study site. Learn to enjoy mathematics.
Google
Web CTK
Best sites for teachers
Sites for teachers
Sites for parents
Terms of use
Awards

Interactive Activities
CTK Exchange
CTK Insights - a blog

Games & Puzzles
What Is What
Arithmetic/Algebra
Geometry
Probability
Outline Mathematics
Make an Identity
Book Reviews
Eye Opener
Analog Gadgets
Inventor's Paradox
Did you know?...
Proofs
Math as Language
Things Impossible
Visual Illusions
My Logo
Math Poll
Cut The Knot!
MSET99 Talk
Other Math sites
Front Page
Movie shortcuts
Personal info
Reciprocal links
Privacy Policy

Guest book
News sites

Recommend this site

Best sites for teachers
Sites for teachers
Sites for parents

Education & Parenting

Manifesto: what CTK is about Search CTK Buying a book is a commitment to learning Table of content Things you can find on CTK Chronology of updates Email to Cut The Knot Recommend this page

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

  1. gcd(KN, KM) = K gcd(N, M)
  2. If gcd(N,M) = 1 then gcd(N,MK) = gcd(N,K)
  3. 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

  1. If N and M are even, gcd(N, M) = 2 gcd(N/2, M/2),
  2. If N is even while M is odd, then gcd(N, M) = gcd(N/2, M),
  3. If both N and M are odd, then (since N-M is even) |N-M| < max(N,M). Replace the largest of the two with |N-M|.

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 binary algorithm was discovered by R.Silver and J.Tersian in 1962 and has been published by G.Stein in 1967. Convergence of the algorithm, if not obvious, can be shown by induction. Property 3. above assures that induction is applicable.

Using our example:

 
NMExplanationFactor to
Remember
2322654right shift N and M2
1161327N - M
834327right shift N
417327N - M
90327right shift N
45327M - N
45282right shift M
45141M - N
4596right shift M
4548right shift M
NMExplanationFactor to
Remember
4524right shift M
4512right shift M
456right shift M
453N - M
423right shift N
213N - M
183right shift N
93N - M
63right shift N
33N - M
03

The last non-zero 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

  1. D.Knuth, Seminumerical Algorithms, Addison-Wesley, 1969.

Copyright © 1996-2008 Alexander Bogomolny

28739333Page copy protected against web site content infringement by Copyscape


Search:
Keywords:


Latest on CTK Exchange
Math
Posted by Laura
2 messages
06:56 AM, Apr-15-08

Divisibility rules - Jargon buste ...
Posted by Carolyn
2 messages
08:35 AM, Apr-04-08

drawing puzzle
Posted by martin gran
31 messages
06:53 PM, May-09-08

conway's game of life
Posted by frequency
0 messages
11:52 PM, May-12-08

Mistake on the page (an aside, Be ...
Posted by Max
4 messages
10:28 AM, Feb-28-08

Deriving functions based on diffe ...
Posted by ke_45
1 messages
12:47 PM, May-10-08

Josephus Flavius (correction)
Posted by David Turner
1 messages
09:42 AM, May-14-08