{"id":278,"date":"2026-11-13T12:36:44","date_gmt":"2026-11-13T04:36:44","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-extended-euclidean-algorithm-writes-bezout\/"},"modified":"2026-11-13T21:54:18","modified_gmt":"2026-11-13T13:54:18","slug":"how-the-extended-euclidean-algorithm-writes-bezout","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-extended-euclidean-algorithm-writes-bezout\/","title":{"rendered":"How the extended Euclidean algorithm writes Bezout"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>extended Euclidean algorithm<\/strong> returns integers x and y such that <strong>a\u00b7x + b\u00b7y = gcd(a, b)<\/strong>. It runs the same divisions as the Euclidean algorithm and also updates two coefficient rows, one for each remainder. The modular inverse of a modulo m exists exactly when the gcd is 1, and it is the coefficient of a, reduced modulo m. The loop is <strong>O(log min(|a|, |b|))<\/strong> divisions.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The plain gcd throws away the quotients. Each remainder is an explicit integer combination of the previous two remainders, and those are combinations of the original a and b. Carrying the combinations along is enough. No extra search is required.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The inverse is the special case. If gcd(a, m) = 1, then a\u00b7x + m\u00b7y = 1, so a\u00b7x \u2261 1 (mod m). If the gcd is d &gt; 1, no inverse exists, and the same run proves it by returning d. A caller who takes x anyway and reduces modulo m has not built an inverse.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The returned x and y satisfy the B\u00e9zout identity for the inputs actually passed in, including signs. Time matches the Euclidean algorithm. Extra memory is a constant number of integers. The coefficients are not unique: x + b\/d and y \u2212 a\/d describe another solution. The modular inverse is x mod m, taken in the positive residue range, and only when d = 1.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Recursive code that returns the triple (g, x, y) from (b, a mod b) and then sets x&#8217; = y, y&#8217; = x \u2212 quotient\u00b7y is the shortest correct form. Iterative code must update both coefficient sequences in the same order as the remainders. Updating only one of them is the usual bug.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>If b is 0, return (|a|, sign(a), 0), or the signed convention the caller documents. The rest of the steps assume the recursive form.<\/li>\n<li>Recurse on (b, a mod b) to obtain (g, x1, y1) with b\u00b7x1 + (a mod b)\u00b7y1 = g.<\/li>\n<li>The quotient q is floor(a \/ b) under the same division that produced the remainder. Set x = y1 and y = x1 \u2212 q\u00b7y1.<\/li>\n<li>Return (g, x, y). Then a\u00b7x + b\u00b7y = g.<\/li>\n<li>For an inverse of a modulo m, run the algorithm on (a, m). If g is not 1, report that no inverse exists. Otherwise return x mod m in the range 0..m\u22121.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The identity uses the same a and b that were divided. Mixing a modular reduction of a into the coefficient update without reducing the equation produces a combination of the wrong pair. Batch inverses of 1..n modulo a prime can be computed more cheaply than n separate runs. One inverse is this algorithm. RSA and Diffie-Hellman call it for the private exponent or for a field inverse, and they do not change the loop.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when only one coefficient is updated<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Return x from the gcd loop and set y = 1<\/td>\n<td>The second coefficient is determined by the quotients. A constant 1 satisfies the identity only for special inputs.<\/td>\n<\/tr>\n<tr>\n<td>Reduce x modulo m when the gcd is not 1<\/td>\n<td>a\u00b7x is a multiple of the gcd, not of 1, in the residues. The inverse does not exist.<\/td>\n<\/tr>\n<tr>\n<td>Use a quotient from floor division and a remainder from a language operator that disagrees on negatives<\/td>\n<td>The identity q\u00b7b + r = a has to be exact for the q that the coefficient update uses. Mismatched division conventions break y.<\/td>\n<\/tr>\n<tr>\n<td>Invert a modulo m by exponentiation when m is composite<\/td>\n<td>Euler&#8217;s theorem needs \u03c6(m) and a coprime a. The extended algorithm does not need \u03c6 and also reports the non-coprime case. Exponentiation on a composite modulus is a different, stricter route.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">The gcd alone is the plain Euclidean algorithm. A sieve builds primes and can support bulk inverses. The extended Euclidean algorithm is the B\u00e9zout coefficient loop and the standard modular inverse.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The Bash script and the Python script run the extended Euclidean algorithm. Both print <code>6 -1 3<\/code>, and 48<em>(-1)+18<\/em>3 equals 6.<\/p>\n\n<pre><code class=\"language-bash\"># Iterative Bezout. r1*s2 style on (old, cur).\na=48\nb=18\ns0=1; s1=0\nt0=0; t1=1\nwhile (( b != 0 )); do\n  q=$((a \/ b))\n  r=$((a % b))\n  a=$b; b=$r\n  ns=$((s0 - q * s1)); s0=$s1; s1=$ns\n  nt=$((t0 - q * t1)); t0=$t1; t1=$nt\ndone\necho &quot;$a $s0 $t0&quot;\n<\/code><\/pre>\n<pre><code class=\"language-python\">def egcd(a, b):\n    if b == 0:\n        return a, 1, 0\n    g, x1, y1 = egcd(b, a % b)\n    return g, y1, x1 - (a \/\/ b) * y1\n\ng, x, y = egcd(48, 18)\nassert g == 6 and 48 * x + 18 * y == g\nprint(g, x, y)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The extended Euclidean algorithm produces the modular inverse: Python <code>pow(a, -1, m)<\/code> and the inverse inside RSA key generation. B\u00e9zout&#8217;s coefficients are the extra output. Fermat&#8217;s little theorem also inverts, and only when the modulus is prime.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop ad hoc coefficient guesses. Run the recurrence. Keep the algorithm whenever an inverse modulo m is required and the caller must also learn that the inverse does not exist when the gcd is greater than 1.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for the extended Euclidean algorithm returns the coefficient update x = y1, y = x1 \u2212 q\u00b7y1 and the inverse test that the gcd is 1. <strong>The extended Euclidean algorithm writes the gcd as a\u00b7x + b\u00b7y and, when the gcd is 1, returns the modular inverse of a.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: The extended Euclidean algorithm returns integers x and y such that a\u00b7x + b\u00b7y = gcd(a, b). It runs the same divisions as the Euclidean algorithm and also updates two coefficient rows, one for each remainder. The modular inverse of a modulo m exists exactly when the&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,42],"tags":[],"class_list":["post-278","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-number-theory"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=278"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/278\/revisions"}],"predecessor-version":[{"id":677,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/278\/revisions\/677"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}