{"id":277,"date":"2026-11-13T12:36:42","date_gmt":"2026-11-13T04:36:42","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-euclidean-algorithm-divides-out-a-gcd\/"},"modified":"2026-11-13T21:54:16","modified_gmt":"2026-11-13T13:54:16","slug":"how-the-euclidean-algorithm-divides-out-a-gcd","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-euclidean-algorithm-divides-out-a-gcd\/","title":{"rendered":"How the Euclidean algorithm divides out a gcd"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>Euclidean algorithm<\/strong> computes gcd(a, b) by replacing the pair with (b, a mod b) until the remainder is 0. The other number is the gcd. It runs in <strong>O(log min(a, b))<\/strong> division steps, and the worst case is a pair of consecutive Fibonacci numbers, which makes every quotient 1. The same loop with the quotients kept is the extended Euclidean algorithm, which writes the gcd as an integer combination of a and b.<\/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\">Any common divisor of a and b divides a mod b, and any common divisor of b and a mod b divides a. The sets of common divisors are therefore equal, and the greatest one is preserved by the replacement. That identity is the algorithm. Subtracting b from a repeatedly is the same idea and is exponential in the bit length when a is much larger than b. The modulo jumps straight to the remainder.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The number of steps is logarithmic because the remainder is strictly smaller than the divisor, and in fact the larger number at least halves every two steps. Fibonacci inputs meet the upper bound up to a constant.<\/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 algorithm returns a positive gcd for any integers that are not both zero, after taking absolute values. gcd(a, 0) is |a|. Time is O(log min(|a|, |b|)) arithmetic operations on integers the size of the inputs. With schoolbook division the bit complexity is polynomial. Extra memory is a few integers. The gcd is unique as a positive integer. The algorithm does not return the coefficients. The extended version does.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A binary GCD replaces division by subtractions and shifts and is often faster in practice. It computes the same number. It is not required for the logarithmic bound of the division form.<\/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>Replace a and b by their absolute values. If b is greater than a, the first modulo swaps the roles.<\/li>\n<li>While b is not 0, set a, b = b, a mod b.<\/li>\n<li>When b is 0, a is the gcd.<\/li>\n<li>To see the Fibonacci worst case, note that gcd(F[k+1], F[k]) walks down to gcd(F[2], F[1]) and takes k\u22121 divisions.<\/li>\n<li>If the coefficients are required, keep the quotients and run the extended algorithm instead of discarding them.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The logarithmic bound counts division steps, not the cost of multiplying the inputs by anything else. The modular inverse, when the gcd is 1, comes from the extended algorithm. A sieve computes many gcds more cheaply in bulk and is the wrong tool for one pair. The positive sign is a convention. The last nonzero remainder is the gcd up to sign if the absolute values were skipped.<\/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 subtraction replaces the modulo on large inputs<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Subtract the smaller from the larger until the order flips<\/td>\n<td>The result is correct and a pair such as (10^12, 1) performs 10^12 subtractions. The modulo computes that remainder in one step.<\/td>\n<\/tr>\n<tr>\n<td>Return the last nonzero remainder without an absolute value and call it the positive gcd<\/td>\n<td>A negative input can leave a negative remainder under some division conventions. Take absolute values first, or abs the result.<\/td>\n<\/tr>\n<tr>\n<td>Stop when the remainder is 1 and skip the rest<\/td>\n<td>Stopping at 1 is correct, because the gcd is 1. Stopping at any other guessed threshold is not.<\/td>\n<\/tr>\n<tr>\n<td>Use the quotients and throw them away when a linear combination was required<\/td>\n<td>The plain loop has discarded the information the extended algorithm stores. Run that algorithm when coefficients are part of the answer.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Integer coefficients for the gcd are the extended Euclidean algorithm. Modular inverses are that algorithm when the gcd is 1. Bulk enumeration of primes is a sieve. The Euclidean algorithm is the single-pair gcd.<\/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 replace the pair by the remainder until the remainder is zero. Both print <code>6<\/code>.<\/p>\n\n<pre><code class=\"language-bash\">a=48\nb=18\nwhile (( b != 0 )); do\n  t=$((a % b))\n  a=$b\n  b=$t\ndone\necho &quot;$a&quot;\n<\/code><\/pre>\n<pre><code class=\"language-python\">def gcd(a, b):\n    while b:\n        a, b = b, a % b\n    return a\n\nassert gcd(48, 18) == 6\nprint(gcd(48, 18))\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 Euclidean algorithm is the gcd in every big-integer library, in fraction reduction, and in the setup of cryptographic keys. Python <code>math.gcd<\/code> is that loop. Binary GCD replaces the division with shifts and computes the same number.<\/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 hand-rolled subtraction. Use the modulo loop, or a language gcd that already does. Keep the extended form when the caller needs the B\u00e9zout coefficients and not only the gcd.<\/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 Euclidean algorithm returns the modulo reduction and the Fibonacci worst case. <strong>The Euclidean algorithm computes the gcd by repeated remainder, in a logarithmic number of divisions, and consecutive Fibonacci numbers are the worst case.<\/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 Euclidean algorithm computes gcd(a, b) by replacing the pair with (b, a mod b) until the remainder is 0. The other number is the gcd. It runs in O(log min(a, b)) division steps, and the worst case is a pair of consecutive Fibonacci numbers, which makes&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-277","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\/277","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=277"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/277\/revisions"}],"predecessor-version":[{"id":676,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/277\/revisions\/676"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}