Content deleted Content added
Quuxplusone (talk | contribs) m →Algorithm: m copyedits |
No edit summary |
||
(32 intermediate revisions by 20 users not shown) | |||
Line 1:
'''Lehmer's GCD algorithm''', named after [[Derrick Henry Lehmer]], is a fast [[greatest common divisor|GCD]] [[algorithm]], an improvement on the simpler but slower [[Euclidean algorithm]]. It is mainly used for big integers that have a representation as a string of digits relative to some chosen numeral system [[radix|base]], say ''β'' = 1000 or ''β'' = 2<sup>32</sup>.
== 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 ''β'' = 1000 or ''β'' = 2<sup>32</sup>), 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''.
*
** Decrease ''m'' by one. Let ''x'' be the leading (most significant) digit in ''a'', ''x'' = ''a'' div ''β''<sup> ''m''</sup> and ''y'' the leading digit in ''b'', ''y'' = ''b'' div ''β''<sup> ''m''</sup>.
** Initialize a 2 by 3 [[matrix (mathematics)|matrix]]
** Compute the quotients ''w<sub>1</sub>'' of (''x'' + ''A'')(''y'' + ''C'') and ''w<sub>2</sub>'' of (''x'' + ''B'')(''y'' + ''D'') respectively. Also let ''w'' be the quotient from ''a''/''b''.▼
*::<math>\textstyle
** If ''w<sub>1</sub>'' = ''w<sub>2</sub>'', set ''w'' to ''w<sub>1</sub>'' (or ''w<sub>2</sub>'').▼
\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},
** If ''B'' = 0, we have reached a ''deadlock''; perform a normal division with ''a'' and ''b'', and recompute the matrix. Otherwise, set ''a'' to ''aA'' + ''bB'' and ''b'' to ''Ca'' + ''Db'' (again simultaneously), and iterate.▼
</math>
*: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''<sub>1</sub>
▲*** If ''w''<sub>1</sub>
*** Replace the current matrix
**::<math>\textstyle \begin{bmatrix} A & B & x \\ C & D & y \end{bmatrix}</math>
**: with the matrix product
**:: <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>
**: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
** 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, [http://www.imsc.res.in/~kapil/crypto/notes/node11.html Lehmer's Algorithm]▼
▲* [[Kapil Hari Paranjape|Kapil Paranjape]], [http://www.imsc.res.in/~kapil/crypto/notes/node11.html Lehmer's Algorithm]
{{Number-theoretic algorithms}}
{{DEFAULTSORT:Lehmer's Gcd Algorithm}}
[[Category:Number theoretic algorithms]]
|