## 31.2-1

Prove that equations $\text{(31.11)}$ and $\text{(31.12)}$ imply equation $\text{(31.13)}$.

(Omit!)

## 31.2-2

Compute the values $(d, x, y)$ that the call $\text{EXTENDED-EUCLID}(899, 493)$ returns.

$(29, -6, 11)$.

## 31.2-3

Prove that for all integers $a$, $k$, and $n$,

$\gcd(a, n) = \gcd(a + kn, n)$.

• $\gcd(a, n) \mid \gcd(a + kn, n)$.

Let $d = \gcd(a, n)$, then $d \mid a$ and $d \mid n$.

Since

$$(a + kn) \mod d = a \mod d + k \cdot (n \mod d) = 0$$

and $d \mid n$, we have

$$d \mid \gcd(a + kn, n)$$

and

$$\gcd(a, n) \mid \gcd(a + kn, n).$$

• $\gcd(a + kn, n) \mid \gcd(a, n)$.

Suppose $d = \gcd(a + kn, n)$, we have $d \mid n$ and $d \mid (a + kn)$.

Since

$$(a + kn) \mod d = a \mod d + k \cdot (n \mod d) = a \mod d = 0,$$

we have $d \mid a$.

Since $d \mid a$ and $d \mid n$, we have

$$d \mid \gcd(a, n)$$

and

$$\gcd(a + kn, n) \mid \gcd(a, n).$$

Since

$$\gcd(a, n) \mid \gcd(a + kn, n)$$

and

$$\gcd(a + kn, n) \mid \gcd(a, n),$$

we have

$$\gcd(a, n) = \gcd(a + kn, n).$$

## 31.2-4

Rewrite $\text{EUCLID}$ in an iterative form that uses only a constant amount of memory (that is, stores only a constant number of integer values).

EUCLID(a, b)
while b != 0
t = a
a = b
b = t % b
return a


## 31.2-5

If $a > b \ge 0$, show that the call EUCLID$(a, b)$ makes at most $1 + \log_\phi b$ recursive calls. Improve this bound to $1 + \log_\phi(b / \gcd(a, b))$.

$$b \ge F_{k + 1} \approx \phi^{k + 1} / \sqrt{5}$$

$$k + 1 < \log_\phi \sqrt{5} + \log_\phi b \approx 1.67 + \log_\phi b$$

$$k < 0.67 + \log_\phi b < 1 + \log_\phi b.$$

Since $d \cdot a \mod d \cdot b = d \cdot (a \mod b)$, $\gcd(d \cdot a, d \cdot b)$ has the same number of recursive calls with $\gcd(a, b)$, therefore we could let $b' = b / \gcd(a, b)$, the inequality $k < 1 + \log_\phi(b') = 1 + \log_\phi(b / \gcd(a, b))$. will holds.

# 31.2-6

What does $\text{EXTENDED-EUCLID}(F_{k + 1}, F_k)$ return? Prove your answer correct.

• If $k$ is odd, then $(1, -F_{k-2}, F_{k - 1})$.
• If $k$ is even, then $(1, F_{k-2}, -F_{k - 1})$.

## 31.2-7

Define the $\gcd$ function for more than two arguments by the recursive equation $\gcd(a_0, a_1, \cdots, a_n) = \gcd(a_0, \gcd(a_1, a_2, \cdots, a_n))$. Show that the $\gcd$ function returns the same answer independent of the order in which its arguments are specified. Also show how to find integers $x_0, x_1, \cdots, x_n$ such that $\gcd(a_0, a_1, \ldots, a_n) = a_0 x_0 + a_1 x_1 + \cdots + a_n x_n$. Show that the number of divisions performed by your algorithm is $O(n + \lg (max \{a_0, a_1, \cdots, a_n \}))$.

Suppose

$$\gcd(a_0, \gcd(a_1, a_2, \cdots, a_n)) = a_0 \cdot x + \gcd(a_1, a_2, \cdots, a_n) \cdot y$$

and

$$\gcd(a_1, \gcd(a_2, a_3, \cdots, a_n)) = a_1 \cdot x' + \gcd(a_2, a_3, \cdots, a_n) \cdot y',$$

then the coefficient of $a_1$ is $y \cdot x'$.

EXTENDED-EUCLID(a, b)
if b == 0
return (a, 1, 0)
(d, x, y) = EXTENDED-EUCLID(b, a % b)
return (d, y, x - (a / b) * y)

EXTENDED-EUCLID-MULTIPLE(a)
if a.length == 1
return (a[0], 1)
g = a[a.length - 1]
xs = [1] * a.length
ys = [0] * a.length
for i = a.length - 2 downto 0
(g, xs[i], ys[i + 1]) = EXTENDED-EUCLID(a[i], g)
m = 1
for i = 1 to a.length
m *= ys[i]
xs[i] *= m
return (g, xs)


## 31.2-8

Define $\text{lcm}(a_1, a_2, \ldots, a_n)$ to be the least common multiple of the $n$ integers $a_1, a_2, \ldots, a_n$, that is, the smallest nonnegative integer that is a multiple of each $a_i$. Show how to compute $\text{lcm}(a_1, a_2, \ldots, a_n)$ efficiently using the (two-argument) $\gcd$ operation as a subroutine.

GCD(a, b)
if b == 0
return a
return GCD(b, a % b)

LCM(a, b)
return a / GCD(a, b) * b

LCM-MULTIPLE(a)
l = a[0]
for i = 1 to a.length
l = LCM(l, a[i])
return l


## 31.2-9

Prove that $n_1$, $n_2$, $n_3$, and $n_4$ are pairwise relatively prime if and only if

$\gcd(n_1n_2,n_3n_4) = \gcd(n_1n_3, n_2n_4) = 1.$

More generally, show that $n_1, n_2, \ldots, n_k$ are pairwise relatively prime if and only if a set of $\lceil \lg k \rceil$ pairs of numbers derived from the $n_i$ are relatively prime.

Suppose $n_1n_2 x + n_3n_4 y = 1$, then $n_1(n_2 x) + n_3(n_4 y) = 1$, thus $n_1$ and $n_3$ are relatively prime, $n_1$ and $n_4$, $n_2$ and $n_3$, $n_2$ and $n_4$ are the all relatively prime. And since $\gcd(n_1n_3, n_2n_4) = 1$, all the pairs are relatively prime.

General: in the first round, divide the elements into two sets with equal number of elements, calculate the products of the two set separately, if the two products are relatively prime, then the element in one set is pairwise relatively prime with the element in the other set. In the next iterations, for each set, we divide the elements into two subsets, suppose we have subsets $\{ (s_1, s_2), (s_3, s_4), \ldots \}$, then we calculate the products of $\{ s_1, s_3, \ldots \}$ and $\{ s_2, s_4, \ldots \}$, if the two products are relatively prime, then all the pairs of subset are pairwise relatively prime similar to the first round. In each iteration, the number of elements in a subset is half of the original set, thus there are $\lceil \lg k \rceil$ pairs of products.

To choose the subsets efficiently, in the $k$th iteration, we could divide the numbers based on the value of the index's $k$th bit.