# Extended Euclidean Algorithm for Modular Inverse

I'm currently learning how to find the inverse of a modulo with the Extended Euclid Algorithm and I stumbled upon a problem when finding an inverse when the $m>p$ as for $m \equiv 1 \pmod{p}$

For example, in $$240x \equiv 1 \pmod{17}$$ what is the inverse? What are the steps to find it?

• Did you try the algorithm? Where are you stuck at? – egreg Apr 9 '14 at 21:57
• yes sir i already tried. what i know is that if there is a problem like this 17x ≡ 1(mod43) to find the inverse i have to do this 43 = 17*2 + 9, then take the 17 and 9 so that 17 = 9*1 + 8, then 9 = 8*1 +1 <-- now that i got 1. i can subs it and so on. but how do i do this if it was the opposite? 43x ≡ 1(mod17) .. 17 = 43.1 + (-26)???? – user142068 Apr 9 '14 at 22:13

You have to write $$1 = 240x+17y$$ so $$240x\equiv 1\pmod{17}$$

The Euclidean algorithm applied to $240$ and $17$ gives \begin{align} \color{red}{240} &= \color{red}{17}\cdot 14 + \color{red}{2} \\ \color{red}{17} &= \color{red}{2}\cdot 8 + \color{red}{1} \end{align} The successive remainders are colored red. Now start from the top: $$\color{red}{2}=\color{red}{240}-\color{red}{17}\cdot 14$$ Go one line down: $$\color{red}{1}=\color{red}{17}-\color{red}{2}\cdot 8$$ Substitute the value you have for $\color{red}{2}$: $$\color{red}{1}=\color{red}{17}-(\color{red}{240}-\color{red}{17}\cdot 14)\cdot 8 =\color{red}{17}\cdot(1+132)-\color{red}{240}\cdot 8 =\color{red}{240}\cdot(-8)+\color{red}{17}\cdot 133$$ Thus you can take $x=-8$, or else $x=9$ since $-8\equiv 9\pmod{17}$. Indeed $$240\cdot9=2160=17\cdot127+1$$ or $$240\cdot 9\equiv 1\pmod{17}.$$

Just remember operating on the “red numbers” as if they were letters. Express each remainder in terms of the previous ones and substitute in the equations below the first. Only terms with $\color{red}{240}$ and $\color{red}{17}$ multiplied by integers will remain.

• wow i didn't expect some detailed explaination, this is simply amazing. but in the 1=17−(240−17⋅14)⋅8 = 17⋅(1+132)−240⋅8= 240⋅(−8)+17⋅133. where did the middle come from? thank you very much for the answer dear sir. – user142068 Apr 9 '14 at 22:41
• Use $X$ for $240$ and $Y$ for $17$: $1=Y-8(X-14Y)=-8X+Y+8\cdot14Y=(-8)X+133Y$. We use, from the relation above, that $2=X-14Y$. – egreg Apr 9 '14 at 22:51

There is no fast, efficient way of finding an inverse. A general tip is to reduce as much as possible first to get everything between $0$ and $p-1$ when taking $\pmod p$. So, $240x \equiv 2x \pmod {17}$ and the inverse of $2$ is just $9$ so $x \equiv 9 \pmod {17}$.

• excuse me sir. did you get 240x ≡ 2x(mod17) by dividing 240 till the smallest (is this what you meant by reducing?) it can be because if a ≡ 1(mod p) it also apply if a.b so that a.b ≡1(modp)? – user142068 Apr 9 '14 at 22:00
• He said: $240=14\cdot17+2$, thus $240\equiv2\pmod{17}$. So he replaces the coefficient $240$ by the coefficient $2$. Remember: $a\equiv a'\pmod D$ and $b\equiv b'\pmod D$ implies that $ab\equiv a'b'\pmod D$. – Lubin Apr 9 '14 at 22:56
• ahh i see, thank very much to all of you. it's all clear now. – user142068 Apr 9 '14 at 23:26

In this case, m > p. So find the remainder of m/p. m mod p = 240 mod 17 = 2. so the inverse of 240 and the inverse of 2 (mod 17) are the same. The above answer stating the inverse is 9 is correct (2 * 9 = 18 and 18 mod 17 = 1)