For the complete documentation index, see llms.txt. This page is also available as Markdown.

31. Number-Theoretic Algorithms

Exercises

31.1-1

Based on Theorem 31.1 (Division theorem), we have

c=c/aa+r=(a+b)/aa+r=1a+r,c=\lfloor c/a\rfloor a+r=\lfloor (a+b)/a\rfloor a+r=1\cdot a+r,

since a>b>0a>b>0. Furthermore, c=a+b    r=b=c ⁣modac=a+b \implies r=b=c \!\mod a.

31.1-2

Read Euclid's proof on Wikipedia.

31.1-3 🌟

We have ab    b=paa \mid b \implies b=pa and bc    c=qbb\mid c\implies c=qb for integers pp and qq. Therefore, c=(pq)a    acc=(pq)a\implies a\mid c.

31.1-4

The only divisors of pp are 1 and pp. No divisor of kk except 1 divides pp, since k<p    pkk<p \implies p \nmid k. Therefore, gcd(k,p)=1\gcd⁡(k, p)=1.

31.1-5

We start from the fact that gcd(a,n)=1\gcd(a,n)=1, thus

1=ax+ny(by Theorem (3.12))b=abx+nby(after multiplying both sides by b)b=nkx+nby(nab    ab=kn)b=n(kx+by)nb.\begin{align*} 1 &= ax+ny && \text{(by Theorem (3.12))} \\ b &= abx +nby && \text{(after multiplying both sides by } b \text{)} \\ b &= nkx+nby && \text{(} n \mid ab \implies ab=kn \text{)} \\ b &= n(kx+by) \\ &\therefore n|b. \end{align*}

31.1-6 🌟

Lemma 1

For any integers a1,a2,,ana_1,a_2,\dots,a_n and pp, if gcd(ai,p)=1\gcd(a_i,p)=1 for all i=1,2,,ni=1,2,\dots,n, then gcd(i=1nai,p)=1\gcd(\prod_{i=1}^n a_i,p)=1.

Proof We prove the lemma by induction on nn. The base case n=2n=2 trivially holds by Theorem 31.6. For n>2n>2, we have

gcd(ai,p)=1(for all i=1,2,,n)    gcd(i=1n1ai,p)=1gcd(an,p)=1(by the inductive hypothesis)    gcd(i=1nai,p)=1(by Theorem (3.16)).\begin{align*} \gcd(a_i, p) &=1 && \text{(for all } i=1,2,\dots,n \text{)} \\ &\implies \gcd(\prod_{i=1}^{n-1}a_i, p)=1 \land \gcd(a_n,p)=1 && \text{(by the inductive hypothesis)} \\ &\implies \gcd(\prod_{i=1}^na_i, p)=1 && \text{(by Theorem (3.16))}. \end{align*}

According to the principles of mathematical induction, we can conclude that the lemma holds for all n2n \ge 2. \blacksquare

(pk)k!(pk)!=p(p1)!    p((pk)k!(pk)!).\binom{p}{k} \cdot k!(p-k)! = p \cdot (p-1)! \implies p \mid \left( \binom{p}{k} \cdot k!(p-k)! \right).

Based on Exercise 31.1-4 and Lemma 1, for 0<k<p0<k<p, gcd(k!,p)=1\gcd(k!,p)=1 and gcd((pk)!,p)=1\gcd((p-k)!,p)=1, so gcd(k!(pk)!,p)=1\gcd(k!(p-k)!, p) = 1. By Theorem 31.7 we can conclude that p(pk)p \mid \binom{p}{k}. Thus, in a binomial expansion all terms except the first and last one are divisible by pp, which concludes the proof.

31.1-7

We successively apply the Division theorem, thus

x=qb+(x ⁣ ⁣ ⁣ ⁣modb)=qka+(x ⁣ ⁣ ⁣ ⁣modb)(ab    b=ka)=qka+qa+((x ⁣ ⁣ ⁣ ⁣modb) ⁣ ⁣ ⁣ ⁣moda)=(qk+q)a+((x ⁣ ⁣ ⁣ ⁣modb) ⁣ ⁣ ⁣ ⁣moda)(x ⁣ ⁣ ⁣ ⁣modb) ⁣ ⁣ ⁣ ⁣moda=x ⁣ ⁣ ⁣ ⁣moda,\begin{align*} x &= q'b + (x \!\!\!\!\mod b) \\ &= q'ka + (x \!\!\!\!\mod b) && \text{(} a\mid b \implies b=ka \text{)} \\ &= q'ka + q''a + ((x \!\!\!\!\mod b) \!\!\!\!\mod a) \\ &= (q'k+q'')a+((x \!\!\!\!\mod b) \!\!\!\!\mod a) \\ &\therefore (x \!\!\!\!\mod b) \!\!\!\!\mod a = x \!\!\!\!\mod a, \end{align*}

because 0((xmodb)moda)<a0 \le ((x \bmod b) \bmod a) < a, so it represents a unique remainder modulo aa.

For the second part, we get

x=y ( ⁣ ⁣ ⁣ ⁣ ⁣ ⁣modb)x ⁣ ⁣ ⁣ ⁣modb=y ⁣ ⁣ ⁣ ⁣modb(x ⁣ ⁣ ⁣ ⁣modb) ⁣ ⁣ ⁣ ⁣moda=(y ⁣ ⁣ ⁣ ⁣modb) ⁣ ⁣ ⁣ ⁣modax ⁣ ⁣ ⁣ ⁣moda=y ⁣ ⁣ ⁣ ⁣moda(by the first part)x=y ( ⁣ ⁣ ⁣ ⁣ ⁣ ⁣moda).\begin{align*} x &= y \space(\!\!\!\!\!\! \mod b) \\ x \!\!\!\!\mod b &= y \!\!\!\!\mod b \\ (x \!\!\!\!\mod b) \!\!\!\!\mod a &= (y \!\!\!\!\mod b) \!\!\!\!\mod a \\ x \!\!\!\!\mod a &= y \!\!\!\!\mod a && \text{(by the first part)} \\ x &= y \space(\!\!\!\!\!\! \mod a). \end{align*}

31.1-8

Assume that multiplication of two β\beta-bit integers requires O(β2)O(\beta^2) time and equality test of two β\beta-bit integers requires O(β)O(\beta) time. The algorithm below only showcases an existence of a solution in time polynomial in β\beta without claiming anything about its asymptotic optimality. As a matter of fact, it is far from being optimal.

The smallest candidate is a=2a=2 and 2β>n    k[2,β)2^\beta>n \implies k \in [2,\beta). Therefore, we iterate over all possible exponents kk in increasing order and see if the kkth root of nn is an integer.

We can leverage binary search over the interval [2,n)[2,n) in searching for this root. We iteratively raise each candidate aa to the kkth power by multiplying the current product with aa (initially setting it to 1). In each iteration, we also check if this product is greater than nn, so that we stop as soon as possible. This stage demands O(β)(O(β2)+O(β))=O(β3)O(\beta)(O(\beta^2)+O(\beta))=O(\beta^3) time. In case of a match, we finish the search, otherwise, recurse into the lower or upper subinterval depending on whether ak>na^k>n or not, respectively. If the subinterval becomes empty, then we declare an unsuccessful attempt to find the root for the given exponent.

All in all, we can determine whether a given β\beta-bit integer nn is a nontrivial power in O(β5)O(\beta^5) time.

31.1-9

Proof of 31.6

By definition, gcd(a,b)\gcd(a,b) is the largest element in the set D(a,b)D_{(a,b)} of the common divisors of aa and bb. Analogously, gcd(b,a)\gcd(b,a) is the largest element in the set D(b,a)D_{(b,a)} of the common divisors of bb and aa. We know that D(a,b)=D(b,a)D_{(a,b)}=D_{(b,a)} and there cannot be two different largest element in a set. Therefore, gcd(a,b)=gcd(b,a)\gcd(a,b) = \gcd(b,a) .

Proof of 31.7

d1=gcd(a,b)    (d1a)(d1b)d_1=\gcd(a,b) \implies (d_1\mid a) \land (d_1\mid b), but this also means that d1(a)d_1\mid (-a), hence d1d2=gcd(a,b)d_1 \le d_2=\gcd(-a,b). With similar reasoning we can conclude that d2d1d_2 \le d_1, therefore d1=d2d_1=d_2.

