ArticleslgStudy

computer science

Lehmer's GCD algorithm

Lehmer's GCD algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Lehmer's GCD algorithm rather than just read about it. In short: Lehmer's GCD algorithm, named after D. H.

Key takeaways

  • Lehmer's GCD algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Lehmer's GCD algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Lehmer's GCD algorithm from memory before moving on to harder problems.

Reference excerpt

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.) 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, 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

[ A B x C D y ] {\displaystyle \textstyle {\begin{bmatrix}A&B&x\\C&D&y\end{bmatrix}}} to an extended identity matrix [ 1 0 x 0 1 y ] , {\displaystyle \textstyle {\begin{bmatrix}1&0&x\\0&1&y\end{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. If these are equal, they also equal w, the (not computed) quotient from the corresponding step of the ordinary Euclidean algorithm. If w1 ≠ w2, then break out of the inner iteration. Else set w to w1 (or w2). Replace the current matrix

[ A B x C D y ] {\displaystyle \textstyle {\begin{bmatrix}A&B&x\\C&D&y\end{bmatrix}}}

with the matrix product

[ 0 1 1 − w ] ⋅ [ A B x C D y ] = [ C D y A − w C B − w D x − w y ] {\displaystyle \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 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

Kapil Paranjape, Lehmer's Algorithm

Worked examples

Example 1 — a first encounter with Lehmer's GCD algorithm

Start with the simplest possible case. Write down what Lehmer's GCD algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Lehmer's GCD algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Lehmer's GCD algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Lehmer's GCD algorithm

In research
Lehmer's GCD algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Lehmer's GCD algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Lehmer's GCD algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Number theoretic algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Lehmer's GCD algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Lehmer's GCD algorithm” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Lehmer's GCD algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Lehmer's GCD algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Lehmer's GCD algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Lehmer's GCD algorithm in simple terms?

Lehmer's GCD algorithm, named after D. H.

Why does Lehmer's GCD algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Lehmer's GCD algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Lehmer's GCD algorithm.

Tags

  • Number theoretic algorithms

Keep exploring