# How to solve $227x \equiv 1 ~ (\text{mod} ~ 2011)$?

How to solve $227x \equiv 1 ~ (\text{mod} ~ 2011)$?

I just asked this question, and it seems those methods are not really suitable for large numbers. Please give me some ideas. Thank you.

-
It is, but the algorithm will take longer. – Pedro Tamaroff Mar 2 '13 at 6:02
You could review the Wikipedia article or search for "Extended Euclidean algorithm" – Ross Millikan Mar 2 '13 at 6:07
The Euclidean algorithm is actually pretty efficient, so the methods described in the other thread will likely not take as long as you at first might think. – JavaMan Mar 2 '13 at 6:08
Chiming in. Methods other than those listed will probably take longer. Just roll up your sleeves. – Jyrki Lahtonen Mar 2 '13 at 6:23

As mentioned in comments, the Euclidean algorithm will be fastest in general for this kind of thing. But since you asked for something different, since $2011$ is prime, for $y\not\equiv0\mod2011$

$$y^{2009}\equiv y^{-1}\mod{2011}$$

So one answer to your question is $227^{2009}$. Reducing that mod $2011$ would be done most quickly by repeated squaring.

-
If you have a calculator that does multiplication modulo a given prime, this is undoubtedly the quickest way of finding a reciprocal. – Lubin Mar 2 '13 at 6:35
I don't see any remark by the OP asking for something different from the extended Euclidean algorithm. It is unlikely that any other method will be competitive. It required only 30 seconds by hand to do the extended Euclidean algorithm in my answer. Good luck beating that! – Math Gems Mar 2 '13 at 6:58
@Math Gems Following the link at the time I read this question, all the answers there were either connected to the Euclidean algorithm, involved finding a generator mod $p$, or involved exhausting all possibilities by direct checking. OP indicates that "these methods" are, in OP's opinion, not suitable for large numbers. So for fun I give something else. And as noted in my answer, yes, the Euclidean algorithm is generally fastest for doing this kind of thing. – alex.jordan Mar 2 '13 at 7:01
@Alex The extended Euclidean algorithm is definitely the most efficient general method for large numbers. I see no explicit remark by the OP ruling it out. – Math Gems Mar 2 '13 at 7:05
@Math Gems "The extended Euclidean algorithm is definitely the most efficient general method for large numbers." Yeah, that's what I said. You are reading what I wrote right? "I see no explicit remark by him ruling it out." No it's not explicit. It is implied if you actually follow OP's link. – alex.jordan Mar 2 '13 at 7:10

Fermat's Little Theorem states that

For any integer $a$, the number $a^p − a$ is an integer multiple of $p$ when $p$ is a prime.

Therefore, $a^{p - 1} \equiv 1 \text{ (mod p)}$

We have here $p = 2011$. Hence,

$a^{p-1} = 227^{p-1} = 227^{2010}$. But we already have it as $227x$. Therefore, $x = 227^{2009}$.

-

The extended Euclidean algorithm took $30$ secs by hand:

$\quad\begin{eqnarray} 2011 =\ \ 1\cdot &2011& +\ \ \ 0\,\cdot &227&\\ 227 =\ \ 0\cdot &2011& +\ \ \ 1\,\cdot &227&\\ 32 = -1\cdot &2011& + \ \ \ 9\,\cdot &227&\\ 3 =\ \ 7\cdot &2011& -\ 62\cdot &227&\\ 1 = 78\cdot &2011&\! - 691\cdot &227&\\ \end{eqnarray}$

Hence $\rm\ mod\,\ 2011\!:\ {-}691\cdot 227\equiv 1$

I doubt you'll find any method more efficient than that. For large, general numbers, the extended Euclidean algorithm is almost always the most efficient method (whether by hand or machine).

-
$30$ seconds by hand is still impressive. :) – Haskell Curry Mar 2 '13 at 7:19

Let's use the extended Euclidean algorithm. One way of arranging the calculation is to set up a matrix

$$\begin{array}{cc|c} 1 & 0 & 2011 \\ 0 & 1 & 227 \end{array} \qquad \qquad (I)$$

The interpretation is that the left column is the coefficient on 2011 and the middle column is the coefficient on 227, and the right column is the number thus produced.

But 4 digit numbers are big! I'm lazy so I'll just look at the top 2 digits:

$$\begin{array}{cc|c} 1 & 0 & 20 \\ 0 & 1 & 2 \end{array}$$

Ah, I can do the extended Euclidean algorithm on that. In fact, it's so easy I can do it in my head:

$$\begin{array}{cc|c} 1 & -10 & 0 \\ 0 & 1 & 2 \end{array}$$

Now I know how to reduce the top digits, I'll take the original array and multiply it by the matrix $\left(\begin{matrix}1 & -10 \\ 0 & 1\end{matrix}\right)$, to get

$$\begin{array}{cc|c} 1 & -10 & -259 \\ 0 & 1 & 227 \end{array} \qquad \qquad(II)$$

I'm lazy, I'll work on just the top 2 digits again

$$\begin{array}{cc|c} 1 & 0 & -25 \\ 0 & 1 & 22 \end{array}$$ $$\begin{array}{cc|c} 1 & 1 & -3 \\ 0 & 1 & 22 \end{array}$$ $$\begin{array}{cc|c} 1 & 1 & -3 \\ 7 & 8 & 1 \end{array}$$ $$\begin{array}{cc|c} 22 & 25 & 0 \\ 7 & 8 & 1 \end{array}$$

Now I'll multiply so now if I multiply (II) by the matrix $\left(\begin{matrix}22 & 25 \\ 7 & 8\end{matrix}\right)$, I'll get

$$\begin{array}{cc|c} 22 & -195 & -23 \\ 7 & -62 & 3\end{array} \qquad \qquad(III)$$

Okay this isn't too bad. But I'm still lazy again:

$$\begin{array}{cc|c} 1 & 0 & -23 \\ 0 & 1 & 3 \end{array}$$

$$\begin{array}{cc|c} 1 & 8 & 1 \\ 0 & 1 & 3 \end{array}$$ $$\begin{array}{cc|c} 1 & 8 & 1 \\ -3 & -23 & 0 \end{array}$$

I just want the top row, so I'll multiply (III) on the left by $(1\ 8)$:

$$\begin{array}{cc|c} 78 & -691 & 1\end{array} \qquad \qquad(IV)$$

and now I know

$$78 \cdot 2011 - 691 \cdot 227 = 1$$

in particular,

$$227 \cdot (-691) = 1 \pmod{2011}$$

I could have saved myself some trouble by only using the second and third columns of (I), (II), (III), (IV) since I don't really care about 78.

Computers use this idea to do very large gcd's; either Lehmer's GCD algorithm or the half-GCD algorithm depending on how much of an extreme you take the idea.

-