KEVOS
ArticlesServicesCase studiesAboutContact
ArticlesServicesCase studiesAboutContact
← ArticlesEuclid's Algorithm for Integer GCDEngineering · Engineering MathematicsLesson 554/883← PrevNext →
GuidePublished 7 Aug 2026Updated 13 Aug 20268 min readBy Kevin Jogin
On this page

Ask about this page

KEVOS AIEuclid's Algorithm for Integer GCD

KEVOS knowledge first · trusted web sources when needed

Engineering  /  Mathematics  — Integer Algorithms

Euclid's Algorithm for Integer GCD

Euclid's algorithm for greatest common divisors, its correctness, and the Fibonacci worst case that bounds its iteration count.

Page KV-MATH-0326Reading time 3 minReviewed 2026-08-07Author Kevin Jogin

Executive summary

Euclid's algorithm computes the gcd by repeatedly replacing the larger argument with its remainder modulo the smaller. It is among the oldest algorithms still in use and remains the method of choice.

Its efficiency is not obvious from the statement. The worst case is attained by consecutive Fibonacci numbers, and the iteration count is logarithmic in the operands.

Learning objectives

  1. State the algorithm and prove correctness from the gcd shift identity.
  2. Bound the number of iterations.
  3. Identify the Fibonacci worst case.

01The algorithm and its correctness

Algorithm

Euclidean algorithm

Inputnon-negative integers a, b
Outputgcd(a, b)
  1. Given a ≥ b ≥ 0.
  2. While b ≠ 0:
  3.   Compute r = a mod b.
  4.   Set a = b, b = r.
  5. Return a.
Cost  O(len(a) · len(b)) bit operations
Theorem

Correctness

gcd(a, b) = gcd(b, a mod b).

Reason. Any common divisor of a and b divides a − qb = a mod b, and any common divisor of b and a mod b divides qb + (a mod b) = a. The two pairs have identical common divisors, hence the same greatest one.

Termination is immediate: the second argument strictly decreases and stays non-negative, so the loop cannot run forever. The remaining question is how fast it decreases.

02The iteration bound

Theorem

Lamé's theorem

The number of division steps is at most about 4.8 · log₁₀(min(a,b)), and the worst case occurs when the inputs are consecutive Fibonacci numbers.

The mechanism is that each pair of consecutive steps at least halves the larger argument. If r = a mod b then either r < b/2 directly, or r ≥ b/2, in which case the next remainder is b mod r = b − r < b/2. Either way the value halves within two steps, giving a logarithmic bound.

Note
The Fibonacci worst case is a satisfying piece of structure: gcd(F_{n+1}, F_n) takes exactly n steps, because each division has quotient 1 and the recurrence is precisely the algorithm running backwards. Consecutive Fibonacci numbers are the slowest inputs of their size.

03Variants

Euclidean algorithm variants
VariantIdeaWhen preferable
ClassicalDivision with remainder each stepDefault; fewest iterations
Binary gcdRemove factors of 2, subtract, halveNo division needed; good where division is costly
LehmerOperate on leading digits to batch several stepsLarge multiprecision operands
ExtendedTrack Bezout coefficients alongsideWhen the coefficients are needed

The binary variant replaces division with subtraction and shifting, which on some architectures is faster despite performing more iterations. Lehmer's variant is the standard choice for very large operands, batching many small-quotient steps using single-word arithmetic on the leading digits.

04Frequently asked questions

Why is the bit complexity quadratic rather than logarithmic?

Because there are O(len) iterations and each performs a division costing O(len²) in the worst case, but the total is O(len²) rather than O(len³) — the operands shrink, and summing the actual per-step costs telescopes. The careful analysis gives O(len(a) · len(b)).

Does the algorithm work for negative inputs?

After taking absolute values, yes. The gcd is defined to be non-negative, so the standard approach normalises signs on entry.

Is there anything asymptotically faster?

Yes. A divide-and-conquer variant based on the same idea as Lehmer's achieves essentially the cost of multiplication times a logarithmic factor, and is used in libraries for very large inputs. It is considerably more intricate.

Related pages

  • Ideals and Greatest Common Divisors of Integers
  • Faster Integer Arithmetic: Karatsuba and Beyond
  • The Extended Euclidean Algorithm

Sources and method

Structural reference: Victor Shoup, A Computational Introduction to Number Theory and Algebra, Version 1, Cambridge University Press, 2005 — book pages 55-58.

This page carries the durable method layer only: definitions, constructions, algorithms, complexity results and selection criteria, authored originally for KEVOS. No text is transcribed or paraphrased from the source, and no numeric tables or benchmark data are reproduced — these are routed to live authoritative sources instead.

Author: Kevin Jogin. Last reviewed 2026-08-07.

Handbook application: from concept to controlled practice

Purpose. This expanded section turns the original page into a practical handbook. It preserves the supplied material and adds a repeatable way to apply, check and review Euclid's Algorithm for Integer GCD. It does not replace a contract, legislation, a controlled standard, competent engineering judgement or specialist advice.

The operating aim is to turn a compact mathematical statement into a usable chain of definitions, claims, examples and checks. Read the original explanation first, then use the workflow and checks below to convert knowledge into evidence.

Treat Euclid's Algorithm for Integer GCD as a network of definitions and implications, not as a list of formulas. The working vocabulary on this page—algorithm, euclid's, correctness, iteration, integer—should be made explicit before any proof or computation begins. Record the ambient set or structure, the permitted operations and the equality or equivalence relation in use. A compact theorem often changes meaning when the base field, finiteness condition, commutativity assumption or direction of an action changes.

