Modular Inverses

Note

This post assumes a basic knowledge of modular arithmetic and the notation for it. In this article, we will use the following notation to denote bb as the remainder when aa is divided by nn.

ab(modn)a \equiv b \pmod{n}

We will also less frequently use the notation a % n=ca\ \%\ n = c to denote the same thing.

Division in Modular Arithmetic

For modular arithmetic, the operations of addition, subtraction, and multiplication are pretty easy to define and understand. You simply perform the operation and then perform the modulo operator on the output.

3+470(mod7)3 + 4 \equiv 7 \equiv 0 \pmod{7}
3416(mod7)3 - 4 \equiv -1 \equiv 6 \pmod{7}
34125(mod7)3 * 4 \equiv 12 \equiv 5 \pmod{7}

However, this doesn't apply to division, since division frequently leads to fractions/decimals, which is not what we are looking for when considering remainders. However, what if we want to solve this problem:

3x4(mod7)3x \equiv 4 \pmod{7}

In typical arithmetic, we would divide both sides by 33 to solve for xx, but that doesn't work here since 34\dfrac{3}{4} doesn't make sense as a remainder for xx after division if we assume that xx must be a whole number.

Divison is a pretty weird thing in modular arithmetic, although it can be applied in some cases. We actually can do division in modular arithmetic with respect to nn to solve the equation under a few conditions:

  1. Both sides of the equation are divisible by the divisor.
  2. The divisor is coprime with nn.
Condition (1) MetCondition (1) Not Met
Condition (2) MetYou can divide safelyYou have to use modular inverses
Condition (2) Not MetYou have to divide everything by gcd(a,n)\gcd(a, n)The equation has no solutions

Here are examples of equations that would belong in each box:

Condition (1) MetCondition (1) Not Met
Condition (2) Met3x6(mod7)3x \equiv 6 \pmod{7}3x4(mod7)3x \equiv 4 \pmod{7}
Condition (2) Not Met6x12(mod15)6x \equiv 12 \pmod{15}3x4(mod6)3x \equiv 4 \pmod{6}

For instance, the given bottom right equation has no integer solutions. For the upper right equation, we can divide both sides by 3 to solve:

x2(mod7)x \equiv 2 \pmod{7}

We will primarily go over the upper right condition in this article. At the end, we will briefly discuss the lower left condition. The other two conditions are easily solved.

Modular Inverse Basics

Definition

The modular inverse of a number aa is defined as a1a^{-1} such that

aa11(modn)a * a^{-1} \equiv 1 \pmod{n}

If we knew the modular inverse of 33 with respect to 77, we could multiply that on both sides of our equation to cancel out the 33 and obtain our whole number answer.

There are many ways we can find modular inverses of a number, and the most simple of these is to simply try every possible number from 00 to n1n - 1. For instance, we could calculate 3x(mod7)3 * x \pmod{7} for all values xx from 00 to 66.

In this case, we can relatively quickly find that the modular inverse of 33 is 55 since 35=153 * 5 = 15, which is 11 above a multiple of 77. Therefore, we can solve our equation as follows:

3x545(mod7)x6(mod7)3x * 5 \equiv 4 * 5 \pmod{7} \rightarrow x \equiv 6 \pmod{7}

Therefore, we can conclude that all numbers that leave a remainder of 66 when divided by 77 satisfy the original equation.

However, not all numbers have a modular inverse. For instance, 22 does not have a modular inverse with respect to 44. You can try it out and you will quickly see that there are no values xx such that 2x1(mod4)2x \equiv 1 \pmod{4}.

Theorem

The modular inverse of aa with respect to nn only exists if aa and nn share no common factors besides 11 or, more formally, gcd(a,n)=1\gcd(a, n) = 1.

We can prove this by first assuming that there exists a number aa and its modular inverse a1a^{-1} with respect to nn such that gcd(a,n)1\gcd(a, n) \neq 1. Then, we can write the following equation:

aa11(modn)a * a^{-1} \equiv 1 \pmod{n}