Proof of 31.8

gcd(a,b)=gcd(b,a)(by property (31.6))=gcd(b,a)(by property (31.7))=gcd(a,b)(by property (31.6))=gcd(a,b)(by property (31.7))=gcd(b,a)(by property (31.6))=gcd(b,a)(by property (31.7))=gcd(a,b)(by property (31.6))gcd(a,b)=gcd(a,b).\begin{align*} \bm{\gcd⁡(a,b)}&=\gcd⁡(b,a) && \text{(by property (31.6))} \\ &=\gcd⁡(−b,a) && \text{(by property (31.7))} \\ &=\bm{\gcd⁡(a,-b)} && \text{(by property (31.6))} \\ &=\bm{\gcd⁡(−a,-b)} && \text{(by property (31.7))} \\ &=\gcd⁡(−b,−a) && \text{(by property (31.6))} \\ &=\gcd⁡(b,−a) && \text{(by property (31.7))} \\ &=\bm{\gcd⁡(−a,b)} && \text{(by property (31.6))} \\ &\therefore \gcd⁡(a,b)=\gcd⁡(|a|,|b|). \end{align*}

Proof of 31.9

By property (31.8) we have that gcd(a,0)=gcd(a,0)\gcd⁡(a,0)=\gcd⁡(|a|,0). Any common divisor of a|a| and 0 cannot be larger than a|a|, so gcd(a,0)=a\gcd⁡(a,0)=|a| (any integer divides 0). As a side note, if a=0a=0 then gcd(0,0)=0=a\gcd⁡(0,0)=0=|a|.

Proof of 31.10

By property (31.8) we have that gcd(a,ka)=gcd(a,ka)=gcd(a,ka)\gcd⁡(a,ka)=\gcd⁡(|a|,|ka|)=\gcd⁡(|a|,|k||a|). We have the following three cases:

  • If k=0k=0 then gcd(a,0)=a\gcd⁡(a,0)=|a| by property (31.9).

  • If a=0a=0 then gcd(0,0)=0=a\gcd(0,0)=0=|a|.

  • Let a0k0a \neq 0 \land k \neq 0. We know that a|a| divides itself, and a|a| divides ka|k||a|. Because a|a| is a common divisor, and no divisor of a|a| can be strictly greater than a|a|, it must be the greatest common divisor. Therefore, gcd(a,ka)=a\gcd(|a|, |k||a|) = |a|.

31.1-10 🌟

Let d1=gcd(a,gcd(b,c))d_1=\gcd(a,\gcd(b,c)) and d2=gcd(gcd(a,b),c)d_2=\gcd(\gcd(a,b),c). We prove the statement from the book by showing that d1d2d_1\mid d_2 and d2d1 d_2\mid d_1 , thus by equation (31.5) they must be equal, as both of them are nonnegative.

We know that d1ad1gcd(b,c)d_1\mid a \land d_1\mid \gcd(b,c), which entails that d1bd1cd_1\mid b \land d_1\mid c. The fact that d1d_1 divides both aa and bb implies that it also divides gcd(a,b)\gcd(a,b) by Corollary 31.3. Therefore, d1d2d_1\mid d_2 by applying again Corollary 31.3.

Using the same reasoning we can conclude that d2d1d_2\mid d_1, which finishes the proof about associativity of the gcd\gcd operator.

★ 31.1-11

You can read the proof here that uses only facts mentioned in the book.

31.1-12

The O(β2)O(\beta^2) time algorithm shown here also handles the edge case when a divisor is larger than a number to be divided.

31.1-13

We follow the hint from the book. The main idea is to split a β\beta-bit binary number and obtain the top and bottom halves of the result with separate recursions. The divide step must enable an easy assembly of these partial results. Since we are converting into a decimal representation it makes sense to split according to powers of 10. The key insight is to precompute these powers (in binary) to facilitate efficient splitting.

Precomputation is a powerful technique in algorithm design. The steps to accomplish this are laid out below:

1

Configure parameters

Calculate the total number of decimal digits D=βlg102D=\lceil\beta \lg_{10}2\rceil, set the recursion depth L=lgDL=\lceil\lg D\rceil, and create 1-indexed lookup tables SS (stores split points) and PP (stores powers of 10) as static arrays of size LL.

2

Fill the lookup tables

For each level i=1,2,,Li=1,2,\dots,L:

  1. Determine the split point Si=D/2iS_i=\lceil D/2^i \rceil (half the digits at level ii).

  2. Compute Pi=10SiP_i=10^{S_i} using exponentiation by squaring.

Each PiP_i has Θ(β/2i)\Theta(\beta/2^i) bits, so computing it demands O(M(β/2i)lgβ)O(M(\beta /2^i)\lg \beta) time. The total time is i=1LO(M(β/2i)lgβ)=O(i=1LM(β/2i)lgβ)=O(M(β)lgβ)\sum_{i=1}^L {O(M(\beta /2^i)\lg \beta)}=O(\sum_{i=1}^L {M(\beta /2^i)\lg \beta)}=O(M(\beta)\lg \beta) assuming MM satisfies the properties of standard multiplication algorithms, like, being non-decreasing as well as the sum of MM over geometrically decreasing sizes is dominated by M(β)M(\beta) (sort of weak regularity).

Recursive conversion function

Assume that handling decimal representation is not influencing the asymptotical behavior of our main routine. For example, concatenating strings (and padding with leading zeros) takes linear time with respect to the number of digits. Because O(β)O(\beta) is strictly bounded by O(M(β))O(M(\beta)) since M(β)=Ω(β)M(\beta) = \Omega(\beta), the string operations do not ruin the asymptotic time complexity.

1

Function signature

Our function has the signature Convert(n,d,i)Convert(n, d, i), where

  • nn is the number to convert

  • dd is the upper bound on number of decimal digits of nn

  • ii is the current recursion level

The function returns a decimal representation of nn. It should be initially called with Convert(N,D,1)Convert(N, D, 1).

2

Base case

If d=1d=1 then return the single digit representation of nn.

3

Recursive case

  1. Compute the values for the top t=n/Pit=\lfloor n /P_i \rfloor and bottom b=n ⁣ ⁣modPib=n \!\!\mod P_i halves.

  2. Recursively obtain the decimal representation of the top half rtop=Convert(t,dSi,i+1)r_{top}=Convert(t,d-S_i,i+1).

  3. Recursively obtain the decimal representation of the bottom half rbottom=Convert(b,Si,i+1)r_{bottom}=Convert(b,S_i,i+1).

  4. Pad rbottomr_{bottom} with leading zeros to length SiS_i.

  5. Produce a combined result r=rtoprbottomr=r_{top} \Join r_{bottom} by concatenating the two halves.

  6. Remove leading zeros from rr, if any.

  7. Return rr.

The recurrence formula is T(β)=2T(β/2)+M(β)T(\beta)=2T(\beta/2)+M(\beta). We have two variations on the recurrence using the fact that M(β)=Ω(β)M(\beta)=\Omega(\beta):

  • If M(β)=Θ(βlgβ)M(\beta)=\Theta(\beta\lg \beta) then by the Master theorem T(β)=Θ(M(β)lgβ)T(\beta)=\Theta(M(\beta)\lg \beta).

  • If M(β)=Θ(β1+ϵ)M(\beta)=\Theta(\beta^{1+\epsilon}) for some ϵ>0\epsilon>0 then by the Master theorem T(β)=Θ(M(β))T(\beta)=\Theta(M(\beta)) assuming M(β)M(\beta) satisfies the regularity condition (most standard multiplication/division algorithms do).

We can conclude that T(β)=O(M(β)lgβ)T(\beta)=O(M(\beta)\lg \beta) taking into account the pre-computation and conversion times, too.

31.1-14

See the answer here with a derailed solution.

31.2-1

You can read the proof here. There are two remarks about external references in the content:

  • Proposition 1(iv) is about the rule (a0da)    da(a \neq 0 \land d\mid a) \implies |d| \le |a|.

  • Proposition 1(i) is Exercise 31.1-3.

31.2-2

We create a table, similar to Figure 31.1 from the book, by running the next Python 3 script.

Observe the usage of the list rows to reverse the output shown below.

