Content deleted Content added
m →Algorithm: punctuation correction |
eponym at the beginning |
||
Line 1:
'''Lehmer's GCD algorithm''', named after [[Derrick Henry Lehmer]], is a rather fast [[greatest common divisor|GCD]] [[algorithm]], an improvement on the simpler but slower [[Euclidean algorithm]].
== Algorithm ==
Say we want to obtain the GCD of the two integers ''a'' and ''b''. Let
* If ''b'' contains only one digit (in the chosen [[radix|base]], say <math>\beta=1000</math> or <math>\beta=2^{32}</math>), 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''.
Line 10:
** Decrease ''m'' by one. Let ''x'' be the leading (most significant) digit in ''a'', <math>x=a \text{ div } \beta^m</math> and ''y'' the leading digit in ''b'', <math>y=b \text{ div } \beta^m</math>.
** Initialize a 2 by 3 [[matrix (mathematics)|matrix]] <math>\textstyle \begin{bmatrix} A & B & x\\ C & D & y \end{bmatrix}</math> to an extended [[identity matrix]] <math>\textstyle \begin{bmatrix} 1 & 0 & x \\ 0 & 1 & y\end{bmatrix}</math>, and perform the euclidean algorithm simultaneously on the pairs <math>(x+A,y+C)</math> and <math>(x+B,y+D)</math>, until the quotients differ. That is, iterate:
*** Compute the quotients ''w<sub>1</sub>'' of the long divisions of (''x'' + ''A'') by (''y'' + ''C'') and ''w''<sub>2</sub>
*** If ''w''<sub>1</sub>
*** Set our current matrix <math>\textstyle \begin{bmatrix} A & B & x \\ C & D & y \end{bmatrix}</math> to <math>\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}</math>
*** If ''B'' = 0, we have reached a ''deadlock''; perform a normal step of the euclidean algorithm with ''a'' and ''b'', and recompute the matrix.
|