This is equivalent to writing the following equation for some integer kk:

aa1=kn+1a * a^{-1} = kn + 1

We can rearrange this to obtain the following:

aa1kn=1a * a^{-1} - kn = 1

However, note that we previously assumed that gcd(a,n)1\gcd(a, n) \neq 1, but that implies that the left hand side of the above equation has the common factor gcd(a,n)1\gcd(a, n) \neq 1. However, the only integer factors of the right hand side are 11, making this statement impossible. Therefore, we have reached a contradiction and we can conclude that there exists no modular inverse of a number aa if gcd(a,n)1\gcd(a, n) \neq 1.

Calculating Modular Inverses with Euler's Theorem

While we introduced the brute-force method above for calculating modular inverses, we can actually solve for the modular inverse directly with Euler's theorem.

Theorem

If aa and nn are coprime, then the following is true:

aϕ(n)1(modn)a^{\phi(n)} \equiv 1 \pmod{n}

where ϕ(n)\phi(n) is the Euler totient function and is defined as the number of positive integers x<nx < n such that xx and nn are coprime. If nn is prime, then ϕ(n)=n1\phi(n) = n - 1. If nn is not prime and has the prime factorization

n=p1a1p2a2...n = {p_1}^{a_1}{p_2}^{a_2}...

then ϕ(n)\phi(n) can be calculated as

ϕ(n)=np11p1p21p2...\phi(n) = n * \dfrac{p_1 - 1}{p_1} * \dfrac{p_2 - 1}{p_2} * ...

By cleverly using this fact, we can actually calculate the modular inverse of aa as follows:

aa11(modn)a * a^{-1} \equiv 1 \pmod{n}
aϕ(n)1(modn)a^{\phi(n)} \equiv 1 \pmod{n}

By substituting the second equation into the first one, we obtain the following relation:

aa1aϕ(n)(modn)a * a^{-1} \equiv a^{\phi(n)} \pmod{n}
a1aϕ(n)1(modn)\boxed{a^{-1} \equiv a^{\phi(n) - 1} \pmod{n}}

In this case, we actually divided both sides by aa, which is valid since aa and nn are coprime (if there exists a modular inverse) and both sides were divisible by aa.

By using this fact, we can actually calculate directly the modular inverse. For instance, if we wanted to calculate 31(mod7)3^{-1} \pmod{7} without brute force, we could do the following:

ϕ(7)=71=6\phi(7) = 7 - 1 = 6
31361352435(mod7)3^{-1} \equiv 3^{6 - 1} \equiv 3^5 \equiv 243 \equiv 5 \pmod{7}

However, as you can probably guess, this method, although direct, can require some intense calculation with very large powers for large nn. However, for small nn, it can be much faster, especially when used for calculating several modular inverses with respect to nn. Additionally, this method is also much more reasonable when done using a computer, which can churn these out very quickly. When combined with binary exponentiation, calculating large exponents is not that daunting.

However, there is another method that can be used to calculate modular inverses, which we will discuss next.

Calculating Modular Inverses with Extended Euclidean Algorithm

Another method of calculating modular inverses that doesn't require huge exponents is with the extended euclidean algorithm.

Extended Euclidean Algorithm

You may have heard of the Euclidean algorithm, which is a computational method for determining the gcd\gcd of two numbers. For those who haven't or need a quick refresher, it is described below.

Theorem

Let the two numbers be aa and bb, with a>ba > b. If b=0b = 0, then the gcd(a,b)=a\gcd(a, b) = a Otherwise,

gcd(a,b)=gcd(a % b,b)\gcd(a, b) = \gcd(a\ \%\ b, b)

As an example, we can compute gcd(100,40)\gcd(100, 40).

gcd(100,40)=gcd(20,40)=gcd(20,0)=20\gcd(100, 40) = \gcd(20, 40) = \gcd(20, 0) = \boxed{20}

The extended Euclidean algorithm is a bit different, although it also calculates the gcd\gcd.

Theorem

Bézout's identity states there always exists integers xx and yy such that the following is true:

ax+by=gcd(a,b)ax + by = \gcd(a, b)

Given the numbers aa and bb, the extended Euclidean algorithm gives us a method to calculate both the coefficients xx and yy given in Bézout's identity, as well as gcd(a,b)\gcd(a, b). This is why it's called the extended Euclidean algorithm. While the normal Euclidean algorithm just gives us gcd(a,b)\gcd(a, b), the extended version also tells us how to form that gcd\gcd through a linear combination of aa and bb.

To show how it works, we can use an example with a=42a = 42 and b=56b = 56. As mentioned before, this will give us gcd(42,56)\gcd(42, 56), as well as xx and yy in the equation:

46x+56y=gcd(46,56)46x + 56y = \gcd(46, 56)

To do this, we first write 5656 as the sum of a multiple of 4646 and a remainder.

56=46(1)+1056 = \colorbox{RoyalBlue}{46}(1) + \colorbox{BurntOrange}{10}

Next, this may seem pretty arbitrary, but we repeat the same process, this time writing 4242 as the sum of a multiple of the previous remainder, 1010, and another remainder.

46=10(4)+6\colorbox{RoyalBlue}{46} = \colorbox{BurntOrange}{10}(4) + \colorbox{ForestGreen}{6}

We repeat this process again until we obtain 00 as a remainder.

10=6(1)+46=4(2)+24=2(2)+0\begin{aligned} \colorbox{BurntOrange}{10} &= \colorbox{ForestGreen}{6}(1) + \colorbox{Orchid}{4} \\ \colorbox{ForestGreen}{6} &= \colorbox{Orchid}{4}(2) + \colorbox{RoyalBlue}{2} \\ \colorbox{Orchid}{4} &= \colorbox{RoyalBlue}{2}(2) + 0 \\ \end{aligned}

Once we obtain 00 as the remainder, we can stop. From this process, the last non-zero remainder that we obtained is actually gcd(a,b)\gcd(a, b). Since in this case it was 22, gcd(46,56)=2\gcd(46, 56) = 2.

Note that this process is actually just running the Euclidean algorithm, it's just that we kept a bunch of extra information as well. We can summarize this process below:

56=46(1)+1046=10(4)+610=6(1)+46=4(1)+24=2(2)+0\begin{aligned} 56 &= \colorbox{RoyalBlue}{46}(1) + \colorbox{BurntOrange}{10} \\ \colorbox{RoyalBlue}{46} &= \colorbox{BurntOrange}{10}(4) + \colorbox{ForestGreen}{6} \\ \colorbox{BurntOrange}{10} &= \colorbox{ForestGreen}{6}(1) + \colorbox{Orchid}{4} \\ \colorbox{ForestGreen}{6} &= \colorbox{Orchid}{4}(1) + \colorbox{RoyalBlue}{2} \\ \colorbox{Orchid}{4} &= \colorbox{RoyalBlue}{2}(2) + 0 \end{aligned}

To determine xx and yy, we need to do a bit of rearranging to isolate the remainder term in each of the equations as follows. We're simply just subtracting a term from the right side and placing it on the right side.

56 - 46(1) = 1046 - 10(4) = 610 - 6(1) = 46 - 4(1) = 2\begin{aligned} \colorbox{BurntOrange}{56 - 46(1) = 10} \\ \colorbox{ForestGreen}{46 - 10(4) = 6} \\ \colorbox{Orchid}{10 - 6(1) = 4} \\ \colorbox{RoyalBlue}{6 - 4(1) = 2} \end{aligned}

Now that we have this, we are actually all set to solve for xx and yy! Remember, we are trying to find xx and yy such that:

46x+56y=gcd(46,56)=246x + 56y = \gcd(46, 56) = 2

To do so, we look at the last equation, which expresses gcd(46,56)\gcd(46, 56) as a linear combination of 66 and 44, not the desired 4646 and 5656.

64(1)=26 - \colorbox{Orchid}{4}(1) = 2

However, the equation right above it is an expression for 44.