31.2-3 🌟

Let d1=gcd(a,n)d_1=\gcd(a,n) and d2=gcd(a+kn,n)d_2=\gcd(a+kn,n). We proceed by showing that d1d2d_1\mid d_2 and d2d1d_2\mid d_1, thus according to equation (31.5) they are equal.

We know that d1ad_1\mid a and d1nd_1\mid n, so by equation (31.4) we have d1a+knd_1\mid a+kn. Based on Corollary 31.3 we can conclude that d1d2d_1\mid d_2.

We know that d2nd_2\mid n, so n=pd2n=pd_2 for some integer pp. Furthermore, d2a+knd_2\mid a+kn, thus a+kn=qd2a+kn=qd_2 for some integer qq. Therefore, a=qd2kn=qd2kpd2=(qkp)d2a=qd_2-kn=qd_2-kpd_2=(q-kp)d_2 implies d2ad_2\mid a. Based on Corollary 31.3 we can conclude that d2d1d_2\mid d_1.

a=1 ( ⁣ ⁣ ⁣ ⁣modn)    a=1+kna=1\space(\!\!\!\!\mod n) \implies a=1+kn, so gcd(1+kn,n)=gcd(1,n)=1\gcd(1+kn,n)=\gcd(1,n)=1 by equation (31.18). Consequently, we have gcd(a,n)=1\gcd(a,n)=1.

31.2-4

Below is the iterative version of Euclid's algorithm in Python 3.

31.2-5

If b=0b=0 then the call Euclid(a,b)\text{Euclid}(a,b) immediately hits the base case and returns, so no recursive calls are made. Assume that b>0b>0.

The steps to prove the first bound are as follows:

  1. Let kk be the number of recursive calls made by Euclid(a,b)\text{Euclid}(a,b).

  2. By Lamé's theorem, we know that bFk+1b \ge F_{k+1}.

  3. By the standard bounds of the Fibonacci sequence, we know that Fk+1ϕk1F_{k+1} \ge \phi^{k-1} for k1k \ge 1.

  4. Combining these gives bϕk1b \ge \phi^{k-1}, so k1+logϕbk \le 1 + \log_\phi b.

Let d=gcd(a,b)d=\gcd(a,b). Because a=d(a/d)a=d\cdot (a/d) and b=d(b/d)b=d\cdot (b/d), the remainder at each step of the algorithm is also perfectly scaled by dd:

amodb=d((a/d)mod(b/d)).a \bmod b = d \cdot ((a/d) \bmod (b/d)).

Because every single division operation yields a remainder exactly dd times larger than the reduced version, the sequence of recursive calls progresses identically. The call Euclid(a,b)\text{Euclid}(a, b) will make the exact same number of recursive calls as Euclid(a/d,b/d)\text{Euclid}(a/d, b/d). Now, proceeding in almost the same way as above, we can establish that the call Euclid(a,b)\text{Euclid}(a,b) makes at most 1+logϕ(b/gcd(a,b))1 + \log_\phi (b/\gcd(a,b)) recursive calls.

31.2-6

Assume that k>2k>2, otherwise we would immediately reach the base case. Fk+1=Fk+Fk1F_{k+1}=F_k+F_{k-1}, hence by Exercise 31.1-1 we have that Fk1=Fk+1 ⁣ ⁣modFkF_{k-1}=F_{k+1} \!\!\mod F_k. Therefore, the call to Extended–Euclid(Fk+1,Fk)\text{Extended\text{\textendash}Euclid}(F_{k+1},F_k) recurses into Extended–Euclid(Fk,Fk1)\text{Extended\text{\textendash}Euclid}(F_k,F_{k-1}), and this process continues until hitting the base that happens right after the call to Extended–Euclid(F3,F2)\text{Extended\text{\textendash}Euclid}(F_3,F_2) . The final call Extended–Euclid(F2,0)\text{Extended\text{\textendash}Euclid}(F_2,0) returns a 3-tuple (1,1,0)(1,1,0). Thus, gcd(Fk+1,Fk)=1\gcd(F_{k+1},F_k)=1. We now need to find out the coefficients xx and yy.

In general, Extended–Euclid(Fk+1,Fk)\text{Extended\text{\textendash}Euclid}(F_{k+1},F_k) returns a 3-tuple (1,(1)k+1Fk2,(1)kFk1)(1,(-1)^{k+1}F_{k-2},(-1)^kF_{k-1}). We prove this by induction on kk. The base case k=2k=2 obviously holds. For k>2k>2 the quotient q=Fk+1/Fk=(Fk+Fk1)/Fk=1q=\lfloor F_{k+1}/F_k \rfloor=\lfloor (F_k+F_{k-1})/F_k \rfloor=1. Thus, (d,x,y)=(d,y,xy)(d,x,y)=(d',y',x'-y') at all levels. By the inductive hypothesis the call to Extended–Euclid(Fk,Fk1)\text{Extended\text{\textendash}Euclid}(F_k,F_{k-1}) returns a 3-tuple (1,(1)kFk3,(1)k1Fk2)(1,(-1)^kF_{k-3},(-1)^{k-1}F_{k-2}), thus Extended–Euclid(Fk+1,Fk)\text{Extended\text{\textendash}Euclid}(F_{k+1},F_k) returns

