Lehmer's GCD Algorithm - 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.) 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, 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
    textstyle begin{bmatrix} A & B & x\ C & D & y end{bmatrix} to an extended identity matrix textstyle begin{bmatrix} 1 & 0 & x \ 0 & 1 & yend{bmatrix},
    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. Also let w be the (not computed) quotient from the current long division in the chain of long divisions of the 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
      textstyle begin{bmatrix} 0 & 1 \ 1 & -w end{bmatrix} cdot begin{bmatrix} A & B & x \ C & D & y end{bmatrix} = begin{bmatrix} C & D &y \ A - wC & B - wD & x-wy end{bmatrix}
      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.

Read more about this topic:  Lehmer's GCD Algorithm

Other articles related to "algorithm":

Algorithm - History: Development of The Notion of "algorithm" - History After 1950
... A number of efforts have been directed toward further refinement of the definition of "algorithm", and activity is on-going because of issues surrounding, in particular, foundations of ... For more, see Algorithm characterizations ...
Tomasulo Algorithm
... The Tomasulo algorithm is a hardware algorithm developed in 1967 by Robert Tomasulo from IBM ... This algorithm differs from scoreboarding in that it utilizes register renaming ... The Tomasulo algorithm also uses a common data bus (CDB) on which computed values are broadcast to all the reservation stations that may need it ...
Timeline Of Algorithms - 1960s
... Hoare 1962 - Ford–Fulkerson algorithm developed by L ... Fulkerson 1962 - Bresenham's line algorithm developed by Jack E ... Fedorenko 1965 - Cooley–Tukey algorithm rediscovered by James Cooley and John Tukey 1965 - Levenshtein distance developed by Vladimir Levenshtein 1965 - Cocke–Younger–Kasami (CYK) algorithm ...
Barcode Reader - New Algorithms For Barcode Decoding - Symbology Decoding Algorithm
... The Symbology Decoding Algorithm for barcode scanners is the first symbology-based algorithm for decoding ... in the signal, whereas the traditional algorithm relies on the maxima and minima ... The Symbology Decoding Algorithm for Bar Code Scanners exhibited high resilience to blur and noise when tested on 1D Universal Product Codes ...
Markov Chain Monte Carlo - Random Walk Algorithms
... are some random walk MCMC methods Metropolis–Hastings algorithm Generates a random walk using a proposal density and a method for rejecting proposed moves ... Metropolis A variation of the Metropolis–Hastings algorithm that allows multiple trials at each point ... This allows the algorithm to generally take larger steps at each iteration, which helps combat problems intrinsic to large dimensional problems ...