For a proof, write the hypotheses as a checklist and mark the line at which each one is used. For a computation, state the representation of the input, the arithmetic model, the termination condition and the output invariant. For a classification problem, distinguish existence from uniqueness and distinguish an object from its representation. These separations prevent a correct local calculation from being mistaken for the general result.

A useful worked example should be small enough to inspect completely but rich enough to exercise the main mechanism. Compute the result in two ways where practical: symbolically and by substitution, structurally and numerically, or directly and through a normal form. Then include one near-miss example in which a hypothesis fails. The contrast explains why the theorem is shaped as it is and gives the reader a diagnostic pattern for later problems.

Verification is part of the mathematics. Check domains and codomains, substitute proposed solutions, test identity and zero cases, compare dimensions or cardinalities, and confirm that maps respect the required operations. In numerical work, report precision, conditioning and a residual rather than digits alone. In algorithmic work, separate mathematical correctness from implementation complexity and resource limits.

Step-by-step operating method

  1. Fix the setting. State the objects, ambient structure, notation and assumptions before manipulating symbols.
  2. Separate claims. Distinguish definitions, hypotheses, conclusions, equivalent conditions and consequences.
  3. Choose a method. Select proof, construction, calculation or algorithm according to the question actually asked.
  4. Work a small case. Use the smallest non-trivial example to expose the mechanism and test edge behaviour.
  5. Verify independently. Substitute back, check invariants, test boundary cases or use an alternative derivation.

Worked-example protocol

Illustrative method—not a source theorem. Start with a small admissible input and list the definitions it must satisfy. Carry out each transformation on a separate line, citing the property that permits it. Preserve exact values until approximation is necessary. At the end, verify the output against the original definition and one invariant such as dimension, degree, determinant, order, norm or residual. Then alter one hypothesis and observe which step ceases to be valid. This protocol creates a reusable example without inventing a theorem-specific numerical answer.

StageRecordQuality check
InputObjects, domain, notation, assumptionsEvery symbol is defined
MethodPermitted operation or cited result at each stepAll hypotheses hold
OutputExact result and representationCorrect type, domain and form
VerificationSubstitution, invariant or alternative derivationIndependent agreement
Boundary testZero, identity, degenerate or failed hypothesisScope is understood

Common failure modes and recovery actions

1. Watch for

Using a theorem without checking every hypothesis.

Recovery: Return to the governing definition or requirement and restate the decision in one sentence.

2. Watch for

Treating a suggestive example as a proof of the general case.

Recovery: Separate evidence from assumption, assign an owner and set a date for validation.

3. Watch for

Changing notation or conventions part-way through an argument.

Recovery: Run a small counterexample, boundary test, pilot or independent check before proceeding.

4. Watch for

Hiding a division-by-zero, convergence, finiteness or commutativity assumption.

Recovery: Record the consequence, decision and rationale, then update the controlled baseline.

5. Watch for

Reporting a computed result without a residual, substitution or structural check.

Recovery: Escalate when the issue affects safety, compliance, acceptance, material value or an agreed tolerance.

Review checklist

  • Can every symbol be traced to a definition or prior result?
  • Which hypothesis does each major step use?
  • Does the method cover zero, identity, degenerate and boundary cases?
  • Can the conclusion be checked by a second representation or calculation?
  • Are mandatory requirements distinguished from recommendations and illustrative values?
  • Are sources, assumptions, units, dates and versions recorded closely enough to reproduce the decision?
  • Have safety, legal, ethical, stakeholder and operational consequences been considered at the appropriate level?
  • Is there a named owner and a trigger for review, escalation, change or retirement?

Questions for deeper application

What is the most important distinction a practitioner must preserve when applying Euclid's Algorithm for Integer GCD?

Answer with a fact or cited source where available. Where evidence is incomplete, record the assumption, consequence, responsible owner and next validation action.

Which assumption about algorithm would change the result most if it proved false?

Answer with a fact or cited source where available. Where evidence is incomplete, record the assumption, consequence, responsible owner and next validation action.

What evidence would allow an independent reviewer to reproduce or challenge the conclusion?

Answer with a fact or cited source where available. Where evidence is incomplete, record the assumption, consequence, responsible owner and next validation action.

Which boundary, exception or failure case has not yet been tested?

Answer with a fact or cited source where available. Where evidence is incomplete, record the assumption, consequence, responsible owner and next validation action.

What must be handed over, monitored or reviewed after the immediate work is complete?

Answer with a fact or cited source where available. Where evidence is incomplete, record the assumption, consequence, responsible owner and next validation action.

Authoritative references and use notes

The sources below were selected as institutional or primary guidance for the broader practice. They support the handbook method; they do not imply that every statement or clause in a source applies to every project. Confirm the current edition, jurisdiction, contract and application before treating any requirement as mandatory.

  • MIT OpenCourseWare — Number Theory I — Massachusetts Institute of Technology. Used for algebraic and analytic number theory. Accessed 2026-08-13.
  • MIT OpenCourseWare — Algebra I — Massachusetts Institute of Technology. Used for groups, vector spaces, linear transformations and linear groups. Accessed 2026-08-13.

Continue learning

Faster Integer Arithmetic: Karatsuba and BeyondGuide · Engineering MathematicsNEXT LESSON →The Extended Euclidean AlgorithmGuide · Engineering MathematicsModular Exponentiation by Repeated SquaringGuide · Engineering MathematicsModular Inverses and Chinese RemainderingGuide · Engineering Mathematics
KEVOS · Engineering, manufacturing and project improvement
ArticlesServicesCase studiesAboutContact
© 2026 KEVOS®