(1,(1)k1Fk2,(1)kFk3(1)k1Fk2)=(1,(1)k+1Fk2,(1)kFk3+(1)kFk2)=(1,(1)k+1Fk2,(1)k(Fk3+Fk2)=(1,(1)k+1Fk2,(1)kFk1)\begin{align*} (1,(-1)^{k-1}F_{k-2},(-1)^kF_{k-3}-(-1)^{k-1}F_{k-2}) &= (1,(-1)^{k+1}F_{k-2},(-1)^kF_{k-3}+(-1)^kF_{k-2}) \\ &= (1,(-1)^{k+1}F_{k-2},(-1)^k(F_{k-3}+F_{k-2}) \\ &= (1,(-1)^{k+1}F_{k-2},(-1)^kF_{k-1}) \quad \checkmark \end{align*}

Observe in line 1 that (1)k1=(1)k+1(-1)^{k-1}=(-1)^{k+1}.

31.2-7

We proceed by induction on nn. The base cases are when n=1n=2n=1 \lor n=2. If n=1n=1 then we have nothing to permute and for n=2n=2 we can apply equation (31.6).

For n>2n>2 assume that the inductive hypothesis holds for nn arguments. Every permutation is consisted of individual pairwise swaps of elements. An informal indirect proof is an ability of any sorting algorithm to find the matching permutation that results in proper ordering of elements. This is attained in a step-by-step fashion, swapping each time a pair of elements on different positions. Thus, let us consider elements aia_i and aja_j where 0i<jn0\le i <j \le n. We have two cases:

  1. If i>0i>0 then it immediately follows by the inductive hypothesis that gcd(a0,a1,,ai,,aj,,an)=gcd(a0,a1,,aj,,ai,,an)\gcd⁡(a_0,a_1,\dots,a_i,\dots,a_j,\dots,a_n)=\gcd⁡(a_0,a_1,\dots,a_j,\dots,a_i,\dots,a_n).

  2. If i=0i=0 then we have the following sequence of equivalent transformations:

gcd(ai,a1,,aj,,an)=gcd(ai,gcd(a1,,aj,,an))=gcd(ai,gcd(aj,a1,,an))(by the inductive hypothesis)=gcd(gcd(aj,a1,,an),ai)(by equation (31.6))=gcd(aj,gcd(a1,,an,ai))(by the associativity rule)=gcd(aj,gcd(a1,,ai,,an))(by the inductive hypothesis)=gcd(aj,a1,,ai,,an).\begin{align*} \gcd⁡(a_i,a_1,\dots,a_j,\dots,a_n)&=\gcd⁡(a_i,\gcd(a_1,\dots,a_j,\dots,a_n)) \\ &= \gcd⁡(a_i,\gcd(a_j,a_1,\dots,a_n)) && \text{(by the inductive hypothesis)} \\ &= \gcd⁡(\gcd(a_j,a_1,\dots,a_n),a_i) && \text{(by equation (31.6))} \\ &= \gcd⁡(a_j,\gcd(a_1,\dots,a_n,a_i)) && \text{(by the associativity rule)} \\ &= \gcd⁡(a_j,\gcd(a_1,\dots,a_i,\dots,a_n)) && \text{(by the inductive hypothesis)} \\ &=\gcd⁡(a_j,a_1,\dots,a_i,\dots,a_n). \end{align*}

Exercise 31.1-10 proves that the gcd\gcd operator is associative. This property extends to arbitrary number of arguments due to the generalized associative law.

gcd(a0,a1,,an)=gcd(a0,gcd(a1,,an))=xa0+ygcd(a1,a2,,an)=xa0+y(xa1+ygcd(a2,a3,,an))=xa0+y(xa1+y(xa2+y′′gcd(a3,a4,,an)))=\begin{align*} \gcd⁡(a_0,a_1,\dots,a_n) &= \gcd⁡(a_0,\gcd⁡(a_1,\dots,a_n)) \\ &= xa_0+y\gcd⁡(a_1,a_2,\dots,a_n) \\ &=xa_0+y(x'a_1+y'\gcd⁡(a_2,a_3,\dots,a_n)) \\ &=xa_0+y(x'a_1+y'(x''a_2+y′′\gcd(a_3,a_4,\dots,a_n))) \\ &=\dots \end{align*}

Let dk=gcd(ak,dk1)d_k=\gcd(a_k, d_{k-1}) at some step 1kn1 \le k \le n. Based on Exercise 31.2-5, the number of divisions for this single step is bounded by

O(1+lg(dk1/dk)).O(1 + \lg(d_{k-1} / d_k)).

Taking into account all steps, we get a telescoping sum

Total Divisionsk=1nO(1+lg(dk1dk))=O(n)+O(lgd0lgdn)=O(n+lg(max{a0,a1,,an})).\text{Total Divisions} \le \sum_{k=1}^n O\left(1 + \lg\left(\frac{d_{k-1}}{d_k}\right)\right)=O(n) + O(\lg d_0 - \lg d_n)=O(n+\lg (\max\{a_0,a_1,\dots,a_n\})).

31.2-8 🌟

The base case is defined as (the RHS is an optimization to avoid an overflow error)

lcm(a,b)=abgcd(a,b)=a(bgcd(a,b)).\text{lcm}(a,b)=\cfrac{|a\cdot b|}{\gcd(a,b)}= |a| \cdot \left( \frac{|b|}{\gcd(a,b)} \right).

The generalization to arbitrary number of arguments is almost the same as with the generic gcd\gcd (see the previous exercise). Therefore,

lcm(a1,a2,,an)=lcm(a1,lcm(a2,a3,,an)).\text{lcm}(a_1,a_2,\dots,a_n)=\text{lcm}(a_1,\text{lcm}(a_2,a_3,\dots,a_n)).

If any ai=0a_i = 0, we should immediately halt and return 0.

31.2-9

We first describe the general case and later demonstrate it using the example with k=4k=4 from the book. WLOG assume that indices are 0-based.

1

Constructing the derived pairs

  1. Write each index i{0,1,,k1}i \in \{0,1,\dots,k-1\} into unique binary form using exactly r=lgkr=\lceil \lg k \rceil digits, padding with leading zeros, as needed.

  2. For each bit position 0j<r0 \le j <r partition the set of input numbers into two groups:

    1. Aj={ni:the jth bit from the right in the binary form of i is 1}A_j=\{n_i:\text{the }j\text{th bit from the right in the binary form of }i \text{ is }1\}

    2. Bj={ni:the jth bit from the right in the binary form of i is 0}B_j=\{n_i:\text{the }j\text{th bit from the right in the binary form of }i \text{ is }0\}

  3. For each jj produce aj=nAjna_j=\prod_{n \in A_j} n and bj=nBjnb_j=\prod_{n \in B_j} n, thus forming rr pairs (aj,bj)(a_j,b_j).

2

Proving correctness

If the set of input numbers are pairwise relatively prime, then gcd(ns,nt)=1\gcd(n_s,n_t)=1 for sts \neq t. Fix any jj. Suppose for the sake of contradiction that some prime pajp\mid a_j and pbjp\mid b_j. Consequently, there are some distinct numbers nsAjn_s \in A_j and ntBjn_t \in B_j such that pnsp\mid n_s and pntp\mid n_t. But that contradicts the initial assumption about our set of input numbers. Thus, no such prime exists, and gcd(aj,bj)=1\gcd(a_j,b_j)=1 for all jj, as our choice of jj was arbitrary.

If the derived pairs are relatively prime, then gcd(aj,bj)=1\gcd(a_j,b_j)=1 for all pairs. Take any two distinct indices ss and tt that differ at bit position jj. WLOG assume that ss has 1 and tt has 0. Therefore, nsAjn_s \in A_j and ntBjn_t \in B_j. Suppose for the sake of contradiction that some prime pnsp\mid n_s and pntp\mid n_t. But we know that nsajn_s\mid a_j and ntbjn_t\mid b_j, so this implies that pajp\mid a_j and pbjp\mid b_j. But that contradicts the initial assumption about derived pairs being relatively prime. Therefore, the set of input numbers are pairwise relatively prime, since our choice of indices ss and tt was arbitrary.

3

Explaining the special case

For k=4k=4 we see that A0={n1,n3}A_0=\{n_1,n_3\}, A1={n2,n3}A_1=\{n_2,n_3\}, B0={n0,n2}B_0=\{n_0,n_2\} and B1={n0,n1}B_1=\{n_0,n_1\}. Therefore, the derived pairs are (n1n3,n0n2)(n_1n_3,n_0n_2) and (n2n3,n0n1)(n_2n_3, n_0n_1). They all satisfy the properties of being pairwise relatively prime. Notice that we are using 0-based indexing, so just shift indices by one to map them to those from the book.

31.3-1

+4012300123112302230133012\def\arraystretch{1.5} \begin{array}{c:c:c:c:c} +_4 & 0 & 1 & 2 & 3 \\ \hdashline 0 & 0 & 1 & 2 & 3 \\ \hdashline 1 & 1 & 2 & 3 & 0 \\ \hdashline 2 & 2 & 3 & 0 & 1 \\ \hdashline 3 & 3 & 0 & 1 & 2 \end{array}
5123411234224133314244321\def\arraystretch{1.5} \begin{array}{c:c:c:c:c} \cdot_5 & 1 & 2 & 3 & 4 \\ \hdashline 1 & 1 & 2 & 3 & 4 \\ \hdashline 2 & 2 & 4 & 1 & 3 \\ \hdashline 3 & 3 & 1 & 4 & 2 \\ \hdashline 4 & 4 & 3 & 2 & 1 \end{array}

Here are two possible mappings:

  1. f(0)=1,f(1)=3,f(2)=4,f(3)=2f(0)=1,f(1)=3,f(2)=4,f(3)=2

  2. f(0)=1,f(1)=2,f(2)=4,f(3)=3f(0)=1,f(1)=2,f(2)=4,f(3)=3

31.3-2

Subgroups of Z9\Zeta_9 are {0}\{0\}, {0,3,6}\{0,3,6\} and Z9\Zeta_9 itself. Subgroups of Z13\Zeta_{13}^* are {1}\{1\}, {1,12}\{1,12\}, {1,3,9}\{1,3,9\}, {1,5,8,12}\{1,5,8,12\}, {1,3,4,9,10,12}\{1,3,4,9,10,12\} and Z13\Zeta_{13}^* itself.

31.3-3 🌟

We only need to show two things (since associativity is automatically inherited from \oplus):

  • The identity element eSe \in S'.

  • Every element aSa \in S' has its inverse a1Sa^{-1} \in S'.

Choose any fixed element aSa \in S'. Define a modular function fa:SSf_a: S' \to S' such that fa(x)=axf_a(x) = a \oplus x (see also Exercise 31.3-5). Suppose fa(x)=fa(y)f_a(x) = f_a(y) for some x,ySx, y \in S'. This means ax=aya \oplus x = a \oplus y. Because a,xa, x and yy also belong to the parent group SS, we know aa has an inverse a1Sa^{-1} \in S. If we left-multiply both sides by a1a^{-1}, we get x=yx=y. Because fa(x)=fa(y)    x=yf_a(x) = f_a(y) \implies x = y, the function faf_a is injective. Since SS' is finite, this entails that faf_a is surjective, too. In other words, faf_a is a bijection.

Because aSa \in S' and faf_a is bijective, there must be some element bSb \in S' that maps to aa. This gives

fa(b)=a    ab=a.f_a(b) = a \implies a \oplus b = a.

In the parent group SS, the only element that satisfies this equation is the identity element ee. Therefore, b=eb=e. Since bSb \in S', we have proven that eSe \in S'.

Because eSe \in S' and faf_a is bijective, there must be some element bSb \in S' that maps to ee. This gives

fa(b)=e    ab=e.f_a(b) = e \implies a \oplus b = e.

In the parent group SS, the only element that satisfies this equation is the inverse element a1a^{-1}. Therefore, b=a1b=a^{-1}. Since bSb \in S', we have proven that a1Sa^{-1} \in S'.

31.3-4

Apply equation (31.21) to get

ϕ(pe)=pe(11p)=pe(p1p)=pe1(p1).\phi(p^e)=p^e\bigg(1-\frac{1}{p}\bigg)=p^e\bigg(\frac{p-1}{p}\bigg)=p^{e-1}(p-1).

31.3-5

We proceed by showing the faf_a is a bijection. Any bijection from a set to itself is a permutation of that set. Since the domain and codomain of faf_a are of the same cardinality, it is enough to show that faf_a is a surjective function.

According to Theorem 31.13, Zn\Zeta_n^* is a finite abelian group. Consequently, a1Zna^{-1} \in \Zeta_n^*. Select any bZnb \in \Zeta_n^*. For a1bZna^{-1}b \in \Zeta_n^*, we get

fa(a1b)=a(a1b) ⁣ ⁣ ⁣ ⁣modn=(aa1)b ⁣ ⁣ ⁣ ⁣modn=b ⁣ ⁣ ⁣ ⁣modn=b.f_a(a^{-1}b)=a(a^{-1}b)\!\!\!\!\mod n=(aa^{-1})b\!\!\!\!\mod n=b \!\!\!\!\mod n=b.

31.4-1

We have a=35a=35, n=50n=50 and b=10b=10. The extended version of Euclid's algorithm returns (5,3,2)(5,3,-2), so d=5d=5 and x0=3(10/5)=6x_0=3(10/5)=6. The step size is 50/5=1050/5=10, thus all solutions are {6,16,26,36,46}\{6,16,26,36,46\}.

31.4-2

Based on Corollary 31.26, there is a multiplicative inverse a1 ⁣ ⁣modna^{-1}\!\!\mod n, so multiplying both sides of ax=ay ( ⁣ ⁣ ⁣ ⁣modn)ax=ay\space(\!\!\!\!\mod n) by a1a^{-1} we get a1ax=a1ay ( ⁣ ⁣ ⁣ ⁣modn)a^{-1}ax=a^{-1}ay\space(\!\!\!\!\mod n), which implies that x=y ( ⁣ ⁣ ⁣ ⁣modn)x=y\space(\!\!\!\!\mod n).

If gcd(a,n)>1\gcd(a,n)>1, then the previous conclusion is not valid. One counterexample is 2x=2y ( ⁣ ⁣ ⁣ ⁣mod4)2x=2y\space(\!\!\!\!\mod 4), where x=1x=1 and y=3y=3 is also a valid solution.

31.4-3

Yes, it will work, forcing the procedure to emit solutions in sorted order. Among those dd distinct solutions, which are n/dn/d positions apart from each other, one will always belong to the interval [0,n/d)[0,n/d). Let it be xi=x(b/d)+i(n/d) ( ⁣ ⁣ ⁣ ⁣modn)x_i=x'(b/d)+i(n/d)\space(\!\!\!\!\mod n) for some i[0,d)i \in [0,d). Exercise 31.1.-7 implies that xi=x(b/d) ( ⁣ ⁣ ⁣ ⁣modn/d)x_i=x'(b/d)\space(\!\!\!\!\mod n/d), hence setting x0=xi ( ⁣ ⁣ ⁣ ⁣modn/d)x_0=x_i\space(\!\!\!\!\mod n/d) is the smallest starting value. Since the code starts at the minimum value and adds a positive constant each time without ever wrapping around the modulo boundary, the output is guaranteed to be strictly increasing.

★ 31.4-4 🌟

I recommend reading the proof based on Euclidean division of polynomials, which is aligned with the narrative from the book.

For deriving broader conclusions from the factor theorem, it is crucial for pp to be prime. For example, if p=8p=8 then the polynomial f(x)=x2+7 ( ⁣ ⁣ ⁣ ⁣modp)f(x)=x^2+7\space(\!\!\!\!\mod p) has 4 roots {1,3,5,7}\{1,3,5,7\} instead of t=2t=2, where tt is a degree of ff.

We proceed by induction on degree tt. The base case t=1t=1 follows from Corollary 31.25.

For t>1t>1, we have f(x)=(xa)g(x)f(x)=(x-a)g(x), so ff has at most 1+(t1)=t1+(t-1)=t distinct roots modulo pp (after applying the inductive hypothesis on g(x)g(x)). Notice, that any other root of ff is forced to be a root of gg. Suppose ff has another distinct zero bb, such that bab \neq a and

f(b)=(ba)g(b)=0(modp).f(b)=(b-a)g(b) = 0 \pmod p.

p(ba)g(b)    p(ba)pg(b)p \mid (b-a)g(b) \implies p \mid (b-a) \lor p \mid g(b) (recall that pp is prime). Since ba(modp)b \neq a \pmod p, it must be that pg(b)p \mid g(b), so g(b)=0(modp)g(b) = 0 \pmod p. This concludes the proof for all t1t \ge 1.

31.5-1

We follow the process outlined in Theorem 31.27. We have x1=4x_1=4, n1=5n_1=5, x2=5x_2=5, n2=11n_2=11, m1=11m_1=11, m11=1 ( ⁣ ⁣ ⁣ ⁣mod5)m_1^{-1}=1\space(\!\!\!\!\mod 5), c1=111=11c_1=11\cdot 1=11, m2=5m_2=5, m21=9 ( ⁣ ⁣ ⁣ ⁣mod11)m_2^{-1}=9\space(\!\!\!\!\mod 11) and c2=59=45c_2=5 \cdot 9=45. Thus, x=114+455 ( ⁣ ⁣ ⁣ ⁣mod55)x=11\cdot4+45\cdot 5\space(\!\!\!\!\mod 55). All solutions are of the form 49+55k49+55k for arbitrary integers kk.

31.5-2

We follow the steps outlined in Theorem 31.27. We have m1=56m_1=56, m11=5 ( ⁣ ⁣ ⁣ ⁣mod9)m_1^{-1}=5\space(\!\!\!\!\mod 9), c1=280c_1=280, m2=63m_2=63, m21=7 ( ⁣ ⁣ ⁣ ⁣mod8)m_2^{-1}=7\space(\!\!\!\!\mod 8), c2=441c_2=441, m3=72m_3=72, m31=4 ( ⁣ ⁣ ⁣ ⁣mod7)m_3^{-1}=4\space(\!\!\!\!\mod 7) and c3=288c_3=288. Thus, x=1280+2441+3288 ( ⁣ ⁣ ⁣ ⁣mod504)x=1 \cdot 280+2\cdot 441+3\cdot 288\space(\!\!\!\!\mod 504). All solutions are of the form 10+504k10+504k for arbitrary integers kk.

31.5-3

Let x=a1 ( ⁣ ⁣ ⁣ ⁣modn)x=a^{-1}\space(\!\!\!\!\mod n). We simply employ equation (31.30) and Corollary 31.29 to get that ax=1 ( ⁣ ⁣ ⁣ ⁣modn)ax=1\space(\!\!\!\!\mod n) has a component-wise modular representation of (1,1,,1)(1,1,\dots,1), where every component is aixi=1 ( ⁣ ⁣ ⁣ ⁣modni)a_ix_i=1\space(\!\!\!\!\mod n_i). Therefore, xi=ai1 ( ⁣ ⁣ ⁣ ⁣modni)x_i=a_i^{-1}\space(\!\!\!\!\mod n_i). This correspondence works in both directions.

31.5-4

Let n=i=1knin=\prod_{i=1}^k n_i, where all terms of this product are pairwise relatively prime. According to Theorem 31.27, there is a one-to-one correspondence between a root of the equation f(x)=0 ( ⁣ ⁣ ⁣ ⁣modn)f(x)=0\space(\!\!\!\!\mod n) and its component-wise modular representation. Based on Corollary 31.29, we also have that f(x)=0 ( ⁣ ⁣ ⁣ ⁣modn)f(x)=0\space(\!\!\!\!\mod n) if and only if f(x)=0 ( ⁣ ⁣ ⁣ ⁣modni)f(x)=0\space(\!\!\!\!\mod n_i) for i=1,2,,ki=1,2,\dots,k. Furthermore, by the basic rules of modular arithmetic f(x)=0 ( ⁣ ⁣ ⁣ ⁣modni)    f(x ⁣ ⁣ ⁣modni)=0 ( ⁣ ⁣ ⁣ ⁣modni)f(x)=0\space(\!\!\!\!\mod n_i) \iff f(x \!\!\!\mod n_i)=0\space(\!\!\!\!\mod n_i).

Let a kk-tuple (x1,x2,,xk)Zn1×Zn2××Znk(x_1,x_2,\dots,x_k) \in \Zeta_{n_1} \times \Zeta_{n_2} \times \dots \times \Zeta_{n_k} be one solution of the system of equations f(xi)=0 ( ⁣ ⁣ ⁣ ⁣modni)f(x_i)=0\space(\!\!\!\!\mod n_i) for i=1,2,,ki=1,2,\dots,k. Consequently, xZnx \in \Zeta_n computed from inputs (x1,x2,,xk)(x_1,x_2,\dots,x_k), by following the steps from Theorem 31.27, represents a zero of ff modulo nn. By the fundamental counting principle, the total number of ways to form a valid kk-tuple (x1,x2,,xk)(x_1, x_2, \dots, x_k) is exactly the product of the number of valid choices for each coordinate. Therefore, the number of roots modulo nn equals the product of the number of roots modulo each nin_i.

31.6-1

Element
Order

1

1

2

10

3

5

4

5

5

5

6

10

7

10

8

10

9

5

10

2

The smallest primitive root is 2 and the indices ind11,2(x)ind_{11,2}(x) of xZ11x \in \Zeta_{11}^* are shown below.

Element
Index

1

0

2

1

3

8

4

2

5

4

6

9

7

7

8

3

9

6

10

5

31.6-2

x2=1 ( ⁣ ⁣ ⁣ ⁣ ⁣ ⁣modpe)    pex21    pe(x1)(x+1).x^2=1\space(\!\!\!\!\!\!\mod p^e) \iff p^e|x^2-1 \iff p^e|(x-1)(x+1).

31.6-3

Replace lines 6 and 7 with the following two lines:

31.6-4

Below is the iterative modular repeated squaring algorithm in Python 3.

31.6-5

By Euler's theorem we know that aϕ(n)=1 ( ⁣ ⁣ ⁣ ⁣modn)    aaϕ(n)1=1 ( ⁣ ⁣ ⁣ ⁣modn)a^{\phi(n)}=1\space(\!\!\!\!\mod n) \iff aa^{\phi(n)-1}=1\space(\!\!\!\!\mod n) . Therefore, we can calculate the inverse by calling Modular-Exponentiation(a,ϕ(n)1,n)\text{Modular-Exponentiation}(a, \phi(n)-1,n).

31.7-1

ϕ(n)=(p1)(q1)=280\phi(n)=(p-1)(q-1)=280, so d=187 ( ⁣ ⁣ ⁣ ⁣mod280)d=187\space(\!\!\!\!\mod 280). The encryption of the message M=100M=100, by equation (31.37), is P(100)=254P(100)=254. You can test that everything is proper by calling Modular-Exponentiation(254,187,319)\text{Modular-Exponentiation}(254, 187,319) (see Exercise 31.6-4) and getting back the original message 100.

31.7-2

Letβ\beta denote the number of bits in n=pqn=pq and assume that pp and qq are large primes. As e=3e=3, we have 3d=1 ( ⁣ ⁣ ⁣ ⁣modϕ(n))3d=1\space(\!\!\!\!\mod \phi(n)) that is equivalent to 3d1=k(p1)(q1)3d-1=k(p-1)(q-1) for some integer kk. Since 0<d<ϕ(n)0<d<\phi(n) we know that k=1k=2k=1 \lor k=2. We can rewrite the previous expression as 3d1=k(n(p+q)+1)3d-1=k(n-(p+q)+1). Apparently, 3d1<n    k=13d-1<n \implies k=1 and 3d1>n    k=23d-1>n \implies k=2.

We can find x=p+q=n(3d1)/k+1x=p+q=n-(3d-1)/k+1. Plugging in q=n/pq=n/p into the expression for xx we get x=p+n/p    n=p(xp)x=p+n/p \implies n=p(x-p). The last equation can be solved for pp using the quadratic formula. Obviously, once we have pp we can trivially find qq.

All listed steps (including the integer square root) can be executed in time polynomial in β\beta, hence the whole process is also doable in time polynomial in β\beta.

★ 31.7-3

PA(M1)PA(M2)=M1eM2e ( ⁣ ⁣ ⁣ ⁣ ⁣ ⁣modn)=(M1M2)e ( ⁣ ⁣ ⁣ ⁣ ⁣ ⁣modn)=PA(M1M2).\begin{align*} P_A(M_1)P_A(M_2) &= M_1^eM_2^e\space(\!\!\!\!\!\!\mod n) \\ &= (M_1M_2)^e\space(\!\!\!\!\!\!\mod n) \\ &= P_A(M_1M_2). \end{align*}

Suppose an attacker possesses a ciphertext CA=PA(MA)C_A=P_A(M_A). She/he executes the following procedure to reveal MAM_A:

  1. Generate a new message MRZnM_R \in \Zeta_n^*.

  2. Compute C=CAPA(MR)C=C_AP_A(M_R).

  3. Try to decrypt CC and store the result into MM.

  4. If successful, then find MR1M_R^{-1} modulo nn and compute MA=(MMR1modn)M_A=(MM_R^{-1}\bmod n).

  5. Goto step 1.

The chance of success at step 3 is 1%. The number of trials follows the geometric distribution with an expected value of 100. Finding inverses modulo nn is step 4 can be efficiently carried out using the extended Euclid's GCD algorithm. Almost all generated messages at step 1 will be relatively prime to nn, since the chance of stumbling across one that contains a prime factor of nn is practically zero. All in all, an attacker can reveal the original message with high probability.

31.8-1

One example is n=75=35n=7\cdot5=35, where 6 is a nontrivial square root of 1 modulo 35, since 62=1 ( ⁣ ⁣ ⁣ ⁣mod35)6^2=1\space(\!\!\!\!\mod 35). Such a root always exists for nn that satisfies the conditions from this exercise.

Decompose nn into a product n1n2n_1n_2, where n1n_1 and n2n_2 are odd numbers greater than 1 that are relatively prime to each other. By Corollary 31.28, there exists an xx simultaneously satisfying the equations

x=1 ( ⁣ ⁣ ⁣ ⁣ ⁣modn1)x=   1 ( ⁣ ⁣ ⁣ ⁣ ⁣modn2).\begin{align*} x&=-1\space (\!\!\!\!\!\mod n_1) \\ x&=\space\space\space1\space (\!\!\!\!\!\mod n_2). \end{align*}

Corollary 31.29 gives that x±1 ( ⁣ ⁣ ⁣ ⁣modn)x\neq±1\space (\!\!\!\!\mod n). On the other hand, we have

x2=1 ( ⁣ ⁣ ⁣ ⁣ ⁣modn1)x2=1 ( ⁣ ⁣ ⁣ ⁣ ⁣modn2).\begin{align*} x^2&=1\space (\!\!\!\!\!\mod n_1) \\ x^2&=1\space (\!\!\!\!\!\mod n_2). \end{align*}

Corollary 31.29 gives that x2=1 ( ⁣ ⁣ ⁣ ⁣modn)x^2=1\space (\!\!\!\!\mod n). Therefore, xx is a nontrivial square root of 1 modulo nn.

★ 31.8-2

In number theory, λ(n)\lambda(n) is known as the Carmichael function.

Euler’s phi function is a multiplicative function that we can see after substituting n=i=1rpein=\prod_{i=1}^rp^{e_i} to get

ϕ(n)=ϕ(p1e1prer)=p1e1prer(11/p1)(11/pr)(by equation (31.21))=p1e11(p11)prer1(pr1)=ϕ(p1e1)ϕ(prer)(by Exercise (31.3-4)).\begin{align*} \phi(n)&=\phi(p_1^{e_1}\dots p_r^{e_r}) \\ &=p_1^{e_1}\dots p_r^{e_r}(1-1/p_1) \dots (1-1/p_r) && \text{(by equation (31.21))} \\ &=p_1^{e_1-1}(p_1-1)\dots p_r^{e_r-1}(p_r-1) \\ &=\phi(p_1^{e_1})\dots\phi(p_r^{e_r}) && \text{(by Exercise (31.3-4))}. \end{align*}

It follows that ϕ(n)\phi(n) is a multiple of λ(n)\lambda(n), thus λ(n)ϕ(n)\lambda(n)\mid \phi(n). Another proof, based on group theory, is given here.

Suppose for the sake of contradiction that a Carmichael number nn is not "square-free," thus it has a factor pieip_i^{e_i} where ei>1e_i>1. Consequently, piϕ(piei)    piλ(n)p_i\mid \phi(p_i^{e_i}) \implies p_i\mid \lambda(n). Furthermore, pin    pin1p_i\mid n \implies p_i \nmid n-1, which is a contradiction with the property that λ(n)n1\lambda(n)\mid n-1. Therefore, nn must be "square-free."

Suppose for the sake of contradiction that a Carmichael number nn is a product of two primes p<qp<q. We have that λ(n)=lcm(p1,q1)    q1λ(n)\lambda(n)=\text{lcm}(p-1,q-1) \implies q-1\mid \lambda(n). On the other hand, q1n1q-1 \nmid n-1 because by the Division theorem we have n1=pq1=(q1)p+(p1)n-1=pq-1=(q-1)p+(p-1). But this is again a contradiction with the property that λ(n)n1\lambda(n)\mid n-1. Therefore, nn must be consisted of at least three primes.

31.8-3

By Exercise 31.6-2 we have x2=1 ( ⁣ ⁣ ⁣ ⁣modn)    n(x1)(x+1)x^2=1\space(\!\!\!\!\mod n) \iff n\mid (x-1)(x+1). If xx is a nontrivial square root of 1 modulo nn, then by Corollary 31.35 nn is composite, so 2<x<n12<x<n-1 (you can easily check that x=2x=2 produces a prime n=3n=3). It follows that nx1n \nmid x-1 and nx+1n \nmid x+1, thus prime factors of nn are distributed between these two terms of a product. Therefore, gcd(x1,n)\gcd(x-1,n) and gcd(x+1,n)\gcd(x+1,n) are both nontrivial divisors of nn.

Problems

31-1 Binary gcd algorithm

a.

By Corollary 31.4, we have gcd(a,b)=gcd(2(a/2),2(b/2))=2gcd(a/2,b/2)\gcd(a,b)=\gcd(2(a/2),2(b/2))=2\gcd(a/2,b/2).

b.

By equation (31.13), gcd(a,b)\gcd(a,b) doesn't have a prime factor 2. Dividing bb by 2 reduces the exponent of 2 in bb, hence making it closer to zero. Consequently, gcd(a,b)=gcd(a,b/2)\gcd(a,b)=\gcd(a,b/2).

c.

Let d1=gcd(a,b)d_1=\gcd(a,b) and d2=gcd(ab,b)d_2=\gcd(a-b,b). We proceed by showing that d1d2d_1\mid d_2 and d2d1d_2\mid d_1, thus according to equation (31.5) they are equal.

We know that d1ad_1\mid a and d1bd_1\mid b, so by equation (31.4) we have d1abd_1\mid a-b. Based on Corollary 31.3 we can conclude that d1d2d_1\mid d_2

We know that d2abd_2\mid a-b and d2bd_2\mid b, so by equation (31.4) we have d2(ab)+b=ad_2\mid (a-b)+b=a. Based on Corollary 31.3 we can conclude that d2d1d_2\mid d_1.

Since aba-b is even and bb is odd, the division of aba-b by 2 is justified according to part (b).

d.

I recommend reading this study of the binary GCD algorithm with insightful C++ based optimizations.

31-2 Analysis of bit operations in Euclid’s algorithm

In all subproblems below assume that 1<ba1<b \le a, so that q1q \ge 1.

a.

We do all the arithmetic in binary by processing the bits of aa from the most significant to the least, accumulating the initial O(lgb)O(\lg b) bits of aa into a value (current residue rr) greater or equal than bb, as they would anyhow produce leading zeros of a quotient.

In the analysis of the long division algorithm, the bits of the quotient and the divisor are counted differently because they play distinct roles. Each bit of qq is determined in one step of the algorithm. Thus, the number of steps required to compute all bits of qq is proportional to it's number of bits lgq+1=O(1+lgq)\lfloor \lg q \rfloor+1=O(1+\lg q). This expression is nonzero even if q1q \le1.

The algorithm still requires at least one step to compare aa and bb even if q=0q=0 (when a<b)a<b). The expression 1+lgq=1+0=11+\lg q=1+0=1 aims to encompass this case, despite lgq\lg q being undefined here.

At each step, the algorithm performs operations (e.g., comparisons or subtractions) involving the divisor bb. Since it has approximately O(lgb)O(\lg b) bits, these operations require that many bit operations per step. Therefore, the total count is O(1+lgq)×O(lgb)=O((1+lgq)lgb)O(1+\lg q) \times O(\lg b)=O((1+\lg q)\lg b). This complexity reflects that the algorithm's time is dominated by the number of quotient bits (steps) and the size of the divisor (cost per step).

The remainder rr gets calculated along the way (see also Exercise 31.1-12) and has O(lgb)O(\lg b) bits.

b.

The reduction requires us to compute the remainder that can be accomplished in time as given in part (a). We know that a=qb+ra=qb+r, where 0r<b0 \le r < b.

μ(a,b)μ(b,a  ⁣ ⁣ ⁣ ⁣ ⁣modb)=μ(a,b)μ(b,r)=(1+lga)(1+lgb)(1+lgb)(1+lgr)=(1+lgb)(lgalgr)=(1+lgb)lg(qbr+1)>(1+lgb)lg(q+1)>(1+lgb)12(1+lgq)>12lgb(1+lgq).\begin{align*} \mu(a,b)-\mu(b,a \space \!\!\!\!\!\mod b) &= \mu(a,b)-\mu(b,r) \\ &=(1+\lg a)(1+\lg b)-(1+\lg b)(1+\lg r) \\ &=(1+\lg b)(\lg a-\lg r) \\ &= (1+\lg b)\lg\left(q\frac{b}{r} + 1\right) \\ &> (1+\lg b)\lg (q+1) \\ &> (1 + \lg b) \frac{1}{2}(1 + \lg q) \\ &> \frac{1}{2} \lg b (1 + \lg q). \end{align*}

We have proven that O((1+lgq)lgb)=O(μ(a,b)μ(b,r))O((1 + \lg q)\lg b)=O(\mu(a,b) - \mu(b,r)).

c.

As shown in part (b), EUCLID takes at most c(μ(a,b)μ(b,a  ⁣ ⁣ ⁣ ⁣modb))c(\mu(a,b)-\mu(b,a \space \!\!\!\!\mod b)) bit operations on the first recursive call, at most c(μ(b,a  ⁣ ⁣ ⁣ ⁣modb)μ(a  ⁣ ⁣ ⁣ ⁣modb,b  ⁣ ⁣ ⁣mod(a  ⁣ ⁣ ⁣ ⁣modb)))c(\mu(b,a \space \!\!\!\!\mod b)-\mu(a \space \!\!\!\!\mod b, b\space \!\!\!\mod (a \space \!\!\!\!\mod b))) on the second, and so on. This sum telescopes, hence we end up with cμ(a,b)O(1)=O(μ(a,b))c\mu(a,b)-O(1)=O(\mu(a,b)).

When applied to two β\beta-bit inputs we get O(μ(a,b))=O((1+β)2)=O(β2)O(\mu(a,b))=O((1+\beta)^2)=O(\beta^2).

31-3 Three algorithms for Fibonacci numbers

a.

The algorithmic recurrence of the running time is T(n)=T(n1)+T(n2)+Θ(1)T(n)=T(n-1)+T(n-2)+\Theta(1). We use a substitution method to prove that this version cannot do better than being exponential in nn. The constant c1>0c_1>0 denotes the lower bound in Θ(1)\Theta(1).

Assume that T(n)cFnT(n) \ge cF_n for n2n \ge 2.

T(n)=T(n1)+T(n2)+Θ(1)cFn1+cFn2+c1(by the inductive hypothesis)=cFn+c1>cFnT(n)=Ω(Fn).\begin{align*} T(n)&=T(n-1)+T(n-2)+\Theta(1) \\ &\ge cF_{n-1}+cF_{n-2}+c_1 && \text{(by the inductive hypothesis)} \\ &= cF_n+c_1 \\ &>cF_n \\ &\therefore T(n)=\Omega(F_n). \end{align*}

This concludes the proof, as Fibonacci numbers grow exponentially.

b.

Here is the Python 3 script showcasing memoization. The recurrence T(n)=T(n1)+Θ(1)T(n)=T(n-1)+\Theta(1) resolves to T(n)=O(n)T(n)=O(n), since memoization prevents repeated work.

It will print in the blink of an eye 354224848179261915075. Try commenting out the annotation to see the difference.

c.

We can leverage the repeated squaring method, with a small change, that instead of multiplying numbers we will work with small constant sized matrices. Each matrix operation will then also take constant time.

We prove the next statement using induction on nn, following the hint from the book.

[0111]n=[Fn1FnFnFn+1].\begin{align*} \begin{bmatrix} 0 & 1 \\ 1 & 1 \end{bmatrix}^n &= \begin{bmatrix} F_{n-1} & F_n \\ F_n & F_{n+1} \end{bmatrix}. \end{align*}

The base case n=1n=1 obviously holds. For n>1n>1 we have

[0111]n=[0111][0111]n1=[0111][Fn2Fn1Fn1Fn](by the inductive hypothesis)=[Fn1FnFn2+Fn1Fn1+Fn]=[Fn1FnFnFn+1]\begin{align*} \begin{bmatrix} 0 & 1 \\ 1 & 1 \end{bmatrix}^n &= \begin{bmatrix} 0 & 1 \\ 1 & 1 \end{bmatrix} \begin{bmatrix} 0 & 1 \\ 1 & 1 \end{bmatrix}^{n-1} \\ &= \begin{bmatrix} 0 & 1 \\ 1 & 1 \end{bmatrix} \begin{bmatrix} F_{n-2} & F_{n-1} \\ F_{n-1} & F_n \end{bmatrix} && \text{(by the inductive hypothesis)} \\ &= \begin{bmatrix} F_{n-1} & F_n \\ F_{n-2}+F_{n-1} & F_{n-1}+F_n \end{bmatrix} \\ &= \begin{bmatrix} F_{n-1} & F_n \\ F_n & F_{n+1} \end{bmatrix} \quad \checkmark \end{align*}

At the end, we need to read out the matching cell containing FnF_n.

d.

Fibonacci numbers have β=Θ(n)\beta=\Theta(n) binary digits, as explained here.

The bare recursive variant still has exponential running time. Increasing the amount of work at each level cannot invalidate the previously established lower bound.

The memoized version has a new recurrence T(n)=T(n1)+Θ(n)T(n)=T(n-1)+\Theta(n) that solves to T(n)=Θ(n2)T(n)=\Theta(n^2).

The matrix version employs both addition and multiplication of numbers, hence it has a new recurrence T(n)=T(n/2)+Θ(n2)T(n)=T(n/2)+\Theta(n^2). Case 3 of the master theorem states that T(n)=Θ(n2)T(n)=\Theta(n^2).

31-4 Quadratic residues

a.

Theorem 31.32 implies that Zp\Zeta_p^* is a cyclic group, so it has a generator gg, where all values gig^i modulo pp are distinct for i=0,1,,p2i=0,1,\dots,p-2. Every even index is associated with a quadratic residue, since (gi/2)2=gi ( ⁣ ⁣ ⁣ ⁣modp){(g^{i/2}})^2=g^i\space(\!\!\!\!\mod p). Thus, the number of quadratic residues is at least (p1)/2(p-1)/2.

Suppose for the sake of contradiction that there is also some odd index ii such that x2=gi ( ⁣ ⁣ ⁣ ⁣modp)x^2=g^i\space(\!\!\!\!\mod p) for some xZpx \in \Zeta_p^*. We must have that x=gj ( ⁣ ⁣ ⁣ ⁣modp)x=g^j\space(\!\!\!\!\mod p) and g2j=gi ( ⁣ ⁣ ⁣ ⁣modp)g^{2j}=g^i\space(\!\!\!\!\mod p). Theorem 31.33 implies that 2j=i ( ⁣ ⁣ ⁣ ⁣modϕ(p))2j=i\space(\!\!\!\!\mod \phi(p)), where ϕ(p)=p1\phi(p)=p-1. But this is impossible, since p12jip-1 \nmid 2j-i due to mismatch in parity. We can conclude that the the number of quadratic residues is exactly (p1)/2(p-1)/2.

b.

If aa is a quadratic residue modulo pp, then there is an xx such that

a(p1)/2=(x2)(pi1)/2=xp1=1=(ap) ( ⁣ ⁣ ⁣ ⁣ ⁣modp)     (by Fermat’s theorem).a^{(p-1)/2}=(x^2)^{(pi-1)/2}=x^{p-1}=1=\bigg(\frac{a}{p}\bigg)\space(\!\!\!\!\!\mod p) \space\space\space\space\text{ (by Fermat's theorem)}.

Otherwise, gi=a ( ⁣ ⁣ ⁣ ⁣modp)g^i=a\space(\!\!\!\!\mod p) for some odd index ii (see part(a)). According to Fermat's theorem gp1=g0=1 ( ⁣ ⁣ ⁣ ⁣modp)g^{p-1}=g^0=1\space(\!\!\!\!\mod p), so it must be that g(p1)/2=1 ( ⁣ ⁣ ⁣ ⁣modp)g^{(p-1)/2}=-1\space(\!\!\!\!\mod p), as powers of gg are distinct for i=0,1,,p2i=0,1,\dots,p-2. We have

a(p1)/2=(gi)(p1)/2=(g(p1)/2)i=(1)i=1=(ap) ( ⁣ ⁣ ⁣ ⁣ ⁣modp).a^{(p-1)/2}=(g^i)^{(p-1)/2}=(g^{(p-1)/2})^i=(-1)^i=-1=\bigg(\frac{a}{p}\bigg)\space(\!\!\!\!\!\mod p).

We can call modular_exponentiation(a, (p-1)/2, p) (see Exercise 31.6-4) and check whether the returned value is 1. If we let β=lgp\beta=\lg p, then It requires O(β3)O(\beta^3) bit operations.

c.

By part (b), a(p1)/2=a2k+1=1 ( ⁣ ⁣ ⁣ ⁣modp)a^{(p-1)/2}=a^{2k+1}=1\space(\!\!\!\!\mod p), so (ak+1)2=a2k+2=aa2k+1=a ( ⁣ ⁣ ⁣ ⁣modp)(a^{k+1})^2=a^{2k+2}=a\cdot a^{2k+1}=a\space(\!\!\!\!\mod p). Therefore, ak+1a^{k+1} is a square root of aa modulo pp. We can again use modular exponentiation to efficiently find it using O(β3)O(\beta^3) bit operations.

d.

Run the algorithm of part (b) by repeatedly trying random numbers a[2,p)a \in [2,p) until (ap)=1 ( ⁣ ⁣ ⁣ ⁣modp)\big(\frac{a}{p}\big)=-1\space(\!\!\!\!\mod p) . Half the elements in Zp\Zeta_p^* are not quadratic residues, thus each experiment can be regarded as a Bernoulli trial with probability 1/2. The expected number of trials follows the geometric distribution and equals 2. The average number of arithmetic operations is O(β)O(\beta).

Last updated