how do you find the modular inverse

I need to find out the modular inverse of 5(mod 11), I know the answer is 9 and got the following so far and don’t understand how to than get the answer. I know how to get the answer for a larger one such as 27(mod 392) but am stuck because they are both low numbers.

11=5 (2)+1

5=1 (5)

Solutions Collecting From Web of "how do you find the modular inverse"

In finding a modular inverse, you are trying to solve the modular equation
$$
ax\equiv 1\pmod n.
$$
Ordinarily, you use the Extended Euclidean Algorithm for this to solve the equation $ax+ny=1$. If the numbers $a$, and $n$ are small, then simple trial and error is probably just as fast or faster.

For your example, we have $a=5$ and $n=11$, which means would could just use trial and error.
\begin{align}
1\cdot 5 &\equiv 5\\
2\cdot 5 &\equiv 10\\
3\cdot 5 &\equiv 15\equiv 4 \\
4\cdot 5 &\equiv 20\equiv -2\\
5\cdot 5 &\equiv 25\equiv 3 \\
6\cdot 5 &\equiv 30\equiv -3 \\
7\cdot 5 &\equiv 35\equiv 2 \\
8\cdot 5 &\equiv 40\equiv -4 \\
9\cdot 5 &\equiv 45\equiv 1 \\
10\cdot 5 &\equiv 50\equiv -5 \\
\end{align}

Since $9\cdot 5 \equiv 1$ then we have found the modular inverse to be 9.

When looking at those numbers on the far right side, keep in mind that any multiple of 11 made be added or subtracted to the modulus and it is still equivalent. That is, $-3\equiv 30\equiv 8$ since $-3+3(11)=30$ and $8+2(11)=30$.

Hint $\ $ mod $\,ab\!+\!1\!:\,\, ab \equiv -1 \,\Rightarrow\, a(-b) \equiv 1.\ $ Yours is $\,a,b = 5,2.$

Generally one can use the Extended Euclidean Algorithm to compute modular inverses (above is an optimization of the single-step case). Here is a convenient way to execute the algorithm.

You can use your work to get $1=11-5(2)=11+5(-2)$,

so an inverse of 5 (mod 11) is given by $-2\equiv9\pmod {11}$.

Here is a piece of C code that you might find useful:

int Inverse(int n,int a)
{
    int x1 = 1;
    int x2 = 0;
    int y1 = 0;
    int y2 = 1;
    int r1 = n;
    int r2 = a;

    while (r2 != 0)
    {
        int r3 = r1%r2;
        int q3 = r1/r2;
        int x3 = x1-q3*x2;
        int y3 = y1-q3*y2;

        x1 = x2;
        x2 = x3;
        y1 = y2;
        y2 = y3;
        r1 = r2;
        r2 = r3;
    }

    return y1>0? y1:y1+n;
}

Calling Inverse(11,5) returns 9.

Calling Inverse(392,27) returns 363.