TITLE: How to find the inverse modulo $m$?
QUESTION [77 upvotes]: For example:
$$7x \equiv 1 \pmod{31} $$
In this example, the modular inverse of $7$ with respect to $31$ is $9$. How can we find out that $9$? What are the steps that I need to do?
Update
If I have a general modulo equation:
$$5x + 1 \equiv 2 \pmod{6}$$
What is the fastest way to solve it? My initial thought was:
$$5x + 1 \equiv 2 \pmod{6}$$
$$\Leftrightarrow 5x + 1 - 1\equiv 2 - 1 \pmod{6}$$
$$\Leftrightarrow 5x \equiv 1 \pmod{6}$$
Then solve for the inverse of $5$ modulo 6. Is it a right approach?
Thanks.

REPLY [2 votes]: Solve $7x \equiv 1 \pmod{31}$.
$\quad 7x = 1 + 31y \implies 3y \equiv 6 \pmod{7} \implies y \equiv 2 \pmod{7}$
and
$\quad y : 2 \;  \mid \; 1 + 31y = 63 \quad \quad \text{AND is divisible by } 7$
So,
$\tag{ANS} 7^{-1} \equiv 9 \pmod{31}$

Here is another more interesting example:
Solve $25x \equiv 1 \pmod{41}$.
$\quad 25x = 1 + 41y \implies y \equiv 4 \pmod{5}$
and
$\quad y : \;\,4 \;  \mid \; 1 + 41y = 165 \quad \quad \text{NO GOOD!}$
$\quad y : \;\,9 \;  \mid \; 1 + 41y = 370 \quad \quad \text{NO GOOD!}$
$\quad y : 14 \;  \mid \; 1 + 41y = 575 \quad \quad \text{AND is divisible by } 25$
So,
$\tag{ANS} 25^{-1} \equiv 23 \pmod{41}$

Here is another more interesting example:
Solve $23x \equiv 1 \pmod{41}$.
$\quad 23x = 1 + 41y \implies 18y \equiv 22 \pmod{23} \implies 9y \equiv 11 \pmod{23}$
Sub Solve: $9y \equiv 11 \pmod{23}$.
$\quad 9y = 11 + 23z \implies 2z \equiv 1 \pmod{3} \implies z \equiv 2 \pmod{3}$
and
$\quad z : \;\,2 \;  \mid \; 11 + 23z = 057 \quad \quad \text{NO GOOD!}$
$\quad z : \;\,5 \;  \mid \; 11 + 23z = 126 \quad \quad \text{AND is divisible by } 9$
So,
$\quad 9y \equiv 11 \pmod{23} \text{ iff } y \equiv 14 \pmod{23}$
Continuing where we left off with the original problem,
$\quad y : \;\,14 \;  \mid \; 1 + 41y = 575 \quad \quad \text{AND is divisible by } 23$
So,
$\tag{ANS} 23^{-1} \equiv 25 \pmod{41}$