10 - 6(1) = 4\colorbox{Orchid}{10 - 6(1) = 4}

We can substitute this in to obtain the following:

6(10 - 6(1))(1)=610(1)+6(1)=6(2)10(1)=26 - \colorbox{Orchid}{(10 - 6(1))}(1) = 6 - 10(1) + 6(1) = \colorbox{ForestGreen}{6}(2) - 10(1) = 2

This expression now expresses gcd(46,56)\gcd(46, 56) as a linear combination of 66 and 1010. Once again, we can use the expression above it to substitute, this time for 66.

46 - 10(4) = 6\colorbox{ForestGreen}{46 - 10(4) = 6}
(46 - 10(4))(2)10(1)=2(46)10(9)=2\colorbox{ForestGreen}{(46 - 10(4))}(2) - 10(1) = 2(46) - \colorbox{BurntOrange}{10}(9) = 2

We can repeat this step one last time and obtain the following:

56 - 46(1) = 10\colorbox{BurntOrange}{56 - 46(1) = 10}
46(2)(56 - 46(1))(9)=46(11)56(9)=246(2) - \colorbox{BurntOrange}{(56 - 46(1))}(9) = \boxed{46(11) - 56(9) = 2}

From this, we can determine that x=11x = 11 and y=9y = -9!

Here all of the calculations sorted:

  1. Express each term as the sum of a multiple of the other term and a remainder.
56=46(1)+1046=10(4)+610=6(1)+46=4(1)+24=2(2)+0\begin{aligned} 56 &= \colorbox{RoyalBlue}{46}(1) + \colorbox{BurntOrange}{10} \\ \colorbox{RoyalBlue}{46} &= \colorbox{BurntOrange}{10}(4) + \colorbox{ForestGreen}{6} \\ \colorbox{BurntOrange}{10} &= \colorbox{ForestGreen}{6}(1) + \colorbox{Orchid}{4} \\ \colorbox{ForestGreen}{6} &= \colorbox{Orchid}{4}(1) + \colorbox{RoyalBlue}{2} \\ \colorbox{Orchid}{4} &= \colorbox{RoyalBlue}{2}(2) + 0 \end{aligned}
  1. Rearrange each of those equations except the last to obtain an expression for the remainder.
56 - 46(1) = 1046 - 10(4) = 610 - 6(1) = 46 - 4(1) = 2\begin{aligned} \colorbox{BurntOrange}{56 - 46(1) = 10} \\ \colorbox{ForestGreen}{46 - 10(4) = 6} \\ \colorbox{Orchid}{10 - 6(1) = 4} \\ \colorbox{RoyalBlue}{6 - 4(1) = 2} \end{aligned}
  1. Starting from the last equation, substitute in the expression directly on top and combine like terms.
64(1)=26(10 - 6(1))(1)=6(2)10(1)=2(46 - 10(4))(2)10(1)=2(46)10(9)=246(2)(56 - 46(1))(9)=46(11)56(9)=2\begin{aligned} 6 - \colorbox{Orchid}{4}(1) = 2 \\ 6 - \colorbox{Orchid}{(10 - 6(1))}(1) = \colorbox{ForestGreen}{6}(2) - 10(1) = 2 \\ \colorbox{ForestGreen}{(46 - 10(4))}(2) - 10(1) = 2(46) - \colorbox{BurntOrange}{10}(9) = 2 \\ 46(2) - \colorbox{BurntOrange}{(56 - 46(1))}(9) = \boxed{46(11) - 56(9) = 2} \end{aligned}

For another worked out example, check out this video:

Applying to Linear Diophantine Equations

As a quick side note, we can also apply this to solve linear diophantine equations of two variables, where diophantine means that the equation only has integer solutions. We won't get too deep into the details, but if we have an equation like the following, we can solve it pretty easily.

19a+7b=2319a + 7b = 23

