-
5. The Extended Euclidean Algorithm
As the modulus, $m$, increases in size it quickly becomes impractical to use multiplication tables or trial and error to find inverses. The Extended Euclidean algorithm provides a significantly more efficient method for determining the inverse of an integer $a$ modulo $m$, when it exists.
We first show how the Extended Euclidean algorithm can be used to write the greatest common divisor (GCD) of two integers $a$ and $m$ as a linear combination of these integers. If we define $d = $gcd$(a, m)$ we seek integers $x$ and $y$ such that $ax + my = d$. In the special case when $d = 1$ we show how the value of $x$ in the linear combination represents the inverse of $a$ modulo $m$.
📹 The Extended Euclidean algorithm
Example 25
(i). Use the Euclidean algorithm to find $d = \;$gcd$(141, 63)$.
(ii). Use the Extended Euclidean algorithm to write $d$ as a linear combination of $141$ and $63$ in the form $63x + 141y = d$. State the values of $x$ and $y$.
Solution
(i). Apply the Euclidean algorithm to find $d = \;$gcd$(141, 63)$.
$141 = 2 \times 63 + 15$
$63 = 4 \times 15 + 3$
$15 = 5 \times 3 + 0$
As the remainder is $0$ we stop here to find $d = \;$gcd$(141, 63) = 3$.
Exercise: Use prime factorisation to verify that the GCD of $141$ and $63$ is $3$.
To express $d$ as a linear combination of $141$ and $63$ start by rearranging the above in terms of the remainders:
$15 = 141 - 2 \times 63$
$ 3 = 63 - 4 \times 15$
Now use back-substitution to obtain the result:
$3 = 63 - 4 \times 15$
$= 1 \times 63 - 4(141 - 2 \times 63)$
$= 1 \times 63 - 4 × 141 + 8 \times 63$
$= 9 \times 63 - 4 \times 141$
Hence, $63 \times 9 + 141 \times (-4) = 3 = \;$gcd$(141, 63)$.
Using the Extended Euclidean algorithm the GCD $(3)$ has been expressed as a linear combination, i.e. $63x + 141y = 3$, where $x = 9$ and $y = -4$ .
We know that if $a$ and $m$ are relatively prime then gcd$(a, m) = 1$ and there exist integers $x$ and $y$ such that $ax + my = 1$ where $x$ is the multiplicative inverse of $a$ modulo $m$. The process is illustrated with an example.
End of Example 25Example 26
(i). Show that $387$ and $479$ are relatively prime.
(ii). Determine a linear combination of $387$ and $479$ that equals $1$.
(iii). Find the multiplicative inverse of $387$ modulo $479$.
Solution
(i). We apply the Euclidean algorithm to show that gcd$( 479, 387) = 1$.
$479 = 1 \times 387 + 92$
$387 = 4 \times 92 + 19$
$92 = 4 \times 19 + 16$
$19 = 1 \times 16 + 3$
$16 = 5 \times 3 + 1$
$3 = 3 \times 1 + 0$
Stop here to find gcd$( 479, 387) = 1$ , so that $387$ and $479$ are relatively prime.
(ii). First write the above in terms of the remainders:
$92 = 479 - 1 \times 387$
$19 = 387 - 4 \times 92$
$16 = 92 - 4 \times 19$
$3 = 19 - 1 \times 16$
$1 = 16 - 5 × 3 $
Express the GCD as a linear combination of $387$ and $479$ using back-substitution:
$1 = 16 - 5 \times 3$
$= 1 \times 16 - 5(19 - 1 \times 16)$
$= 1 \times 16 - 5 \times 19 + 5 \times 16$
$= 6 \times 16 - 5 \times 19$
$= 6(92 - 4 \times 19) - 5 \times 19$
$= 6 \times 92 - 24 \times 19 - 5 \times 19$
$= 6 \times 92 - 29 \times 19$
$= 6 \times 92 - 29(387 - 4 \times 92)$
$= 6 \times 92 - 29 \times 387 + 116 \times 92$
$= 122 \times 92 - 29 \times 387$
$= 122(479 - 1 \times 387) - 29 \times 387$
$= 122 \times 479 - 122 \times 387 - 29 \times 387$
$= 122 \times 479 - 151 \times 387$
Hence, $387 \times (-151) + 479 \times 122 = 1$
Using the Extended Euclidean algorithm the GCD $(1)$ of $387$ and $479$ has been expressed as a linear combination, i.e. $387x + 479y = 1$, where $x = -151$ and $y = 122$.
You should attempt the exercises available 🔗 here
(iii). We need to find the multiplicative inverse of $387$ modulo $479$. Hence, we require an $x$ such that $387x \equiv 1\, mod (479)$.
From part (ii) we have that $x = -151$.
To obtain a positive value we simply add the modulus $(479)$ to $-151$ giving $328$.
Then, $-151 \equiv 328\, mod(479)$ so that $387 \times 328 \equiv 1\, mod(479)$
Hence, the multiplicative inverse of $387$ modulo $479$ is $x = 328$.
Check: $387 \times 328 = 126936 \equiv 1\, mod(479)$ as required.
Here $x$ is a solution of the congruence equation $387x \equiv 1\, mod(479)$. We shall investigate congruence equations in Section 7.
Click on this link for more practice with GCD, linear combinations and inverses. 🔗 Find the Greatest common Divisor
End of Example 26 -
6. Further modular arithmetic
One of the most practical applications of modular arithmetic is in reducing calculations involving large integers to more manageable calculations with smaller integers.
If we take property (v) in Section 3.2 and express it in a slightly different form we obtain
$ab ( mod\, m ) = [( a\, mod\, m )( b\, mod\, m )] ( mod\, m )$
or equivalently
$ab \equiv [( a\, mod\, m )( b\, mod\, m )]( mod \,m )$
Also, by defining $k$ to be a positive integer we can write
$a^k( mod\, m ) = [ a\, (mod\, m ) ]^k(mod\, m)$
or
$a^k \equiv [ a ( mod\, m )]^k( mod\, m )$.
Example 27
(i). Determine the remainder when $376 \times 427$ is divided by $9$.
(ii). Determine the remainder when $23^8$ is divided by $7$.
Solution
(i). $(376 \times 427)(mod\, 9) = [( 376\, mod\, 9 ) \times ( 427\, mod\, 9 )]( mod\, 9)$
$= [7 \times 4 ]( mod\, 9 ) = 28( mod\, 9 ) = 1$.
(ii). $23^8( mod\, 7 ) = [ 23( mod\, 7 )]^8( mod\, 7)$
$= 2^8( mod\, 7 )$
$ = 256( mod\, 7 ) = 4$.
End of Example 276.1. Repeated squaring
Another technique often used to find remainders in calculations involving very large numbers, such as in RSA ciphers, is repeated squaring, also called modular exponentiation. For example, if we were required to compute the remainder of $3^{209}$ on division by $44$ we would find that neither of the methods in Example 27 would really help. In this particular case we could of course use a computer to evaluate $3^{209}$, divide by $44$ and obtain the remainder. However, as the exponent $k$, and hence the expression $a^k$, becomes extremely large, as is common in many modern ciphers, it is totally unrealistic to compute $a^k$ directly. Repeated squaring enables us to perform the calculation.
Now watch the following video:
Example 28
What is the remainder when $3^{209}$ is divided by $44$?
Solution
We need to calculate $3^{209}( mod\, 44 )$ .
Step 1
Write the exponent $(209)$ as a sum of powers of $2$:
$209 = 128 + 64 + 16 + 1.$
We note here that every positive integer can be expressed as a sum of distinct powers of $2$.
Step 2
Working modulo $44$, we repeatedly square $3$ to find $3^{2^k}$ for all $k$ such that $2^k \le 209$.
$k = 0: 3^{2^0} \equiv 3^1 \equiv 3( mod\, 44 )$
$k = 1: 3^{2^1} \equiv 3^2 \equiv 9( mod\, 44 )$
$k = 2: 3^{2^2} \equiv 3^4 \equiv ( 3^2 )^2 \equiv 9^2 \equiv 81 \equiv 37( mod\, 44 )$
$k = 3: 3^{2^3} \equiv 3^8 \equiv ( 3^4 )^2 \equiv 37^2 \equiv 1369 \equiv 5( mod\, 44 )$
$k = 4: 3^{2^4} \equiv 3^{16} \equiv ( 3^8 )^2 \equiv 5^2 \equiv 25( mod\, 44 )$
$k = 5: 3^{2^5} \equiv 3^{32} \equiv ( 3^{16} )^2 \equiv 25^2 \equiv 625 \equiv 9( mod\, 44 )$
$k = 6: 3^{2^6} \equiv 3^{64} \equiv ( 3^{32} )^2 \equiv 9^2 \equiv 81 \equiv 37( mod\, 44 )$
$k = 7: 3^{2^7} \equiv 3^{128} \equiv ( 3^{64} )^2 ≡ 37^2 ≡ 1369 ≡ 5( mod\, 44 )$.
We stop here as doubling the exponent again produces an exponent $(256)$ greater than the original $(209)$. At each step we simply square the output from the previous step so that, as we are working modulo $44$, we never have to work with a number larger than $43^2$. Furthermore, note that in this case the remainder cycles $(9, 37, 5, 25)$ and so we do not have to compute it at each subsequent step thereby making the calculations relatively straightforward.
Step 3
Since $209 = 128 + 64 + 16 + 1$ we have
$3^{209} \equiv 3^{128 + 64 + 16 + 1}( mod\, 44 )$
$\equiv 3^{128} \times 3^{64} \times 3^{16} × 3^1( mod\, 44 )$
$\equiv 5 \times 37 \times 25 \times 3( mod\, 44 )$
$\equiv 13875( mod\, 44 )$
$\equiv 15( mod\, 44 )$
Hence, $3^{209}( mod\, 44 ) \equiv 15\;(mod\,44)$ and the remainder when $\;3^{209}$ is divided by $44$ is $15$ .
Note that when evaluating $5 \times 37 \times 25 \times 3( mod\, 44 )$ we could split the calculation so that we are working with “manageable” numbers. For example, we could proceed as follows:
$5 \times 37 \times 25 \times 3( mod\, 44 )$
$\equiv [(5 \times 37 \times 3\, mod\, 44 )( 25\, mod\, 44 )]( mod\, 44 )$
$\equiv [( 555\, mod\, 44 )( 25\, mod\, 44 )]( mod\, 44 )$
$\equiv [ 27 \times 25 ]( mod\, 44 )$
$\equiv 675( mod\, 44 )$
$\equiv 15( mod\, 44 )$.
End of Example 28 -
7. Linear Congruence Equations
A linear congruence equation has the form $ax \equiv b (mod\, m)$ where $x$ is the unknown and $a, b $ and $m$ are integers with $m > 0$. If the equation has a solution, $x_0$ say, then there will be infinitely many solutions of the form $x = x_0 + km$ where $k$ is any integer. Note that any solution, $x$, must be such that $m \mid ( ax – b )$. We have actually met congruence equations before when working with the Euclidean algorithm.
Consider the congruence equation, $ax \equiv b ( mod\, m )$. Let $d$ be the GCD of $a$ and $m$, i.e. $d =\;$ gcd$( a, m )$, then:
(i). If $d \mid b$ the equation has $d$ solutions.
(ii). If $d \nmid b$ the equation has no solution.
We first look at linear congruence equations of the form $ax \equiv 1 ( mod\, m )$ , where $a \neq 0 ( mod\, m )$, and then consider the more general equation $ax \equiv b ( mod\, m )$ for $b > 1$.
7.1. Case 1: $ax \equiv 1(mod\, m)$
The linear congruence equation $ax \equiv 1 ( mod\, m )$ has a unique solution modulo $m$ if $a$ and $m$ are relatively prime, i.e. gcd$(a, m) = 1$ ; otherwise it has no solution.
Example 29
If possible solve each of the following congruence equations. If no solution exists, explain why not.
(i). $3x \equiv 1( mod\, 5 )$
(ii). $3x \equiv 1( mod\, 6 )$.
Solution
(i). $d = \;$gcd$(3, 5) = 1$ and so the equation has a unique solution.
As the modulus is small (5) we can test each of the numbers in $\mathbb{Z}_5 = \{0, 1, 2, 3, 4\}$ by substituting for $x$.
The equation requires that $5 \mid (3x - 1)$ .
We form the table below and test each of the numbers $0, 1, 2, 3$ and $4$ in turn.
$x$ $3x - 1$ Does $5 \mid (3x - 1)$? 0 -1 No 1 2 No 2 5 Yes 3 8 No 4 11 No The unique solution modulo 5 is $x = 2$ , since $5 \mid (3 \times 2 - 1)$ , i.e. $5 \mid 5$.
(The general solution is $2 + 5 k$ for $k \in \mathbb{Z}$)
Note: An alternative approach is to find the multiplicative inverse of $3$ in $\mathbb{Z}_5$ and multiply both sides of the equation by this inverse value.
We found earlier that in $\mathbb{Z}_5, 3^{-1} = 2$, see the multiplication table for $\mathbb{Z}_5$ in section 4.2.1.
Multiplying the equation through by $2$ gives, $x \equiv 2( mod\, 5 )$.
(ii). $d =\; $gcd$(3, 6) = 2$ , and $2 \nmid 1$ so the equation has no solution.
Alternatively, as the modulus is small $(6)$ we can test each of the numbers in $\mathbb{Z}_6 = \{0, 1, 2, 3, 4, 5\}$ to find that there is no value of $x$ that satisfies $6 \mid (3x - 1)$.
$x$ $3x - 1$ Does $6 \mid (3x - 1)$? 0 -1 No 1 2 No 2 5 No 3 8 No 4 11 No 5 14 No Another approach is to note that the multiplication table for $\mathbb{Z}_6$ shows that $3$ does not have a multiplicative inverse in $\mathbb{Z}_6$ and so the equation has no solution.
When the modulus in a congruence equation is large it becomes unrealistic to test individual values of $x$. In such cases the Euclidean Algorithm is employed to find $d =\; $gcd$(a, m)$ and then, if $d = 1$ , back-substitution is applied to arrive at an expression of the form $ax_0 + my_0 = 1$ where $x = x_0$ is a solution of the equation $ax \equiv 1( mod\, m )$.
End of Example 29Example 30
Solve the congruence equation $387x \equiv 1 (mod\, 479)$ or show that there is no solution.
Solution
See Example 26 where we already solved this equation to find that the unique solution modulo $479$ is $x = 328$.
End of Example 30Example 31
Solve the congruence equation $213x \equiv 1 (mod\, 243)$ or show that there is no solution.
Solution
Use either the Euclidean algorithm or prime factorisation to show that $d =\; $gcd$(243, 213) = 3$ and as $3 \nmid 1$ the equation has no solution.
End of Example 317.2. Case 2: $ax \equiv b(mod\, m), b \neq 1$
Theorem
The linear congruence equation $ax \equiv b ( mod\, m )$ has a unique solution if $a$ and $m$ are relatively prime, i.e. $d =\; $gcd$(a, m) = 1$. Furthermore, if $x_0$ is the unique solution to $ax \equiv 1 (mod\, m )$ then $x = bx_0 ( mod\, m )$ is the unique solution to $ax \equiv b ( mod\, m )$ .
Example 32
If possible solve the following congruence equations:
(i). $3x \equiv 4( mod\, 5 )$.
(ii). $387x \equiv 63 ( mod\, 479 )$ .
Solution
(i). In Example 29(i) we showed that $x = 2$ is the unique solution modulo $5$ to the equation $3x \equiv 1( mod\, 5 )$. By the above theorem the unique solution $3x \equiv 4(mod\, 5)$ is therefore given by $x \equiv ( 4 \times 2 ) \equiv 8 \equiv 3( mod\, 5 )$.
In this example, as the modulus is small, we could have simply tested the values from $0$ to $4$ to show that the only value in $\mathbb{Z}_5$ for which $5 \mid ( 3x - 4 )$ is $x = 3$ and hence the only solution to $3x \equiv 4( mod\, 5 )$ . Alternatively, we could use the Cayley multiplication table to find $3^{-1} = 2$, and multiply the equation through by $2$ to obtain $x \equiv 8 \equiv 3( mod\, 5 )$.
(ii). In Examples 26 and 30 we showed that $x = 328$ is the unique solution modulo $479$ to the equation $387x \equiv 1( mod \,479 )$.
By the above theorem the unique solution modulo $479$ to $387x \equiv 63( mod\, 479 )$ is therefore given by $x \equiv ( 63 \times 328 )(mod\, 479 ) \equiv 67$.
Now watch the video at the link below.
📹 Solving Congruence Equations
End of Example 32 -
THE REMAINDER OF THE MATERIAL IS OPTIONAL.
We now look at congruence equations, $ax \equiv b ( mod\, m )$ where $b \neq 1$ and $d =$ gcd$( a, m ) > 1$.
Before we proceed we need to define the Simplification Law which states:
If $d$ divides $a, b$ and $m$ then $a \equiv b(mod \, m) \Rightarrow {\Large\frac{a}{d}} \equiv {\Large\frac{b}{d}}\Big(mod {\Large\frac{m}{d}} \Big)$
We shall also need the following theorem.
Theorem
Consider the linear congruence equation $ax \equiv b ( mod\, m )$ with $b \neq 1$ and $d =\; $gcd$( a, m ) > 1$.
(i). if $d \nmid b$ then the equation has no solution.
(ii). if $d \mid b$ then the equation has $d$ solutions which are all congruent modulo $M$ to the unique solution of
$Ax \equiv B ( mod\, M )$ where $A = {\Large\frac{a}{d}},\, B = {\Large\frac{b}{d}},\, M = {\Large\frac{m}{d}}$.
Note that $Ax \equiv B ( mod\, M )$ has a unique solution as gcd$( A, M ) = 1$ .
We illustrate the above results through two examples.
Example 33
If possible, solve $102x \equiv 96 ( mod \, 399 )$. (1)
Solution
Use the Euclidean algorithm to find $d =\; $gcd$( 399, 102 )$
$399 = 3 \times 102 + 93$
$102 = 1 \times 93 + 9$
$93 = 10 \times 9 + 3$
$ 9 = 3 × 3 + 0$.
Hence, $d =\; $gcd$( 399, 102 ) = 3$ and $3 \mid 96$, so there are $3$ solutions to the equation.
Now apply the Simplification Law to Eq (1) (as $3$ divides $102, 96$ and $399$) to obtain:
$34x \equiv 32 ( mod\, 133 )$.(2)
To solve Eq. (2) we first apply the Euclidean Algorithm to calculate $d =\; $gcd$( 133, 34 )$, which we know by the above theorem to be $1$ as $34$ and $133$ must be relatively prime:
$133 = 3 \times 34 + 31$
$34 = 1 \times 31 + 3$
$31 = 10 \times 3 + 1$
$3 = 3 \times 1 + 0$.
So, gcd$( 133, 34 ) = 1$ and as expected there is a unique solution to Eq. (2).
Now apply the Extended Euclidean algorithm to solve,
$34x \equiv 1( mod\, 133 )$.(3)
Rearrange the above in terms of the remainder:
$31 = 133 - 3 \times 34$
$3 = 34 - 1 \times 31$
$1 = 31 - 10 \times 3$.
Express the GCD as a linear combination of 34 and 133 using back-substitution:
$1 = 31 - 10 \times 3$
$= 1 \times 31 - 10 \times ( 34 - 1 \times 31 )$
$= 1 \times 31 - 10 \times 34 + 10 \times 31$
$= 11 \times 31 - 10 \times 34$
$= 11 \times (133 - 3 \times 34) - 10 \times 34$
$= 11 \times 133 - 33 \times 34 - 10 \times 34$
$= 11 \times 133 - 43 \times 34$
Hence, $34 \times (-43) + 133 \times 11 = 1$.
Using the Extended Euclidean algorithm the GCD (1) of $34$ and $133$ has been expressed as a linear combination, i.e. $34x + 133y = 1$, where $x = -43$ and $y = 11$.
Now, $-43\; mod(133) \equiv 90$ and so, $x = 90$ is the unique solution modulo $133$ to Eq. (3)
To find the unique solution to Eq. (2), which is also a solution to Eq. (1), we calculate:
$ x = (32 \times 90)(mod\, 133 ) = 87.$
The other solutions to Eq. (1) are found by adding $d - 1 = 2$ multiples of $133$ to $x = 87$.
We have,
$x = 87 + 1 \times 133 = 220$ and
$x = 87 + 2 \times 133 = 353$.
The three solutions modulo $399$ to Eq. (1) are therefore $x=87$, $x=220$ and $x=353$.
End of Example 33Example 34
If possible, solve, $91x \equiv 193( mod\, 299 )$.
Solution
Applying Euclid’s Algorithm gives $d =\; $gcd$( 299, 91 ) = 13$ but $13 \nmid 193$ and so there are no solutions to the equation.
End of Example 34 -
Summary
- In this section we have continued our work on elementary number theory. You should now be able to:
- Apply the Extended Euclidean algorithm to write the GCD of two integers $a$ and $m$ as a linear combination of these integers.
- Apply the Extended Euclidean algorithm to determine the inverse of an integer $a$ modulo $m$, when it exists.
- Use the method of repeated squaring (modular exponentiation) to find remainders in calculations involving very large numbers.
- Solve specific types of linear congruence equations.