A quick review of Lecture 13.
We can compute GCDs efficiently using the Euclidean algorithm. Invented in 300 BC!
Computing GCDs with the Euclidean algorithm.
Proof follows straightforwardly from the definition of GCD and divisibility.
Apply $\gcd{a}{b} = \gcd{b}{\mod{a}{b}}$ until you get $\gcd{a}{0} = a$.
Example implementation:
// Assumes a >= b >= 0.
public static int gcd(int a, int b) {
if (b == 0)
return a; // GCD(a, 0) = a
else
return gcd(b, a % b); // GCD(a, b) = GCD(b, a mod b)
}
In tableau form:
660 = | 5 * | 126 + | 30 |
126 = | 4 * | 30 + | 6 |
30 = | 5 * | 6 + | 0 |
Bézout’s theorem and the extended Euclidean algorithm.
We can extend Euclidean algorithm to find $s$ and $t$ in addition to computing $\gcd{a}{b}$.
$\gcd{35}{27} = 35s + 27t$.
$a$ $=$ | $q$ $*$ | $b$ $+$ | $r$ |
$35$ $=$ | $1$ $*$ | $27$ $+$ | $8$ |
$27$ $=$ | $3$ $*$ | $8$ $+$ | $3$ |
$8$ $=$ | $2$ $*$ | $3$ $+$ | $2$ |
$3$ $=$ | $1$ $*$ | $2$ $+$ | $1$ |
$\gcd{a}{b}$ | $\gcd{b}{\mod{a}{b}}$ | $r=\mod{a}{b}$ | ||
---|---|---|---|---|
$\gcd{35}{27}$ | $=$ | $\gcd{27}{\mod{35}{27}}$ | $=$ | $\gcd{27}{8}$ |
$=$ | $\gcd{8}{\mod{27}{8}}$ | $=$ | $\gcd{8}{3}$ | |
$=$ | $\gcd{3}{\mod{8}{3}}$ | $=$ | $\gcd{3}{2}$ | |
$=$ | $\gcd{2}{\mod{3}{2}}$ | $=$ | $\gcd{2}{1}$ | |
$=$ | $\gcd{1}{\mod{2}{1}}$ | $=$ | $\gcd{1}{0}$ |
$\gcd{35}{27} = 35s + 27t$.
$a$ $=$ | $q$ $*$ | $b$ $+$ | $r$ |
$35$ $=$ | $1$ $*$ | $27$ $+$ | $8$ |
$27$ $=$ | $3$ $*$ | $8$ $+$ | $3$ |
$8$ $=$ | $2$ $*$ | $3$ $+$ | $2$ |
$3$ $=$ | $1$ $*$ | $2$ $+$ | $1$ |
$r$ $=$ | $a$ $-$ | $q$ $*$ | $b$ |
$8$ $=$ | $35$ $-$ | $1$ $*$ | $27$ |
$3$ $=$ | $27$ $-$ | $3$ $*$ | $8$ |
$2$ $=$ | $8$ $-$ | $2$ $*$ | $3$ |
$1$ $=$ | $3$ $-$ | $1$ $*$ | $2$ |
$\gcd{35}{27} = 35s + 27t$.
$r$ $=$ | $a$ $-$ | $q$ $*$ | $b$ |
$8$ $=$ | $35$ $-$ | $1$ $*$ | $27$ |
$3$ $=$ | $27$ $-$ | $3$ $*$ | $8$ |
$2$ $=$ | $8$ $-$ | $2$ $*$ | $3$ |
$1$ $=$ | $3$ $-$ | $1$ $*$ | $2$ |
$r_i = r_{i-2} - q_i*r_{i-1}$ |
$r_0 = a = 35$ |
$r_1 = b = 27$ |
$r_2 = r_0 - q_2*r_1 = 8$ |
$r_3 = r_1 - q_3*r_2 = 3$ |
$r_4 = r_2 - q_4*r_3 = 2$ |
$r_5 = r_3 - q_5*r_4 = 1$ |
$1\ $ | $ = \ra{3} - 1 * \rb{2}$ | $r_5 = \ra{r_3} - q_5 * \rb{r_4}$. |
$ = 3 - 1 * (\ra{8} - 2 * \rb{3})$ | Plug in $r_4 = \ra{r_2} - q_4 * \rb{r_3}$. | |
$ = (-1) * \ra{8} + 3*\rb{3}$ | Combine $\ra{r_2}, \rb{r_3}$ terms. | |
$ = (-1) * 8 + 3*(\ra{27} - 3*\rb{8})$ | Plug in $r_3 = \ra{r_1} - q_3 * \rb{r_2}$. | |
$ = 3 * \ra{27} + (-10) * \rb{8}$ | Combine $\ra{r_1}, \rb{r_2}$ terms. | |
$ = 3 * 27 + (-10) * (\ra{35} - 1*\rb{27})$ | Plug in $r_2 = \ra{r_0} - q_2 * \rb{r_1}$. | |
$ = (-10) * \ra{35} + 13 * \rb{27}$ | Combine $\ra{r_0}, \rb{r_1}$ terms. |
Suppose $\gcd{a}{m} = 1$.
By Bézout’s theorem, there exist integers $s$ and $t$ such that $sa + tm = 1$.
In other words, $\mod{s}{m}$ is the multiplicative inverse of $\mod{a}{m}$ iff $\congruent{sa}{1}{m}$.
So, we can compute multiplicative inverses with the extended Euclidean algorithm. These inverses let us solve modular equations.
Solving modular equations with the extended Euclidean algorithm.
Solve: $\congruent{7x}{1}{26}$
① Compute GCD and keep the tableau.
② Solve the equations for $r$ in the tableau.
$a$ $=$ | $q$ $*$ | $b$ $+$ | $r$ |
$26$ $=$ | $3$ $*$ | $7$ $+$ | $5$ |
$7$ $=$ | $1$ $*$ | $5$ $+$ | $2$ |
$5$ $=$ | $2$ $*$ | $2$ $+$ | $1$ |
$r$ $=$ | $a$ $-$ | $q$ $*$ | $b$ |
$5$ $=$ | $26$ $-$ | $3$ $*$ | $7$ |
$2$ $=$ | $7$ $-$ | $1$ $*$ | $5$ |
$1$ $=$ | $5$ $-$ | $2$ $*$ | $2$ |
③ Back substitute the equations for $r$.
④ Solve for $x$.
Solve: $\congruent{7y}{3}{26}$
$\gcd{a}{m}=1$ if $m$ is prime and $0 < a < m$, so we can always solve modular equations for prime $m$.
$+$ | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|---|
0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
1 | 1 | 2 | 3 | 4 | 5 | 6 | 0 |
2 | 2 | 3 | 4 | 5 | 6 | 0 | 1 |
3 | 3 | 4 | 5 | 6 | 0 | 1 | 2 |
4 | 4 | 5 | 6 | 0 | 1 | 2 | 3 |
5 | 5 | 6 | 0 | 1 | 2 | 3 | 4 |
6 | 6 | 0 | 1 | 2 | 3 | 4 | 5 |
$*$ | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
2 | 0 | 2 | 4 | 6 | 1 | 3 | 5 |
3 | 0 | 3 | 6 | 2 | 5 | 1 | 4 |
4 | 0 | 4 | 1 | 5 | 2 | 6 | 3 |
5 | 0 | 5 | 3 | 1 | 6 | 4 | 2 |
6 | 0 | 6 | 5 | 4 | 3 | 2 | 1 |
A fast algorithm for computing $a^k\ \text{mod}\ m$.
How would you compute $\mod{78365^{81453}}{104729}$?
To keep the intermediate results small, we use fast modular exponentiation.
If $k = 2^i$, we can compute $\mod{a^k}{m}$ in just $i$ steps.
Note that $\congruent{\mod{a}{m}}{a}{m}$ and $\congruent{\mod{b}{m}}{b}{m}$. So, we have $\mod{ab}{m} = \mod{((\mod{a}{m})(\mod{b}{m}))}{m}$.
For example:
What if $k$ is not a power of 2?
Example implementation:
// Assumes a > 0, k >= 0, m > 0.
public static long fastModExp(long a, long k, long m) {
if (k == 0) { // k = 0
return 1;
} else if (k % 2 == 0) { // k is even
long tmp = fastModExp(a, k/2, m);
return (tmp * tmp) % m;
} else { // k is odd
long tmp = fastModExp(a, k-1, m);
return (a * tmp) % m;
}
}
$\mod{78365^{81453}}{104729} = 45235$
The fast exponentiation algorithm computes $\mod{a^k}{m}$ using $\leq 2\log k$ multiplications mod $m$.