Since gcd(19,7)=1\gcd(19, 7) = 1, we can use the extended Euclidean algorithm to find the values of aa' and bb' that make 19a+7b=119a' + 7b' = 1. Then, we can scale these values up by 2323 to get the solution for our original equation. This works for any value on the right side that we need to find, as long as it is is a multiple of the gcd\gcd. It can actually be shown that this will always produce the solution for us if it exists since if the right side is not a multiple of the gcd\gcd, there are no solutions.

Applying to Modular Inverses

To see how this can be useful for what this article is about, let's consider what happens when aa and bb are coprime. By definition then, gcd(a,b)=1\gcd(a, b) = 1, meaning that

ax+by=1ax + by = 1

If we consider this equation mod b\text{mod } b, then we obtain the following:

ax=1(modb)ax = 1 \pmod{b}

Therefore, a1x(modb)a^{-1} \equiv x \pmod{b} and b1y(moda)b^{-1} \equiv y \pmod{a}!

While this method may appear longer than the previous one, it does not involve any exponents and therefore does not require as large of calculations. For example, this method can actually be used to calculate the modular inverse of extremely large numbers with just a four-function calculator, which is especially useful when doing things such as decrypting RSA ciphers by hand (the reason why I bring this up is because of the Codebusters event I did in Science Olympiad).

Example

Calculate the modular inverse of 13819371381937 with respect to 283819382283819382.

Clearly, exponentiating 13819371381937 would not lead to a good time, as ϕ(283819382)=108701280\phi(283819382) = 108701280. This could be done with a computer rather quickly if you use binary lifting exponentiation, but doing so by hand would almost definitely lead to a bad time. Instead, we should use the extended Euclidean algorithm to find the modular inverse.

We first write each term as the sum of multiples of the other term and a remainder:

283819382=1381937(205)+5222971381937=522297(2)+337343522297=337343(1)+184954337343=184954(1)+152389184954=152389(1)+32565152389=32565(4)+2212932565=22129(1)+1043622129=10436(2)+125710436=1257(8)+3801257=380(3)+117380=117(3)+29117=29(4)+129=1(29)+0\begin{aligned} 283819382 &= 1381937(205) + 522297 \\ 1381937 &= 522297(2) + 337343 \\ 522297 &= 337343(1) + 184954 \\ 337343 &= 184954(1) + 152389 \\ 184954 &= 152389(1) + 32565 \\ 152389 &= 32565(4) + 22129 \\ 32565 &= 22129(1) + 10436 \\ 22129 &= 10436(2) + 1257 \\ 10436 &= 1257(8) + 380 \\ 1257 &= 380(3) + 117 \\ 380 &= 117(3) + 29 \\ 117 &= 29(4) + 1 \\ 29 &= 1(29) + 0 \\ \end{aligned}

From this exhausting amount of calculations, we can determine that gcd(283819382,1381937)=1\gcd(283819382, 1381937) = 1, so the modular inverse of 13819371381937 does in fact exist. We can proceed with the next step, to rearrange each of the equations:

2838193821381937(205)=5222971381937522297(2)=337343522297337343(1)=184954337343184954(1)=152389184954152389(1)=3256515238932565(4)=221293256522129(1)=104362212910436(2)=1257104361257(8)=3801257380(3)=117380117(3)=2911729(4)=1\begin{aligned} 283819382 - 1381937(205) &= 522297 \\ 1381937 - 522297(2) &= 337343 \\ 522297 - 337343(1) &= 184954 \\ 337343 - 184954(1) &= 152389 \\ 184954 - 152389(1) &= 32565 \\ 152389 - 32565(4) &= 22129 \\ 32565 - 22129(1) &= 10436 \\ 22129 - 10436(2) &= 1257 \\ 10436 - 1257(8) &= 380 \\ 1257 - 380(3) &= 117 \\ 380 - 117(3) &= 29 \\ 117 - 29(4) &= 1 \\ \end{aligned}

Finally, we back-substitute each of the expressions one-by-one into the one below it, starting from the bottom.

