# Extended Euclidean Algorithm

## Objective

The **Extended Euclidean Algorithm** not only computes $$\gcd(A, B)$$, but also finds **integers** $$x$$ **and** $$y$$ that satisfy:

$$
Ax + By = \gcd(A, B)
$$

The pair $$(x, y)$$ is also known as the [**Bézout coefficients**](https://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity).

The **Extended Euclidean Algorithm** is used to compute the [**modular inverse**](/intro/primitives/modular-arithmetic/modular-inverse.md) of an integer, which is essential in many cryptographic algorithms like RSA, ECC, and digital signatures.

## Code (Recursive Version)

```python
def extended_gcd(a, b):
    if b == 0:
        return (a, 1, 0)
    else:
        d, x1, y1 = extended_gcd(b, a % b)
        x = y1
        y = x1 - (a // b) * y1
        return (d, x, y)
```

## Proof

**Base Case:** $$B = 0$$

If $$B = 0$$, then:

$$
\gcd(A, 0) = A \Rightarrow x = 1,\ y = 0
$$

which satisfies the identity:

$$
A \cdot 1 + B \cdot 0
$$

***

**Inductive Hypothesis:**\
Assume the recursive call returns $$(d, x\_1, y\_1)$$ such that:

$$
d = b \cdot x\_1 + (A \bmod B) \cdot y\_1
$$

Then:

$$
\begin{align\*}
d &= B \cdot x\_1+ \left(A - \left\lfloor\frac{A}{B}\right\rfloor \cdot B\right) \cdot y1 \\
&= A \cdot y\_1 + B\left(x\_1 - \left\lfloor\frac{A}{B}\right\rfloor  \cdot y1 \right)
\end{align\*}
$$

Now define:

$$
x = y\_1, \quad y = x\_1 - \left\lfloor\frac{A}{B}\right\rfloor  \cdot y1
$$

So:

$$
Ax + By = d
$$

which proves that the identity holds at each step.

## Code (Non-Recursive Version)

```python
def extended_gcd(a, b):
    # Initialize: (r, s, t) ← (a, 1, 0), (r', s', t') ← (b, 0, 1)
    r_prev, r = a, b
    s_prev, s = 1, 0
    t_prev, t = 0, 1

    while r != 0:
        q = r_prev // r  # quotient

        # Update (r, s, t)
        r_prev, r = r, r_prev - q * r
        s_prev, s = s, s_prev - q * s
        t_prev, t = t, t_prev - q * t

    # At termination: r_prev = gcd(a, b), s_prev = x, t_prev = y
    return r_prev, s_prev, t_prev
```

> Written by [Ryan Kim](mailto:undefined) of Fractalyze


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fractalyze.gitbook.io/intro/primitives/euclidean-algorithm/extended-euclidean-algorithm.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
