Lehmer's GCD algorithm

Lehmer's GCD algorithm, named after D. H. Lehmer, is a fast GCD algorithm for multiple-precision arithmetic, which improves on the simpler Euclidean algorithm by doing most operations using only the leading digits of the values. "Digit" here is not necessarily a decimal digit; the algorithm is often used with integers represented using a base β such as β = 1000 or β = 232.

Algorithm

Lehmer noted that most of the quotients 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.[1]) 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 ab.

  • If b contains only one digit (in the chosen base, say β = 1000 or β = 232), use some other method, such as the Euclidean algorithm, to obtain the result.
  • If a and b differ in the length of digits, reduce a modulo b and exchange them as in the standard Euclidean algorithm. Repeat until the two are of the same length 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
    to an extended identity matrix
    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 w1 of the long divisions of (x + A) by (y + C) and w2 of (x + B) by (y + D) respectively. If these are equal, they also equal w, the (not computed) quotient from the corresponding step of the ordinary Euclidean algorithm.
      • If w1w2, then break out of the inner iteration. Else set w to w1 (or w2).
      • Replace the current matrix
      with the matrix product
      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 which 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

  1. ^ Knuth, The Art of Computer Programming vol 2 "Seminumerical algorithms", chapter 4.5.3 Theorem E.

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.