{
  "id": "euclidean-algorithm",
  "title": "Euclidean Algorithm",
  "lang": "en",
  "source_lang": "en",
  "summary": "The Euclidean algorithm computes the greatest common divisor by repeatedly replacing a pair of numbers with a divisor and its remainder.",
  "category": "mathematics",
  "subcategory": "algebra",
  "type": "concept",
  "aliases": [
    "Euclid's algorithm",
    "Euclidean GCD algorithm"
  ],
  "generated_at": "2026-10-04",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/euclidean-algorithm/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/euclidean-algorithm/",
      "json": "https://aiwiki.page/zh/euclidean-algorithm.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "The Euclidean algorithm is an algorithm for computing the greatest common divisor (GCD) of two integers: the largest positive integer dividing both, provided they are not both zero. It repeatedly performs division with remainder until a remainder becomes zero; the last nonzero remainder is the answer. The method is fundamental in number theory and extends to polynomials and other algebraic structures. It computes the GCD without requiring the prime factorizations of its inputs. (cs.drexel.edu)"
    },
    {
      "heading": "Historical formulation",
      "text": "The algorithm is named after Euclid, whose Elements presents the procedure in Book VII. Proposition 1 concerns recognizing relatively prime numbers, while Proposition 2 finds the greatest common measure of two numbers that are not relatively prime. Proposition 3 extends the construction to three numbers. The ancient formulation uses repeated subtraction rather than modern division notation: the smaller quantity is repeatedly removed from the larger. Division with remainder groups multiple such subtractions into one operation. (mathcs.clarku.edu)"
    },
    {
      "heading": "Procedure and example",
      "text": "For positive integers \\(a\\geq b\\), Euclidean division gives uniquely determined integers \\(q\\) and \\(r\\) satisfying\n\n\\[a=qb+r,\\qquad 0\\leq r<b.\\]\n\nThe algorithm replaces \\((a,b)\\) with \\((b,r)\\) and repeats. When the second component becomes zero, the first component is the GCD. This rule can be implemented iteratively or through recursion. (cs.drexel.edu)\n\nFor example, applying the rule to 252 and 105 gives\n\n\\[\\begin{aligned} 252&=2\\cdot105+42,\\\\ 105&=2\\cdot42+21,\\\\ 42&=2\\cdot21+0. \\end{aligned}\\]\n\nThus \\(\\gcd(252,105)=21\\). The quotients are 2, 2, and 2; the last nonzero remainder is 21.\n\nA compact pseudocode formulation, for nonnegative inputs not both zero, is:\n\ngcd(a, b):\n    while b ≠ 0:\n        r ← a mod b\n        a ← b\n        b ← r\n    return a\n\nHere mod denotes the nonnegative remainder. The temporary variable preserves the old values during each update. The loop also handles \\(a<b\\): its first iteration exchanges the roles of the inputs. (mosullivan.sdsu.edu)"
    },
    {
      "heading": "Correctness and termination",
      "text": "The central identity is\n\n\\[\\gcd(a,b)=\\gcd(b,a-qb).\\]\n\nIts proof follows from divisibility. Every common divisor of \\(a\\) and \\(b\\) divides \\(a-qb\\). Conversely, every common divisor of \\(b\\) and \\(a-qb\\) divides their sum \\((a-qb)+qb=a\\). Consequently, the two pairs have exactly the same common divisors. (mosullivan.sdsu.edu)\n\nThe unchanged GCD is a loop invariant. Meanwhile, successive nonzero remainders form a strictly decreasing sequence of positive integers, so the process must terminate. At termination, the pair is \\((d,0)\\), whose greatest common divisor is \\(d\\). These two properties establish that the returned value is correct. (mosullivan.sdsu.edu)"
    },
    {
      "heading": "Extended algorithm and applications",
      "text": "The extended Euclidean algorithm additionally computes integers \\(x\\) and \\(y\\) satisfying Bézout's identity:\n\n\\[ax+by=\\gcd(a,b).\\]\n\nEach remainder is an integer linear combination of the original inputs. Tracking its coefficients during division, or substituting backward through the equations afterward, produces the required coefficients. (cs.drexel.edu)\n\nFor the example above,\n\n\\[21=105-2\\cdot42 =105-2(252-2\\cdot105) =-2\\cdot252+5\\cdot105.\\]\n\nThus \\(x=-2\\) and \\(y=5\\).\n\nThis extension solves linear Diophantine equations. For \\(a,b\\) not both zero, the equation \\(ax+by=c\\) has integer solutions exactly when \\(\\gcd(a,b)\\) divides \\(c\\). Multiplying Bézout coefficients by \\(c/\\gcd(a,b)\\) supplies one solution whenever that condition holds. (web.cs.miami.edu)\n\nIn modular arithmetic, if \\(\\gcd(a,m)=1\\), then \\(ax+my=1\\) implies \\(ax\\equiv1\\pmod m\\). Therefore, \\(x\\) represents the modular multiplicative inverse of \\(a\\). Such an inverse exists precisely when \\(a\\) and \\(m\\) are relatively prime. (mosullivan.sdsu.edu)"
    },
    {
      "heading": "Efficiency and continued fractions",
      "text": "The algorithm's computational complexity depends on whether one counts divisions or individual bit operations. For positive inputs, the number of divisions is \\(O(\\log\\min(a,b))\\), expressed using big-O notation. Consecutive Fibonacci numbers produce particularly long remainder chains relative to their size: successive divisions largely follow the Fibonacci recurrence backward. Inputs of at most \\(n\\) bits therefore require \\(O(n)\\) divisions. (sites.math.rutgers.edu)\n\nThis does not mean that the running time is linear in the bit length. Division of large integers is itself a multi-operation computation. A straightforward analysis assigning \\(O(n^2)\\) time to each division yields an \\(O(n^3)\\) upper bound, although this bound is not tight; more advanced GCD methods achieve substantially better bit complexity. (sites.math.rutgers.edu)\n\nThe successive quotients also give the finite continued fraction expansion of the rational number \\(a/b\\). For the example,\n\n\\[\\frac{252}{105} =2+\\frac{1}{2+\\frac12} =[2;2,2].\\]\n\nThus the same division sequence encodes both a common divisor and a continued-fraction representation. (sites.math.rutgers.edu)"
    },
    {
      "heading": "Algebraic generalization",
      "text": "For polynomials over a field, division has the form\n\n\\[f=qg+r, \\qquad r=0\\ \\text{or}\\ \\deg r<\\deg g.\\]\n\nReplacing \\((f,g)\\) with \\((g,r)\\) preserves common divisors, while decreasing degree ensures termination. The last nonzero remainder is a polynomial GCD, determined up to multiplication by a nonzero constant; it is commonly normalized to have leading coefficient 1. (singacom.uva.es)\n\nIn abstract algebra, a Euclidean domain is an integral domain admitting division with a remainder whose nonnegative integer-valued size decreases. This supplies the termination mechanism needed for the generalized algorithm. Polynomial rings over fields are examples, with degree serving as the size measure. (singacom.uva.es)"
    }
  ],
  "links": [
    "algorithm",
    "greatest-common-divisor",
    "integer",
    "number-theory",
    "euclid",
    "euclids-elements",
    "euclidean-division",
    "recursion",
    "pseudocode",
    "mathematical-proof",
    "loop-invariant",
    "extended-euclidean-algorithm",
    "bezouts-identity",
    "linear-combination",
    "linear-diophantine-equation",
    "modular-arithmetic",
    "modular-multiplicative-inverse",
    "computational-complexity",
    "big-o-notation",
    "fibonacci-sequence",
    "continued-fraction",
    "rational-number",
    "polynomial",
    "field-mathematics",
    "abstract-algebra"
  ],
  "sources": [
    {
      "title": "cs.drexel.edu/~johnsojr/2017-18/fall/cs303/lectures/euclid.html",
      "url": "https://www.cs.drexel.edu/~johnsojr/2017-18/fall/cs303/lectures/euclid.html"
    },
    {
      "title": "mathcs.clarku.edu/~djoyce/java/elements/bookVII/bookVII.html",
      "url": "https://mathcs.clarku.edu/~djoyce/java/elements/bookVII/bookVII.html"
    },
    {
      "title": "mosullivan.sdsu.edu/Teaching/Algebra11f/foundation.pdf",
      "url": "https://mosullivan.sdsu.edu/Teaching/Algebra11f/foundation.pdf"
    },
    {
      "title": "web.cs.miami.edu/home/burt/learning/Csc609.082/notes/gcd.html",
      "url": "https://web.cs.miami.edu/home/burt/learning/Csc609.082/notes/gcd.html"
    },
    {
      "title": "sites.math.rutgers.edu/~sk1233/courses/ANT-F14/lec1.pdf",
      "url": "https://sites.math.rutgers.edu/~sk1233/courses/ANT-F14/lec1.pdf"
    },
    {
      "title": "singacom.uva.es/~ruano/Algebra22013/Slides17Alg13.pdf",
      "url": "https://www.singacom.uva.es/~ruano/Algebra22013/Slides17Alg13.pdf"
    }
  ],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}