11729(4)=1117(380117(3))(4)=117(13)380(4)=1(1257380(3))(13)380(4)=1257(13)380(43)=11257(13)(104361257(8))(43)=1257(357)10436(43)=1(2212910436(2))(357)10436(43)=22129(357)10436(757)=122129(357)(3256522129(1))(757)=22129(1114)32565(757)=1(15238932565(4))(1114)32565(757)=152389(1114)32565(5213)=1152389(1114)(184954152389(1))(5213)=152389(6327)184954(5213)=1(337343184954(1))(6327)184954(5213)=337343(6327)184954(11540)=1337343(6327)(522297337343(1))(11540)=337343(17867)522297(11540)=1(1381937522297(2))(17867)522297(11540)=1381937(17867)522297(47274)=11381937(17867)(2838193821381937(205))(47274)=1381937(9709037)283819382(47274)=1\begin{aligned} 117 - 29(4) &= 1 \\ 117 - (380 - 117(3))(4) = 117(13) - 380(4) &= 1 \\ (1257 - 380(3))(13) - 380(4) = 1257(13) - 380(43) &= 1 \\ 1257(13) - (10436 - 1257(8))(43) = 1257(357) - 10436(43) &= 1 \\ (22129 - 10436(2))(357) - 10436(43) = 22129(357) - 10436(757) &= 1 \\ 22129(357) - (32565 - 22129(1))(757) = 22129(1114) - 32565(757) &= 1 \\ (152389 - 32565(4))(1114) - 32565(757) = 152389(1114) - 32565(5213) &= 1 \\ 152389(1114) - (184954 - 152389(1))(5213) = 152389(6327) - 184954(5213) &= 1 \\ (337343 - 184954(1))(6327) - 184954(5213) = 337343(6327) - 184954(11540) &= 1 \\ 337343(6327) - (522297 - 337343(1))(11540) = 337343(17867) - 522297(11540) &= 1 \\ (1381937 - 522297(2))(17867) - 522297(11540) = 1381937(17867) - 522297(47274) &= 1 \\ 1381937(17867) - (283819382 - 1381937(205))(47274) = 1381937(9709037) - 283819382(47274) &= 1 \\ \end{aligned}

Therefore, x=9709037x = 9709037 and y=47274y = 47274. From this, we can conclude that the modular inverse of 13819371381937 is 97090379709037 with respect to 283819382283819382. We can multiply this out to see that this in fact works. While this set of equations was exhausting to go through, it works and only requires basic arithmetic operations.

Chart Method

While the above gives a great amount of intuition for why the algorithm works and makes it easy to memorize, it is not that great for computation since you have to rewrite a ton of stuff. As a result, it is faster to use a chart to organize everything.

We can construct a chart as follows for the process of gcd(46,56)\gcd(46, 56).

Rowabrqxy
15646

To perform the first step of the algorithm, we have to fill in the columns a,b,r, and qa, b, r, \text{ and } q. The recurrence relation to calculate the values of the chart are listed below, where a term like aia_i represents the value in the column aa in the row ii.

a1=ab1=bai+1=bibi+1=riri=ai % biqi=ai/bixn1=1yn1=1xi=yi+1yi=xi+1yi+1qi\begin{aligned} & a_1 = a & b_1 = b \\ \\ & a_{i + 1} = b_i \\ & b_{i + 1} = r_{i} \\\\ & r_i = a_i \ \%\ b_i \\ & q_i = \lfloor{a_i / b_i}\rfloor \\\\ & x_{n - 1} = 1 & y_{n - 1} = -1 \\ \\ & x_{i} = y_{i + 1} \\ & y_{i} = x_{i + 1} - y_{i + 1} * q_{i} \end{aligned}

aa and bb represent our two numbers, rr represents the remainder (a % ba\ \%\ b) and qq represents the truncated quotient of the two (a/b)(\lfloor{a / b}\rfloor). After this, we transfer the value of bb to aa for the next row, and the value of rr to bb for the next row. Note that this is equivalent to the first step from the previously mentioned procedure, just in the form of a chart.

Rowabrqxy
15646101
2461064
310641
46421
54202

