{"id":137,"date":"2026-02-15T03:18:29","date_gmt":"2026-02-15T03:18:29","guid":{"rendered":"https:\/\/modularmath.org\/?page_id=137"},"modified":"2026-02-15T03:18:29","modified_gmt":"2026-02-15T03:18:29","slug":"euclidean-algorithm","status":"publish","type":"page","link":"https:\/\/modularmath.org\/?page_id=137","title":{"rendered":"Euclidean Algorithm"},"content":{"rendered":"\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.5, user-scalable=yes\">\n    <title>modularmath \u00b7 LIVE \u00b7 euclidean &#038; inverses<\/title>\n    <style>\n        * {\n            margin: 0;\n            padding: 0;\n            box-sizing: border-box;\n        }\n\n        body {\n            background: linear-gradient(145deg, #d0def0 0%, #bccde4 100%);\n            font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;\n            padding: 1rem 0.75rem 2rem;\n            min-height: 100vh;\n            color: #0a1a30;\n        }\n\n        .module-card {\n            max-width: 1100px;\n            margin: 0 auto;\n            background: rgba(255, 255, 255, 0.9);\n            backdrop-filter: blur(10px);\n            border-radius: 2.5rem;\n            padding: 1.8rem 1.2rem 2.2rem;\n            box-shadow: 0 25px 45px -20px #102a44;\n            border: 1px solid #ffffffd0;\n        }\n\n        h1 {\n            font-size: clamp(2rem, 8vw, 2.8rem);\n            background: linear-gradient(145deg, #0f2b48, #284e7a);\n            -webkit-background-clip: text;\n            -webkit-text-fill-color: transparent;\n            background-clip: text;\n            margin-bottom: 0.5rem;\n            display: flex;\n            flex-wrap: wrap;\n            align-items: center;\n            gap: 0.5rem 1rem;\n        }\n\n        .badge {\n            background: #254e7a;\n            color: white;\n            font-size: 1rem;\n            padding: 0.3rem 1.4rem;\n            border-radius: 40px;\n            -webkit-text-fill-color: white;\n            box-shadow: 0 6px 10px #00000030;\n        }\n\n        .nav-links {\n            display: flex;\n            flex-wrap: wrap;\n            gap: 0.6rem 0.8rem;\n            margin: 1rem 0 2rem;\n            padding-bottom: 0.8rem;\n            border-bottom: 3px dashed #9bb1d1;\n        }\n\n        .nav-links a {\n            text-decoration: none;\n            font-weight: 550;\n            color: #1a3454;\n            background: #deecff;\n            padding: 0.5rem 1.2rem;\n            border-radius: 40px;\n            font-size: 0.9rem;\n            border: 1px solid white;\n            transition: 0.1s;\n        }\n\n        .nav-links a:hover {\n            background: #c6daf8;\n        }\n\n        .nav-links .home-link {\n            background: #1c3f6b;\n            color: white;\n        }\n\n        \/* interactive section cards *\/\n        .interactive-area {\n            background: #eef4fd;\n            border-radius: 2.2rem;\n            padding: 1.8rem 1.5rem;\n            margin: 2rem 0;\n            border-left: 12px solid #3d70b0;\n        }\n\n        .input-group {\n            display: flex;\n            flex-wrap: wrap;\n            align-items: center;\n            gap: 1rem;\n            margin: 1.5rem 0;\n        }\n\n        .input-group label {\n            font-weight: 600;\n            color: #1b3a60;\n            min-width: 80px;\n        }\n\n        .input-group input {\n            background: white;\n            border: 2px solid #b1c9e5;\n            border-radius: 60px;\n            padding: 0.8rem 1.5rem;\n            font-size: 1.2rem;\n            width: 140px;\n            font-family: 'Inter', monospace;\n            font-weight: 500;\n            transition: 0.15s;\n        }\n\n        .input-group input:focus {\n            border-color: #2b6190;\n            outline: none;\n            box-shadow: 0 0 0 3px #8db6e0;\n        }\n\n        button {\n            background: #2a4e7a;\n            border: none;\n            color: white;\n            font-weight: 600;\n            padding: 0.8rem 2rem;\n            border-radius: 60px;\n            font-size: 1.1rem;\n            cursor: pointer;\n            box-shadow: 0 6px 0 #0f2540, 0 6px 14px #00000030;\n            transition: 0.05s linear;\n            border: 1px solid #ffffff80;\n        }\n\n        button:active {\n            transform: translateY(5px);\n            box-shadow: 0 1px 0 #0f2540, 0 6px 14px #00000030;\n        }\n\n        .result-card {\n            background: #ffffffd9;\n            border-radius: 2rem;\n            padding: 1.2rem 2rem;\n            margin: 1.2rem 0;\n            font-size: 1.4rem;\n            font-weight: 500;\n            border: 2px solid #86a9d1;\n            word-break: break-word;\n        }\n\n        .step-display {\n            background: #152f4b;\n            color: white;\n            padding: 1.2rem 1.8rem;\n            border-radius: 2rem;\n            font-family: 'Courier New', monospace;\n            font-size: 1.2rem;\n            line-height: 1.8;\n            white-space: pre-wrap;\n        }\n\n        .inverse-highlight {\n            background: #ecd6b0;\n            border-radius: 2rem;\n            padding: 1.2rem 2rem;\n            font-size: 1.4rem;\n            border: 2px solid #b2802c;\n        }\n\n        table {\n            width: 100%;\n            border-collapse: collapse;\n            background: white;\n            border-radius: 1.5rem;\n            overflow: hidden;\n        }\n\n        th {\n            background: #1e4470;\n            color: white;\n            padding: 0.7rem;\n        }\n\n        td {\n            padding: 0.6rem;\n            text-align: center;\n            background: #f5faff;\n            border-bottom: 1px solid #c1d3ec;\n        }\n\n        footer {\n            margin-top: 2.5rem;\n            text-align: center;\n            border-top: 2px solid #b3c7e5;\n            padding-top: 1.5rem;\n        }\n    <\/style>\n<\/head>\n<body>\n<div class=\"module-card\">\n    <h1>modularmath <span class=\"badge\">\u26a1 LIVE: euclidean &#038; inverses<\/span><\/h1>\n\n    <div class=\"nav-links\">\n        <a href=\"#\">\ud83c\udfae games<\/a>\n        <a href=\"#\">\ud83d\udc41\ufe0f vis<\/a>\n        <a href=\"#\">\ud83e\udde9 residue fields<\/a>\n        <a href=\"#\" style=\"background: #f5d3b3;\">\u26a1 euclidean (live)<\/a>\n        <a href=\"#\" class=\"home-link\">\ud83c\udfe0 home<\/a>\n    <\/div>\n\n    <!-- INTRO: real interactive tool -->\n    <div class=\"interactive-area\">\n        <h2 style=\"margin-bottom: 1rem;\">\ud83e\uddee extended euclidean calculator<\/h2>\n        <p style=\"margin-bottom: 1rem;\">Enter two numbers (1\u2013999) to compute gcd, B\u00e9zout coefficients, and modular inverse if coprime.<\/p>\n        \n        <div class=\"input-group\">\n            <label>a =<\/label>\n            <input type=\"number\" id=\"numA\" value=\"30\" min=\"1\" max=\"999\" step=\"1\">\n        <\/div>\n        <div class=\"input-group\">\n            <label>b =<\/label>\n            <input type=\"number\" id=\"numB\" value=\"108\" min=\"1\" max=\"999\" step=\"1\">\n        <\/div>\n\n        <div style=\"display: flex; flex-wrap: wrap; gap: 1rem;\">\n            <button id=\"computeBtn\">\ud83d\ude80 compute gcd &#038; inverses<\/button>\n            <button id=\"exampleBtn\">\ud83c\udfb2 random example<\/button>\n        <\/div>\n\n        <!-- output area - dynamic -->\n        <div style=\"margin-top: 2rem;\" id=\"resultArea\">\n            <div class=\"result-card\">\ud83d\udcd0 gcd(30, 108) = <span id=\"gcdResult\">6<\/span><\/div>\n            <div class=\"step-display\" id=\"stepsDisplay\">\n                108 = 30\u00b73 + 18<br>\n                30 = 18\u00b71 + 12<br>\n                18 = 12\u00b71 + 6<br>\n                12 = 6\u00b72 + 0<br>\n                \u2190 back-substitution \u2192<br>\n                6 = 18 &#8211; 12\u00b71 = 18 &#8211; (30-18\u00b71) = 2\u00b718 &#8211; 30 = 2\u00b7(108-30\u00b73) &#8211; 30 = 2\u00b7108 &#8211; 7\u00b730\n            <\/div>\n            <div id=\"inverseDisplay\" class=\"inverse-highlight\" style=\"margin-top: 1rem;\">\n                \ud83d\udd01 30\u207b\u00b9 mod 108 ? <strong>not invertible<\/strong> (gcd \u2260 1)\n            <\/div>\n        <\/div>\n    <\/div>\n\n    <!-- second interactive: modular inverse solver (mod prime) -->\n    <h2>\ud83d\udd01 find modular inverse (mod prime example)<\/h2>\n    <div style=\"background: #e4defa; border-radius: 2rem; padding: 1.8rem;\">\n        <div class=\"input-group\">\n            <label>a mod p<\/label>\n            <input type=\"number\" id=\"invA\" value=\"7\" min=\"1\" max=\"998\" step=\"1\">\n            <span style=\"font-size:1.5rem; margin:0 0.5rem;\">mod<\/span>\n            <input type=\"number\" id=\"primeP\" value=\"23\" min=\"2\" max=\"997\" step=\"1\">\n        <\/div>\n        <button id=\"calcInverseBtn\">\ud83d\udd0d compute inverse<\/button>\n        <div class=\"result-card\" id=\"inverseResult\" style=\"margin-top: 1.4rem; min-height: 4rem;\">\n            \u23f3 7\u207b\u00b9 mod 23 = <strong>10<\/strong> (since 7\u00b710 = 70 \u2261 1 mod 23)\n        <\/div>\n        <p style=\"margin-top:1rem;\">\u26a0\ufe0f if modulus is not prime, inverse may not exist. Try a=7, p=23 (prime).<\/p>\n    <\/div>\n\n    <!-- interactive euclidean step builder (visual) -->\n    <h2>\ud83d\udcd0 build your own euclidean steps<\/h2>\n    <div style=\"display: flex; flex-wrap: wrap; gap: 2rem; margin: 2rem 0;\">\n        <div style=\"flex: 2 1 280px;\">\n            <div class=\"input-group\">\n                <label>a<\/label>\n                <input type=\"number\" id=\"stepA\" value=\"64\" step=\"1\" min=\"1\">\n            <\/div>\n            <div class=\"input-group\">\n                <label>b<\/label>\n                <input type=\"number\" id=\"stepB\" value=\"15\" step=\"1\" min=\"1\">\n            <\/div>\n            <button id=\"showStepsBtn\">show division steps<\/button>\n        <\/div>\n        <div style=\"flex: 3 1 300px; background: #ffffffc9; border-radius: 2rem; padding: 1.5rem;\" id=\"stepBox\">\n            <pre id=\"stepPre\" style=\"font-family: 'Courier New'; font-size: 1.1rem;\">64 = 15\u00b74 + 4\n15 = 4\u00b73 + 3\n4 = 3\u00b71 + 1\n3 = 1\u00b73 + 0\n\u2192 gcd = 1<\/pre>\n        <\/div>\n    <\/div>\n\n    <!-- table of small inverses (static but now live context) -->\n    <h2>\ud83d\udccb modular inverses mod 13 (test with above tools)<\/h2>\n    <div style=\"overflow-x: auto;\">\n        <table>\n            <tr><th>a<\/th><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><td>5<\/td><td>6<\/td><td>7<\/td><td>8<\/td><td>9<\/td><td>10<\/td><td>11<\/td><td>12<\/td><\/tr>\n            <tr><th>a\u207b\u00b9<\/th><td>1<\/td><td>7<\/td><td>9<\/td><td>10<\/td><td>8<\/td><td>11<\/td><td>2<\/td><td>5<\/td><td>3<\/td><td>4<\/td><td>6<\/td><td>12<\/td><\/tr>\n        <\/table>\n    <\/div>\n    <p style=\"margin: 1rem 0;\">\u2728 Try a=5, p=13 in inverse calculator \u2192 should get 8.<\/p>\n\n    <!-- footer -->\n    <footer>\n        <p>\u26a1 every button does real computation using javascript. You are in control.<\/p>\n        <p>\ud83c\udfe0 <a href=\"#\" style=\"color:#1d3a5c;\">modularmath.org\/euclidean-live<\/a> \u2014 new interactive category<\/p>\n    <\/footer>\n<\/div>\n\n<script>\n    (function() {\n        \/\/ ----- HELPER: extended euclidean (returns [gcd, x, y])  such that a*x + b*y = gcd\n        function extendedGcd(a, b) {\n            if (b === 0) return [a, 1, 0];\n            let [gcd, x1, y1] = extendedGcd(b, a % b);\n            let x = y1;\n            let y = x1 - Math.floor(a \/ b) * y1;\n            return [gcd, x, y];\n        }\n\n        \/\/ format steps as string (simple version)\n        function formatEuclidSteps(a, b) {\n            let steps = [];\n            let A = a, B = b;\n            while (B !== 0) {\n                let q = Math.floor(A \/ B);\n                let r = A % B;\n                steps.push(`${A} = ${B}\u00b7${q} + ${r}`);\n                A = B;\n                B = r;\n            }\n            return steps.join('\\n');\n        }\n\n        \/\/ main compute\n        const numA = document.getElementById('numA');\n        const numB = document.getElementById('numB');\n        const gcdSpan = document.getElementById('gcdResult');\n        const stepsDiv = document.getElementById('stepsDisplay');\n        const inverseDisplay = document.getElementById('inverseDisplay');\n\n        function updateMain() {\n            let a = parseInt(numA.value) || 30;\n            let b = parseInt(numB.value) || 108;\n            if (a <= 0) a = 1;\n            if (b <= 0) b = 1;\n            numA.value = a;\n            numB.value = b;\n\n            \/\/ compute gcd &#038; coefficients\n            let [gcd, x, y] = extendedGcd(a, b);\n            gcdSpan.innerText = gcd;\n\n            \/\/ generate division steps\n            let steps = [];\n            let A = a, B = b;\n            let q, r;\n            while (B !== 0) {\n                q = Math.floor(A \/ B);\n                r = A % B;\n                steps.push(`${A} = ${B} \u00b7 ${q} + ${r}`);\n                A = B;\n                B = r;\n            }\n            steps.push(`\u2192 gcd = ${gcd}`);\n            \/\/ back-substitution short form\n            let bezout = `${a}\u00b7(${x}) + ${b}\u00b7(${y}) = ${gcd}`;\n            stepsDiv.innerHTML = steps.join('<br>') + '<br>\u2190 B\u00e9zout \u2192<br>' + bezout;\n\n            \/\/ inverse if gcd == 1\n            if (gcd === 1) {\n                let inv = (x % b + b) % b;  \/\/ positive inverse of a mod b\n                inverseDisplay.innerHTML = `\ud83d\udd01 ${a}\u207b\u00b9 mod ${b} = <strong>${inv}<\/strong> (since ${a}\u00b7${inv} \u2261 1 mod ${b})`;\n            } else {\n                inverseDisplay.innerHTML = `\ud83d\udd01 ${a}\u207b\u00b9 mod ${b} ? <strong>not invertible<\/strong> (gcd = ${gcd} \u2260 1)`;\n            }\n        }\n\n        document.getElementById('computeBtn').addEventListener('click', updateMain);\n\n        document.getElementById('exampleBtn').addEventListener('click', function() {\n            let randA = Math.floor(Math.random() * 98) + 2;  \/\/ 2..99\n            let randB = Math.floor(Math.random() * 98) + 2;\n            numA.value = randA;\n            numB.value = randB;\n            updateMain();\n        });\n\n        \/\/ modular inverse calculator (prime \/ any)\n        const invA = document.getElementById('invA');\n        const primeP = document.getElementById('primeP');\n        const invResult = document.getElementById('inverseResult');\n\n        document.getElementById('calcInverseBtn').addEventListener('click', function() {\n            let a = parseInt(invA.value) || 7;\n            let p = parseInt(primeP.value) || 23;\n            if (a <= 0) a = 1;\n            if (p <= 1) p = 2;\n            invA.value = a;\n            primeP.value = p;\n\n            let [gcd, x, y] = extendedGcd(a, p);\n            if (gcd === 1) {\n                let inv = (x % p + p) % p;\n                invResult.innerHTML = `\u2705 ${a}\u207b\u00b9 mod ${p} = <strong>${inv}<\/strong> (${a}\u00b7${inv} = ${a*inv}, \u2261 1 mod ${p})`;\n            } else {\n                invResult.innerHTML = `\u274c ${a} and ${p} are not coprime (gcd = ${gcd}). No inverse.`;\n            }\n        });\n\n        \/\/ step builder (division steps only)\n        const stepA = document.getElementById('stepA');\n        const stepB = document.getElementById('stepB');\n        const stepPre = document.getElementById('stepPre');\n\n        document.getElementById('showStepsBtn').addEventListener('click', function() {\n            let a = parseInt(stepA.value) || 64;\n            let b = parseInt(stepB.value) || 15;\n            if (a <= 0) a = 1;\n            if (b <= 0) b = 1;\n            stepA.value = a;\n            stepB.value = b;\n\n            let steps = [];\n            let A = a, B = b;\n            while (B !== 0) {\n                let q = Math.floor(A \/ B);\n                let r = A % B;\n                steps.push(`${A} = ${B} \u00b7 ${q} + ${r}`);\n                A = B;\n                B = r;\n            }\n            steps.push(`\u2192 gcd = ${A}`);\n            stepPre.innerText = steps.join('\\n');\n        });\n\n        \/\/ initial update on page load\n        updateMain();\n\n        \/\/ also trigger step builder initial display\n        (function initSteps() {\n            let a = parseInt(stepA.value) || 64;\n            let b = parseInt(stepB.value) || 15;\n            let steps = [];\n            let A = a, B = b;\n            while (B !== 0) {\n                let q = Math.floor(A \/ B);\n                let r = A % B;\n                steps.push(`${A} = ${B} \u00b7 ${q} + ${r}`);\n                A = B;\n                B = r;\n            }\n            steps.push(`\u2192 gcd = ${A}`);\n            stepPre.innerText = steps.join('\\n');\n        })();\n    })();\n<\/script>\n<\/body>\n<\/html>\n\n\n\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <title>tiered \u00b7 collapsible explainer \u00b7 euclidean depths<\/title>\n    <style>\n        * {\n            margin: 0;\n            padding: 0;\n            box-sizing: border-box;\n        }\n        body {\n            background: #eef3fc;\n            font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;\n            padding: 2rem 1rem;\n            color: #091e34;\n        }\n        .explainer-section {\n            max-width: 1000px;\n            margin: 2rem auto 0;\n            background: rgba(255, 255, 255, 0.75);\n            backdrop-filter: blur(4px);\n            border-radius: 2.5rem;\n            padding: 2rem 1.5rem 2.5rem;\n            box-shadow: 0 20px 40px -16px #1d3853;\n            border: 1px solid #ffffffd0;\n        }\n\n        h2 {\n            font-size: 2.2rem;\n            font-weight: 580;\n            color: #113355;\n            border-left: 10px solid #3a74b0;\n            padding-left: 1.4rem;\n            margin-bottom: 2rem;\n            letter-spacing: -0.01em;\n        }\n\n        \/* tiered collapsible styling *\/\n        .tier {\n            background: #ffffffd9;\n            border-radius: 2rem;\n            margin-bottom: 1.2rem;\n            border: 1px solid #b6ceec;\n            overflow: hidden;\n            transition: 0.2s;\n            box-shadow: 0 8px 14px -10px #2d4b74;\n        }\n        .tier-header {\n            background: #e1edff;\n            padding: 1.2rem 1.8rem;\n            font-size: 1.4rem;\n            font-weight: 560;\n            color: #0b2f50;\n            cursor: pointer;\n            display: flex;\n            align-items: center;\n            gap: 0.8rem;\n            border-bottom: 2px solid transparent;\n            transition: background 0.1s;\n        }\n        .tier-header:hover {\n            background: #d2e3ff;\n        }\n        .tier-header .icon {\n            font-size: 1.8rem;\n            line-height: 1;\n            transition: transform 0.2s;\n        }\n        .tier[open] .tier-header .icon {\n            transform: rotate(90deg);\n        }\n        .tier-header::-webkit-details-marker {\n            display: none; \/* remove default triangle *\/\n        }\n        .tier-content {\n            padding: 1.6rem 2rem 2rem;\n            background: #f9fcff;\n            font-size: 1.1rem;\n            line-height: 1.6;\n            border-top: 2px solid #c7dcf5;\n        }\n        .tier-content p {\n            margin-bottom: 1rem;\n        }\n        .tier-content ul, .tier-content ol {\n            margin-left: 1.6rem;\n            margin-bottom: 1rem;\n        }\n        .tier-content li {\n            margin: 0.5rem 0;\n        }\n        .tier-content .insight {\n            background: #e7eef9;\n            border-radius: 1.5rem;\n            padding: 1rem 1.5rem;\n            border-left: 8px solid #9b7c54;\n            margin-top: 1rem;\n        }\n\n        .elegant-math {\n            font-family: 'Courier New', monospace;\n            background: #0b263f;\n            color: #e2efff;\n            padding: 1.2rem 1.8rem;\n            border-radius: 1.5rem;\n            overflow-x: auto;\n            white-space: pre-wrap;\n            margin: 1.2rem 0;\n        }\n\n        hr {\n            border: none;\n            border-top: 2px dashed #a6c0e0;\n            margin: 1.5rem 0;\n        }\n\n        .footnote {\n            margin-top: 2rem;\n            font-style: italic;\n            color: #2a4d75;\n            text-align: center;\n        }\n    <\/style>\n<\/head>\n<body>\n\n<div class=\"explainer-section\">\n    <h2>\ud83d\udcd0 layers of insight \u00b7 the euclidean algorithm<\/h2>\n\n    <!-- TIER 1: ANCIENT ORIGINS -->\n    <details class=\"tier\" open>\n        <summary class=\"tier-header\">\n            <span class=\"icon\">\ud83d\udcdc<\/span> tier 1 \u00b7 a gift from antiquity\n        <\/summary>\n        <div class=\"tier-content\">\n            <p>Attributed to Euclid (c. 300 BCE), but likely known even earlier, this is the oldest surviving general algorithm \u2014 a procedure that has been executed by humans, scribes, and now silicon for over two thousand years. Its endurance is not an accident: it reveals something fundamental about the integers.<\/p>\n            <p>The core observation is simple yet profound: any common divisor of two numbers also divides their remainder. By repeatedly replacing the larger number with the remainder, we descend through ever\u2011smaller pairs until the greatest common divisor (gcd) appears as the last non\u2011zero remainder. There is no guessing, no factorization \u2014 just pure, relentless reduction.<\/p>\n            <div class=\"elegant-math\">\n                gcd(a, b) = gcd(b, a mod b)\n            <\/div>\n            <p>This invariance is the hidden architecture beneath the surface of arithmetic. The algorithm does not just compute; it <em>reveals<\/em> a relationship that was always there.<\/p>\n        <\/div>\n    <\/details>\n\n    <!-- TIER 2: B\u00c9ZOUT'S GHOST -->\n    <details class=\"tier\">\n        <summary class=\"tier-header\">\n            <span class=\"icon\">\ud83d\udd4a\ufe0f<\/span> tier 2 \u00b7 the linear combination\n        <\/summary>\n        <div class=\"tier-content\">\n            <p>The Euclidean algorithm has a secret double life. If we track the quotients and keep careful account, the same steps that yield the gcd also produce two integers \u2014 often called B\u00e9zout coefficients \u2014 that combine the original numbers to make the gcd:<\/p>\n            <div class=\"elegant-math\">\n                a\u00b7x + b\u00b7y = gcd(a, b)\n            <\/div>\n            <p>This is the <strong>extended Euclidean algorithm<\/strong>. The coefficients x and y are not unique; they form a family, a kind of echo of the division steps. The existence of such integers is a theorem (B\u00e9zout's identity), but watching them emerge from the backward substitutions gives a tangible sense of how the integers intertwine.<\/p>\n            <ul>\n                <li>For coprime numbers (gcd = 1), this identity becomes a key to modular division.<\/li>\n                <li>The signs of x and y alternate, a quiet rhythm in the arithmetic.<\/li>\n            <\/ul>\n            <p>This is not a trick; it is a structural consequence of the fact that the integers form a principal ideal domain \u2014 but even without that language, one can feel the inevitability.<\/p>\n        <\/div>\n    <\/details>\n\n    <!-- TIER 3: THE INVERSE UNLOCKED -->\n    <details class=\"tier\">\n        <summary class=\"tier-header\">\n            <span class=\"icon\">\ud83d\udd11<\/span> tier 3 \u00b7 modular inverse\n        <\/summary>\n        <div class=\"tier-content\">\n            <p>When gcd(a, n) = 1, the B\u00e9zout identity a\u00b7x + n\u00b7y = 1 reduces modulo n to a\u00b7x \u2261 1 (mod n). That x \u2014 taken modulo n \u2014 is the <strong>modular inverse<\/strong> of a. It is the number that undoes multiplication by a in the finite ring \u2124\/n\u2124.<\/p>\n            <p>The existence of inverses is what makes \u2124\/p\u2124 (p prime) a field: every nonzero element has a partner that multiplies to 1. But the extended Euclidean algorithm does more than assert existence \u2014 it actually produces the inverse, step by step, from the same quotients used to find the gcd.<\/p>\n            <div class=\"insight\">\n                <p>\u2728 There is a quiet elegance here: the algorithm that finds the greatest common divisor also finds the key to division when the numbers are coprime. The same motion of reduction yields both the measure of commonality and the instrument of reciprocity.<\/p>\n            <\/div>\n            <p>In the interactive tool above, you can see this happen. When gcd = 1, the displayed B\u00e9zout coefficients directly give the inverse of a modulo b (or b modulo a). The numbers speak to each other through the algorithm.<\/p>\n        <\/div>\n    <\/details>\n\n    <!-- TIER 4: BEYOND INTEGERS -->\n    <details class=\"tier\">\n        <summary class=\"tier-header\">\n            <span class=\"icon\">\ud83c\udf0c<\/span> tier 4 \u00b7 resonance in other worlds\n        <\/summary>\n        <div class=\"tier-content\">\n            <p>The Euclidean algorithm is not confined to ordinary integers. It works in any <strong>Euclidean domain<\/strong> \u2014 rings that have a notion of division with remainder, such as:<\/p>\n            <ul>\n                <li>Polynomials over a field (like \u211a[x] or \ud835\udd3d\u2082[x]) \u2014 here the algorithm finds greatest common divisors of polynomials and leads to inverses in finite fields \ud835\udd3d_{p\u207f}.<\/li>\n                <li>Gaussian integers \u2124[i] \u2014 revealing gcds in the complex plane.<\/li>\n                <li>Some rings of algebraic integers (though not all).<\/li>\n            <\/ul>\n            <p>In each setting, the same pattern repeats: repeated remainder steps, back\u2011substitution, and \u2014 when the gcd is a unit \u2014 an inverse emerges. The algorithm is a kind of archetype, a pattern that manifests wherever a sensible notion of \"size\" allows remainders to shrink.<\/p>\n            <p>The example of \ud835\udd3d\u2084 (the field with 4 elements) from the residue fields module: the Euclidean algorithm for polynomials gives the inverses there. The same ancient steps echo in new contexts.<\/p>\n            <div class=\"elegant-math\" style=\"background: #2f3f5f;\">\n                \ud835\udd3d\u2082[x]\/(x\u00b2+x+1)  \u2245  \ud835\udd3d\u2084   \u27f9   (x+1)\u207b\u00b9 = x   via extended euclidean\n            <\/div>\n            <p>This universality is part of what makes the algorithm feel less like a tool and more like a natural phenomenon.<\/p>\n        <\/div>\n    <\/details>\n\n    <!-- TIER 5: CONTEMPLATING INFINITE DESCENT -->\n    <details class=\"tier\">\n        <summary class=\"tier-header\">\n            <span class=\"icon\">\ud83c\udf00<\/span> tier 5 \u00b7 infinite descent and the well\u2011ordering\n        <\/summary>\n        <div class=\"tier-content\">\n            <p>The algorithm terminates because the remainders are non\u2011negative and strictly decreasing \u2014 an infinite descent is impossible in the natural numbers. This relies on the well\u2011ordering principle: every non\u2011empty set of positive integers has a least element. It is a topological fact about the number line, a guarantee that the process cannot continue forever.<\/p>\n            <p>One can view the Euclidean algorithm as a proof by infinite descent: if a common divisor larger than the last remainder existed, it would have to divide an even smaller number, eventually reaching a contradiction. The algorithm is, in this sense, a constructive version of a proof by contradiction.<\/p>\n            <p>There is something meditative in watching the numbers shrink: 108, 30, 18, 12, 6, 0. Each step is forced, inevitable. The algorithm does not invent; it uncovers what was latent in the numbers themselves.<\/p>\n        <\/div>\n    <\/details>\n\n    <!-- small footer within explainer -->\n    <hr>\n    <div class=\"footnote\">\n        \u26a1 the algorithm does not calculate \u2014 it reveals. each step was already written in the relationship between the numbers.\n    <\/div>\n<\/div>\n\n<!-- optional note that this is meant to sit below interactive content -->\n<p style=\"max-width:1000px; margin:1rem auto; text-align:center; color:#346191;\">\u2b06\ufe0f this tiered explainer unfolds below the interactive tools \u2014 a space for contemplation.<\/p>\n\n<\/body>\n<\/html>\n","protected":false},"excerpt":{"rendered":"<p>For millennia, the Euclidean algorithm has revealed the hidden structure of numbers. Here, you can follow its ancient logic step by step, watching remainders shrink until the greatest common divisor emerges. When numbers are coprime, the algorithm yields something profound: a modular inverse, the key that unlocks division in finite fields. This is not a tool for calculation, but a meditation on pattern and relationship\u2014a glimpse into the elegant architecture that governs modular arithmetic.<\/p>\n","protected":false},"author":1,"featured_media":141,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-137","page","type-page","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Euclidean Algorithm - ModularMath<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/modularmath.org\/?page_id=137\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Euclidean Algorithm - ModularMath\" \/>\n<meta property=\"og:description\" content=\"For millennia, the Euclidean algorithm has revealed the hidden structure of numbers. Here, you can follow its ancient logic step by step, watching remainders shrink until the greatest common divisor emerges. When numbers are coprime, the algorithm yields something profound: a modular inverse, the key that unlocks division in finite fields. This is not a tool for calculation, but a meditation on pattern and relationship\u2014a glimpse into the elegant architecture that governs modular arithmetic.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/modularmath.org\/?page_id=137\" \/>\n<meta property=\"og:site_name\" content=\"ModularMath\" \/>\n<meta property=\"og:image\" content=\"https:\/\/modularmath.org\/wp-content\/uploads\/2026\/02\/img_3661.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/modularmath.org\\\/?page_id=137\",\"url\":\"https:\\\/\\\/modularmath.org\\\/?page_id=137\",\"name\":\"Euclidean Algorithm - ModularMath\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/modularmath.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/modularmath.org\\\/?page_id=137#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/modularmath.org\\\/?page_id=137#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/modularmath.org\\\/wp-content\\\/uploads\\\/2026\\\/02\\\/img_3661.png\",\"datePublished\":\"2026-02-15T03:18:29+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/modularmath.org\\\/?page_id=137#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/modularmath.org\\\/?page_id=137\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/modularmath.org\\\/?page_id=137#primaryimage\",\"url\":\"https:\\\/\\\/modularmath.org\\\/wp-content\\\/uploads\\\/2026\\\/02\\\/img_3661.png\",\"contentUrl\":\"https:\\\/\\\/modularmath.org\\\/wp-content\\\/uploads\\\/2026\\\/02\\\/img_3661.png\",\"width\":1024,\"height\":1024},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/modularmath.org\\\/?page_id=137#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/modularmath.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Euclidean Algorithm\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/modularmath.org\\\/#website\",\"url\":\"https:\\\/\\\/modularmath.org\\\/\",\"name\":\"ModularMath\",\"description\":\"Where Numbers Spiral into Meaning.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/modularmath.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Euclidean Algorithm - ModularMath","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/modularmath.org\/?page_id=137","og_locale":"en_US","og_type":"article","og_title":"Euclidean Algorithm - ModularMath","og_description":"For millennia, the Euclidean algorithm has revealed the hidden structure of numbers. Here, you can follow its ancient logic step by step, watching remainders shrink until the greatest common divisor emerges. When numbers are coprime, the algorithm yields something profound: a modular inverse, the key that unlocks division in finite fields. This is not a tool for calculation, but a meditation on pattern and relationship\u2014a glimpse into the elegant architecture that governs modular arithmetic.","og_url":"https:\/\/modularmath.org\/?page_id=137","og_site_name":"ModularMath","og_image":[{"url":"https:\/\/modularmath.org\/wp-content\/uploads\/2026\/02\/img_3661.png","width":1024,"height":1024,"type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/modularmath.org\/?page_id=137","url":"https:\/\/modularmath.org\/?page_id=137","name":"Euclidean Algorithm - ModularMath","isPartOf":{"@id":"https:\/\/modularmath.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/modularmath.org\/?page_id=137#primaryimage"},"image":{"@id":"https:\/\/modularmath.org\/?page_id=137#primaryimage"},"thumbnailUrl":"https:\/\/modularmath.org\/wp-content\/uploads\/2026\/02\/img_3661.png","datePublished":"2026-02-15T03:18:29+00:00","breadcrumb":{"@id":"https:\/\/modularmath.org\/?page_id=137#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/modularmath.org\/?page_id=137"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/modularmath.org\/?page_id=137#primaryimage","url":"https:\/\/modularmath.org\/wp-content\/uploads\/2026\/02\/img_3661.png","contentUrl":"https:\/\/modularmath.org\/wp-content\/uploads\/2026\/02\/img_3661.png","width":1024,"height":1024},{"@type":"BreadcrumbList","@id":"https:\/\/modularmath.org\/?page_id=137#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/modularmath.org\/"},{"@type":"ListItem","position":2,"name":"Euclidean Algorithm"}]},{"@type":"WebSite","@id":"https:\/\/modularmath.org\/#website","url":"https:\/\/modularmath.org\/","name":"ModularMath","description":"Where Numbers Spiral into Meaning.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/modularmath.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/modularmath.org\/index.php?rest_route=\/wp\/v2\/pages\/137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/modularmath.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/modularmath.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/modularmath.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/modularmath.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=137"}],"version-history":[{"count":5,"href":"https:\/\/modularmath.org\/index.php?rest_route=\/wp\/v2\/pages\/137\/revisions"}],"predecessor-version":[{"id":217,"href":"https:\/\/modularmath.org\/index.php?rest_route=\/wp\/v2\/pages\/137\/revisions\/217"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/modularmath.org\/index.php?rest_route=\/wp\/v2\/media\/141"}],"wp:attachment":[{"href":"https:\/\/modularmath.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}