Lehmer's GCD algorithm
   HOME

TheInfoList



OR:

Lehmer's GCD algorithm, named after Derrick Henry Lehmer, is a fast GCD
algorithm In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
, an improvement on the simpler but slower
Euclidean algorithm In mathematics, the Euclidean algorithm,Some widely used textbooks, such as I. N. Herstein's ''Topics in Algebra'' and Serge Lang's ''Algebra'', use the term "Euclidean algorithm" to refer to Euclidean division or Euclid's algorithm, is a ...
. It is mainly used for big integers that have a representation as a string of digits relative to some chosen numeral system base, say ''β'' = 1000 or ''β'' = 232.


Algorithm

Lehmer noted that most of the
quotient In arithmetic, a quotient (from 'how many times', pronounced ) is a quantity produced by the division of two numbers. The quotient has widespread use throughout mathematics. It has two definitions: either the integer part of a division (in th ...
s from each step of the division part of the standard algorithm are small. (For example, Knuth observed that the quotients 1, 2, and 3 comprise 67.7% of all quotients. Knuth, ''
The Art of Computer Programming ''The Art of Computer Programming'' (''TAOCP'') is a comprehensive multi-volume monograph written by the computer scientist Donald Knuth presenting programming algorithms and their analysis. it consists of published volumes 1, 2, 3, 4A, and 4 ...
vol 2 "Seminumerical algorithms"'', chapter 4.5.3 Theorem E.
) Those small quotients can be identified from only a few leading digits. Thus the algorithm starts by splitting off those leading digits and computing the sequence of quotients as long as it is correct. Say we want to obtain the GCD of the two integers ''a'' and ''b''. Let ''a'' ≥ ''b''. * If ''b'' contains only one digit (in the chosen base, say ''β'' = 1000 or ''β'' = 232), use some other method, such as the
Euclidean algorithm In mathematics, the Euclidean algorithm,Some widely used textbooks, such as I. N. Herstein's ''Topics in Algebra'' and Serge Lang's ''Algebra'', use the term "Euclidean algorithm" to refer to Euclidean division or Euclid's algorithm, is a ...
, to obtain the result. * If ''a'' and ''b'' differ in the length of digits, perform a division so that ''a'' and ''b'' are equal in length, with length equal to ''m''. * ''Outer loop:'' Iterate until one of ''a'' or ''b'' is zero: ** Decrease ''m'' by one. Let ''x'' be the leading (most significant) digit in ''a'', ''x'' = ''a'' div ''β'' ''m'' and ''y'' the leading digit in ''b'', ''y'' = ''b'' div ''β'' ''m''. ** Initialize a 2 by 3
matrix Matrix (: matrices or matrixes) or MATRIX may refer to: Science and mathematics * Matrix (mathematics), a rectangular array of numbers, symbols or expressions * Matrix (logic), part of a formula in prenex normal form * Matrix (biology), the m ...
*::\textstyle \begin A & B & x\\ C & D & y \end to an extended
identity matrix In linear algebra, the identity matrix of size n is the n\times n square matrix with ones on the main diagonal and zeros elsewhere. It has unique properties, for example when the identity matrix represents a geometric transformation, the obje ...
\textstyle \begin 1 & 0 & x \\ 0 & 1 & y\end, *:and perform the euclidean algorithm simultaneously on the pairs (''x'' + ''A'', ''y'' + ''C'') and (''x'' + ''B'', ''y'' + ''D''), until the quotients differ. That is, iterate as an ''inner loop'': *:* Compute the quotients ''w''1 of the long divisions of (''x'' + ''A'') by (''y'' + ''C'') and ''w''2 of (''x'' + ''B'') by (''y'' + ''D'') respectively. Also let ''w'' be the (not computed) quotient from the current long division in the chain of long divisions of the euclidean algorithm. *** If ''w''1 ≠ ''w''2, then break out of the inner iteration. Else set ''w'' to ''w''1 (or ''w''2). *** Replace the current matrix **::\textstyle \begin A & B & x \\ C & D & y \end **: with the matrix product **:: \textstyle \begin 0 & 1 \\ 1 & -w \end \cdot \begin A & B & x \\ C & D & y \end = \begin C & D &y \\ A - wC & B - wD & x-wy \end **:according to the matrix formulation of the extended euclidean algorithm. ***If ''B'' ≠ 0, go to the start of the inner loop. ** If ''B'' = 0, we have reached a ''deadlock''; perform a normal step of the euclidean algorithm with ''a'' and ''b'', and restart the outer loop. ** Set ''a'' to ''aA'' + ''bB'' and ''b'' to ''Ca'' + ''Db'' (again simultaneously). This applies the steps of the euclidean algorithm that were performed on the leading digits in compressed form to the long integers ''a'' and ''b''. If ''b'' ≠ 0 go to the start of the outer loop.


References

* Kapil Paranjape
Lehmer's Algorithm
{{DEFAULTSORT:Lehmer's Gcd Algorithm Number theoretic algorithms