From this, we can find that the value of the gcd\gcd is the last non-zero value of rr, which is 22. Next, to fill in the values of xx and yy, we first start off with the values of 11 and 1-1 at the second to last row.

Rowabrqxy
15646101
2461064
310641
464211-1
54202N/AN/A

To compute the values for xx and yy for the row above it, we set the transfer the yy from below to the xx above. Then, to compute the above yy, we take the below value of xx and subtract from it the below value of yy multiplied by the upper value for qq. For instance, the new value of yy here would be

yabove=xbelowybelowqabove=1(1)1=2y_{\text{above}} = x_{\text{below}} - y_{\text{below}} * q_{\text{above}} = 1 - (-1) * 1 = 2

We can do this for the entire grid to get the following:

Rowabrqxy
15646101-911
24610642-9
310641-12
464211-1
54202N/AN/A

We can verify that these values work, as 956+1146=2-9 * 56 + 11 * 46 = 2, as we expected.

Dealing With Nonexistent Modular Inverses

As we mentioned before, if aa shares any factors with nn, the modular inverse of aa with respect to nn does not exist. This corresponds to the bottom left portion of the chart shown in the beginning of this post. However, there may still be equations that we need to solve, such as the following:

2x2(mod8)2x \equiv 2 \pmod{8}

In this case, we do not have a modular inverse for 22 as gcd(2,8)=21\gcd(2, 8) = 2 \neq 1. However, the reason why this modular inverse does not exist is actually that this equation has multiple solutions modulo 88. For instance, x1(mod8)x \equiv 1 \pmod{8} and x5(mod8)x \equiv 5 \pmod{8} both work for the above case!

The way we solve a problem like this is that if we want to divide by aa, we first want to find gcd(a,n)\gcd(a, n) and then divide everything by that value, including nn. For instance, in this case, gcd(2,8)=2\gcd(2, 8) = 2, so we divide everything by 22.

2x2(mod8)x1(mod4)2x \equiv 2 \pmod{8} \rightarrow x \equiv 1 \pmod{4}

This is our new equation, and this gives us our solutions for xx! Notice how they match up with the two that were mentioned earlier. However, it is not always the case that gcd(a,n)=a\gcd(a, n) = a, so it will not directly give you the solution as it did here. However, in those cases, you have divided out the shared factors, which means that the new aa and nn are now coprime! This means you can apply the modular inverse technique mentioned earlier to then solve that new equation.

However, all of this assumed that the right side of the equation was also divisible by gcd(a,n)\gcd(a, n). However, it can be shown that if this is not the case, then the equation has no solutions, which means we do not have to worry about this case.

As a final example, we will solve the following equation:

26x12(mod16)26x \equiv 12 \pmod{16}

We can first find gcd(26,16)=2\gcd(26, 16) = 2 from inspection and reduce the equation to the following;

13x6(mod8)13x \equiv 6 \pmod{8}

Then, we just need to find 131(mod8)13^{-1} \pmod{8}. To do this, we will apply the Euler's theorem method.

ϕ(8)=812=4\phi(8) = 8 * \dfrac{1}{2} = 4
131515ϕ(8)1535(mod8)13^-1 \equiv 5^-1 \equiv 5^{\phi(8) - 1} \equiv 5^{3} \equiv 5 \pmod{8}

Therefore, we can multiply both sides of the equation by 55 to obtain our answer:

x306(mod8)x \equiv 30 \equiv 6 \pmod{8}

If we wanted to, we could convert this back to mod 16\text{mod } 16 and obtain

x6,14(mod16)x \equiv 6, 14 \pmod{16}

With that, we're done!

Summary

All in all, division in modular arithmetic can be pretty messy, and it is pretty important to carefully consider what you're doing before you do anything. Additionally, when calculating modular inverses, it is very important to keep good bookkeeping and to constantly check that the equations being written still satisfy the relations they're supposed to. For instance, periodically checking while you're doing the extended Euclidean algorithm to make sure the values still sum to the gcd\gcd properly while substituting is pretty important for avoiding mistakes.