31. Number-Theoretic Algorithms
Exercises
31.1-1
Based on Theorem 31.1 (Division theorem), we have
since a>b>0. Furthermore, c=a+b⟹r=b=cmoda.
31.1-2
Read Euclid's proof on Wikipedia.
31.1-3 🌟
Proves that the a∣b relation is transitive.
We have a∣b⟹b=pa and b∣c⟹c=qb for integers p and q. Therefore, c=(pq)a⟹a∣c.
31.1-4
The only divisors of p are 1 and p. No divisor of k except 1 divides p, since k<p⟹p∤k. Therefore, gcd(k,p)=1.
31.1-5
We start from the fact that gcd(a,n)=1, thus
31.1-6 🌟
Proves that for all integers a and b and all primes p, (a+b)p=ap+bp(modp).
Lemma 1
For any integers a1,a2,…,an and p, if gcd(ai,p)=1 for all i=1,2,…,n, then gcd(∏i=1nai,p)=1.
Proof We prove the lemma by induction on n. The base case n=2 trivially holds by Theorem 31.6. For n>2, we have
According to the principles of mathematical induction, we can conclude that the lemma holds for all n≥2. ■
Based on Exercise 31.1-4 and Lemma 1, for 0<k<p, gcd(k!,p)=1 and gcd((p−k)!,p)=1, so gcd(k!(p−k)!,p)=1. By Theorem 31.7 we can conclude that p∣(kp). Thus, in a binomial expansion all terms except the first and last one are divisible by p, which concludes the proof.
31.1-7
We successively apply the Division theorem, thus
because 0≤((xmodb)moda)<a, so it represents a unique remainder modulo a.
For the second part, we get
31.1-8
Assume that multiplication of two β-bit integers requires O(β2) time and equality test of two β-bit integers requires O(β) time. The algorithm below only showcases an existence of a solution in time polynomial in β without claiming anything about its asymptotic optimality. As a matter of fact, it is far from being optimal.
The smallest candidate is a=2 and 2β>n⟹k∈[2,β). Therefore, we iterate over all possible exponents k in increasing order and see if the kth root of n is an integer.
We can leverage binary search over the interval [2,n) in searching for this root. We iteratively raise each candidate a to the kth power by multiplying the current product with a (initially setting it to 1). In each iteration, we also check if this product is greater than n, so that we stop as soon as possible. This stage demands O(β)(O(β2)+O(β))=O(β3) time. In case of a match, we finish the search, otherwise, recurse into the lower or upper subinterval depending on whether ak>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 β-bit integer n is a nontrivial power in O(β5) time.
31.1-9
Proof of 31.6
By definition, gcd(a,b) is the largest element in the set D(a,b) of the common divisors of a and b. Analogously, gcd(b,a) is the largest element in the set D(b,a) of the common divisors of b and a. We know that D(a,b)=D(b,a) and there cannot be two different largest element in a set. Therefore, gcd(a,b)=gcd(b,a).
Proof of 31.7
d1=gcd(a,b)⟹(d1∣a)∧(d1∣b), but this also means that d1∣(−a), hence d1≤d2=gcd(−a,b). With similar reasoning we can conclude that d2≤d1, therefore d1=d2.
Proof of 31.8
Proof of 31.9
By property (31.8) we have that gcd(a,0)=gcd(∣a∣,0). Any common divisor of ∣a∣ and 0 cannot be larger than ∣a∣, so gcd(a,0)=∣a∣ (any integer divides 0). As a side note, if a=0 then gcd(0,0)=0=∣a∣.
Proof of 31.10
By property (31.8) we have that gcd(a,ka)=gcd(∣a∣,∣ka∣)=gcd(∣a∣,∣k∣∣a∣). We have the following three cases:
If k=0 then gcd(a,0)=∣a∣ by property (31.9).
If a=0 then gcd(0,0)=0=∣a∣.
Let a=0∧k=0. We know that ∣a∣ divides itself, and ∣a∣ divides ∣k∣∣a∣. Because ∣a∣ is a common divisor, and no divisor of ∣a∣ can be strictly greater than ∣a∣, it must be the greatest common divisor. Therefore, gcd(∣a∣,∣k∣∣a∣)=∣a∣.
31.1-10 🌟
Proves that the gcd operator is associative.
Let d1=gcd(a,gcd(b,c)) and d2=gcd(gcd(a,b),c). We prove the statement from the book by showing that d1∣d2 and d2∣d1, thus by equation (31.5) they must be equal, as both of them are nonnegative.
We know that d1∣a∧d1∣gcd(b,c), which entails that d1∣b∧d1∣c. The fact that d1 divides both a and b implies that it also divides gcd(a,b) by Corollary 31.3. Therefore, d1∣d2 by applying again Corollary 31.3.
Using the same reasoning we can conclude that d2∣d1, which finishes the proof about associativity of the 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) 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 β-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:
Fill the lookup tables
For each level i=1,2,…,L:
Determine the split point Si=⌈D/2i⌉ (half the digits at level i).
Compute Pi=10Si using exponentiation by squaring.
Each Pi has Θ(β/2i) bits, so computing it demands O(M(β/2i)lgβ) time. The total time is ∑i=1LO(M(β/2i)lgβ)=O(∑i=1LM(β/2i)lgβ)=O(M(β)lgβ) assuming M satisfies the properties of standard multiplication algorithms, like, being non-decreasing as well as the sum of M over geometrically decreasing sizes is dominated by M(β) (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(β) is strictly bounded by O(M(β)) since M(β)=Ω(β), the string operations do not ruin the asymptotic time complexity.
Recursive case
Compute the values for the top t=⌊n/Pi⌋ and bottom b=nmodPi halves.
Recursively obtain the decimal representation of the top half rtop=Convert(t,d−Si,i+1).
Recursively obtain the decimal representation of the bottom half rbottom=Convert(b,Si,i+1).
Pad rbottom with leading zeros to length Si.
Produce a combined result r=rtop⋈rbottom by concatenating the two halves.
Remove leading zeros from r, if any.
Return r.
The recurrence formula is T(β)=2T(β/2)+M(β). We have two variations on the recurrence using the fact that M(β)=Ω(β):
If M(β)=Θ(βlgβ) then by the Master theorem T(β)=Θ(M(β)lgβ).
If M(β)=Θ(β1+ϵ) for some ϵ>0 then by the Master theorem T(β)=Θ(M(β)) assuming M(β) satisfies the regularity condition (most standard multiplication/division algorithms do).
We can conclude that T(β)=O(M(β)lgβ) 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 (a=0∧d∣a)⟹∣d∣≤∣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 🌟
Proves that for all integers a,k,n we have gcd(a,n)=gcd(a+kn,n). Consequently, a=1 (modn)⟹gcd(a,n)=1.
Let d1=gcd(a,n) and d2=gcd(a+kn,n). We proceed by showing that d1∣d2 and d2∣d1, thus according to equation (31.5) they are equal.
We know that d1∣a and d1∣n, so by equation (31.4) we have d1∣a+kn. Based on Corollary 31.3 we can conclude that d1∣d2.
We know that d2∣n, so n=pd2 for some integer p. Furthermore, d2∣a+kn, thus a+kn=qd2 for some integer q. Therefore, a=qd2−kn=qd2−kpd2=(q−kp)d2 implies d2∣a. Based on Corollary 31.3 we can conclude that d2∣d1.
a=1 (modn)⟹a=1+kn, so gcd(1+kn,n)=gcd(1,n)=1 by equation (31.18). Consequently, we have gcd(a,n)=1.
31.2-4
Below is the iterative version of Euclid's algorithm in Python 3.
31.2-5
If b=0 then the call Euclid(a,b) immediately hits the base case and returns, so no recursive calls are made. Assume that b>0.
The steps to prove the first bound are as follows:
Let k be the number of recursive calls made by Euclid(a,b).
By Lamé's theorem, we know that b≥Fk+1.
By the standard bounds of the Fibonacci sequence, we know that Fk+1≥ϕk−1 for k≥1.
Combining these gives b≥ϕk−1, so k≤1+logϕb.
Let d=gcd(a,b). Because a=d⋅(a/d) and b=d⋅(b/d), the remainder at each step of the algorithm is also perfectly scaled by d:
Because every single division operation yields a remainder exactly d times larger than the reduced version, the sequence of recursive calls progresses identically. The call Euclid(a,b) will make the exact same number of recursive calls as Euclid(a/d,b/d). Now, proceeding in almost the same way as above, we can establish that the call Euclid(a,b) makes at most 1+logϕ(b/gcd(a,b)) recursive calls.
31.2-6
Assume that k>2, otherwise we would immediately reach the base case. Fk+1=Fk+Fk−1, hence by Exercise 31.1-1 we have that Fk−1=Fk+1modFk. Therefore, the call to Extended–Euclid(Fk+1,Fk) recurses into Extended–Euclid(Fk,Fk−1), and this process continues until hitting the base that happens right after the call to Extended–Euclid(F3,F2) . The final call Extended–Euclid(F2,0) returns a 3-tuple (1,1,0). Thus, gcd(Fk+1,Fk)=1. We now need to find out the coefficients x and y.
In general, Extended–Euclid(Fk+1,Fk) returns a 3-tuple (1,(−1)k+1Fk−2,(−1)kFk−1). We prove this by induction on k. The base case k=2 obviously holds. For k>2 the quotient q=⌊Fk+1/Fk⌋=⌊(Fk+Fk−1)/Fk⌋=1. Thus, (d,x,y)=(d′,y′,x′−y′) at all levels. By the inductive hypothesis the call to Extended–Euclid(Fk,Fk−1) returns a 3-tuple (1,(−1)kFk−3,(−1)k−1Fk−2), thus Extended–Euclid(Fk+1,Fk) returns
Observe in line 1 that (−1)k−1=(−1)k+1.
31.2-7
We proceed by induction on n. The base cases are when n=1∨n=2. If n=1 then we have nothing to permute and for n=2 we can apply equation (31.6).
For n>2 assume that the inductive hypothesis holds for n 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 ai and aj where 0≤i<j≤n. We have two cases:
If i>0 then it immediately follows by the inductive hypothesis that gcd(a0,a1,…,ai,…,aj,…,an)=gcd(a0,a1,…,aj,…,ai,…,an).
If i=0 then we have the following sequence of equivalent transformations:
Exercise 31.1-10 proves that the gcd operator is associative. This property extends to arbitrary number of arguments due to the generalized associative law.
Let dk=gcd(ak,dk−1) at some step 1≤k≤n. Based on Exercise 31.2-5, the number of divisions for this single step is bounded by
Taking into account all steps, we get a telescoping sum
31.2-8 🌟
Shows how to compute a generalized least common multiple using the binary gcd operator.
The base case is defined as (the RHS is an optimization to avoid an overflow error)
The generalization to arbitrary number of arguments is almost the same as with the generic gcd (see the previous exercise). Therefore,
If any ai=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=4 from the book. WLOG assume that indices are 0-based.
Constructing the derived pairs
Write each index i∈{0,1,…,k−1} into unique binary form using exactly r=⌈lgk⌉ digits, padding with leading zeros, as needed.
For each bit position 0≤j<r partition the set of input numbers into two groups:
Aj={ni:the jth bit from the right in the binary form of i is 1}
Bj={ni:the jth bit from the right in the binary form of i is 0}
For each j produce aj=∏n∈Ajn and bj=∏n∈Bjn, thus forming r pairs (aj,bj).
Proving correctness
If the set of input numbers are pairwise relatively prime, then gcd(ns,nt)=1 for s=t. Fix any j. Suppose for the sake of contradiction that some prime p∣aj and p∣bj. Consequently, there are some distinct numbers ns∈Aj and nt∈Bj such that p∣ns and p∣nt. But that contradicts the initial assumption about our set of input numbers. Thus, no such prime exists, and gcd(aj,bj)=1 for all j, as our choice of j was arbitrary.
If the derived pairs are relatively prime, then gcd(aj,bj)=1 for all pairs. Take any two distinct indices s and t that differ at bit position j. WLOG assume that s has 1 and t has 0. Therefore, ns∈Aj and nt∈Bj. Suppose for the sake of contradiction that some prime p∣ns and p∣nt. But we know that ns∣aj and nt∣bj, so this implies that p∣aj and p∣bj. 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 s and t was arbitrary.
Explaining the special case
For k=4 we see that A0={n1,n3}, A1={n2,n3}, B0={n0,n2} and B1={n0,n1}. Therefore, the derived pairs are (n1n3,n0n2) and (n2n3,n0n1). 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
Here are two possible mappings:
f(0)=1,f(1)=3,f(2)=4,f(3)=2
f(0)=1,f(1)=2,f(2)=4,f(3)=3
31.3-2
Subgroups of Z9 are {0}, {0,3,6} and Z9 itself. Subgroups of Z13∗ are {1}, {1,12}, {1,3,9}, {1,5,8,12}, {1,3,4,9,10,12} and Z13∗ itself.
31.3-3 🌟
Introduces the cancellation law from the parent group to prove properties of a subgroup.
We only need to show two things (since associativity is automatically inherited from ⊕):
The identity element e∈S′.
Every element a∈S′ has its inverse a−1∈S′.
Choose any fixed element a∈S′. Define a modular function fa:S′→S′ such that fa(x)=a⊕x (see also Exercise 31.3-5). Suppose fa(x)=fa(y) for some x,y∈S′. This means a⊕x=a⊕y. Because a,x and y also belong to the parent group S, we know a has an inverse a−1∈S. If we left-multiply both sides by a−1, we get x=y. Because fa(x)=fa(y)⟹x=y, the function fa is injective. Since S′ is finite, this entails that fa is surjective, too. In other words, fa is a bijection.
Because a∈S′ and fa is bijective, there must be some element b∈S′ that maps to a. This gives
In the parent group S, the only element that satisfies this equation is the identity element e. Therefore, b=e. Since b∈S′, we have proven that e∈S′.
Because e∈S′ and fa is bijective, there must be some element b∈S′ that maps to e. This gives
In the parent group S, the only element that satisfies this equation is the inverse element a−1. Therefore, b=a−1. Since b∈S′, we have proven that a−1∈S′.
31.3-4
Apply equation (31.21) to get
31.3-5
We proceed by showing the fa is a bijection. Any bijection from a set to itself is a permutation of that set. Since the domain and codomain of fa are of the same cardinality, it is enough to show that fa is a surjective function.
According to Theorem 31.13, Zn∗ is a finite abelian group. Consequently, a−1∈Zn∗. Select any b∈Zn∗. For a−1b∈Zn∗, we get
31.4-1
We have a=35, n=50 and b=10. The extended version of Euclid's algorithm returns (5,3,−2), so d=5 and x0=3(10/5)=6. The step size is 50/5=10, thus all solutions are {6,16,26,36,46}.
31.4-2
Based on Corollary 31.26, there is a multiplicative inverse a−1modn, so multiplying both sides of ax=ay (modn) by a−1 we get a−1ax=a−1ay (modn), which implies that x=y (modn).
If gcd(a,n)>1, then the previous conclusion is not valid. One counterexample is 2x=2y (mod4), where x=1 and y=3 is also a valid solution.
31.4-3
Yes, it will work, forcing the procedure to emit solutions in sorted order. Among those d distinct solutions, which are n/d positions apart from each other, one will always belong to the interval [0,n/d). Let it be xi=x′(b/d)+i(n/d) (modn) for some i∈[0,d). Exercise 31.1.-7 implies that xi=x′(b/d) (modn/d), hence setting x0=xi (modn/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 🌟
Explains the factor theorem applied modulo p.
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 p to be prime. For example, if p=8 then the polynomial f(x)=x2+7 (modp) has 4 roots {1,3,5,7} instead of t=2, where t is a degree of f.
We proceed by induction on degree t. The base case t=1 follows from Corollary 31.25.
For t>1, we have f(x)=(x−a)g(x), so f has at most 1+(t−1)=t distinct roots modulo p (after applying the inductive hypothesis on g(x)). Notice, that any other root of f is forced to be a root of g. Suppose f has another distinct zero b, such that b=a and
p∣(b−a)g(b)⟹p∣(b−a)∨p∣g(b) (recall that p is prime). Since b=a(modp), it must be that p∣g(b), so g(b)=0(modp). This concludes the proof for all t≥1.
31.5-1
We follow the process outlined in Theorem 31.27. We have x1=4, n1=5, x2=5, n2=11, m1=11, m1−1=1 (mod5), c1=11⋅1=11, m2=5, m2−1=9 (mod11) and c2=5⋅9=45. Thus, x=11⋅4+45⋅5 (mod55). All solutions are of the form 49+55k for arbitrary integers k.
31.5-2
We follow the steps outlined in Theorem 31.27. We have m1=56, m1−1=5 (mod9), c1=280, m2=63, m2−1=7 (mod8), c2=441, m3=72, m3−1=4 (mod7) and c3=288. Thus, x=1⋅280+2⋅441+3⋅288 (mod504). All solutions are of the form 10+504k for arbitrary integers k.
31.5-3
Let x=a−1 (modn). We simply employ equation (31.30) and Corollary 31.29 to get that ax=1 (modn) has a component-wise modular representation of (1,1,…,1), where every component is aixi=1 (modni). Therefore, xi=ai−1 (modni). This correspondence works in both directions.
31.5-4
Let n=∏i=1kni, 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) and its component-wise modular representation. Based on Corollary 31.29, we also have that f(x)=0 (modn) if and only if f(x)=0 (modni) for i=1,2,…,k. Furthermore, by the basic rules of modular arithmetic f(x)=0 (modni)⟺f(xmodni)=0 (modni).
Let a k-tuple (x1,x2,…,xk)∈Zn1×Zn2×⋯×Znk be one solution of the system of equations f(xi)=0 (modni) for i=1,2,…,k. Consequently, x∈Zn computed from inputs (x1,x2,…,xk), by following the steps from Theorem 31.27, represents a zero of f modulo n. By the fundamental counting principle, the total number of ways to form a valid k-tuple (x1,x2,…,xk) is exactly the product of the number of valid choices for each coordinate. Therefore, the number of roots modulo n equals the product of the number of roots modulo each ni.
31.6-1
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) of x∈Z11∗ are shown below.
1
0
2
1
3
8
4
2
5
4
6
9
7
7
8
3
9
6
10
5
31.6-2
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). Therefore, we can calculate the inverse by calling Modular-Exponentiation(a,ϕ(n)−1,n).
31.7-1
ϕ(n)=(p−1)(q−1)=280, so d=187 (mod280). The encryption of the message M=100, by equation (31.37), is P(100)=254. You can test that everything is proper by calling Modular-Exponentiation(254,187,319) (see Exercise 31.6-4) and getting back the original message 100.
31.7-2
Letβ denote the number of bits in n=pq and assume that p and q are large primes. As e=3, we have 3d=1 (modϕ(n)) that is equivalent to 3d−1=k(p−1)(q−1) for some integer k. Since 0<d<ϕ(n) we know that k=1∨k=2. We can rewrite the previous expression as 3d−1=k(n−(p+q)+1). Apparently, 3d−1<n⟹k=1 and 3d−1>n⟹k=2.
We can find x=p+q=n−(3d−1)/k+1. Plugging in q=n/p into the expression for x we get x=p+n/p⟹n=p(x−p). The last equation can be solved for p using the quadratic formula. Obviously, once we have p we can trivially find q.
All listed steps (including the integer square root) can be executed in time polynomial in β, hence the whole process is also doable in time polynomial in β.
★ 31.7-3
Suppose an attacker possesses a ciphertext CA=PA(MA). She/he executes the following procedure to reveal MA:
Generate a new message MR∈Zn∗.
Compute C=CAPA(MR).
Try to decrypt C and store the result into M.
If successful, then find MR−1 modulo n and compute MA=(MMR−1modn).
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 n 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 n, since the chance of stumbling across one that contains a prime factor of n is practically zero. All in all, an attacker can reveal the original message with high probability.
31.8-1
One example is n=7⋅5=35, where 6 is a nontrivial square root of 1 modulo 35, since 62=1 (mod35). Such a root always exists for n that satisfies the conditions from this exercise.
Decompose n into a product n1n2, where n1 and n2 are odd numbers greater than 1 that are relatively prime to each other. By Corollary 31.28, there exists an x simultaneously satisfying the equations
Corollary 31.29 gives that x=±1 (modn). On the other hand, we have
Corollary 31.29 gives that x2=1 (modn). Therefore, x is a nontrivial square root of 1 modulo n.
★ 31.8-2
In number theory, λ(n) is known as the Carmichael function.
Euler’s phi function is a multiplicative function that we can see after substituting n=∏i=1rpei to get
It follows that ϕ(n) is a multiple of λ(n), thus λ(n)∣ϕ(n). Another proof, based on group theory, is given here.
Suppose for the sake of contradiction that a Carmichael number n is not "square-free," thus it has a factor piei where ei>1. Consequently, pi∣ϕ(piei)⟹pi∣λ(n). Furthermore, pi∣n⟹pi∤n−1, which is a contradiction with the property that λ(n)∣n−1. Therefore, n must be "square-free."
Suppose for the sake of contradiction that a Carmichael number n is a product of two primes p<q. We have that λ(n)=lcm(p−1,q−1)⟹q−1∣λ(n). On the other hand, q−1∤n−1 because by the Division theorem we have n−1=pq−1=(q−1)p+(p−1). But this is again a contradiction with the property that λ(n)∣n−1. Therefore, n must be consisted of at least three primes.
31.8-3
By Exercise 31.6-2 we have x2=1 (modn)⟺n∣(x−1)(x+1). If x is a nontrivial square root of 1 modulo n, then by Corollary 31.35 n is composite, so 2<x<n−1 (you can easily check that x=2 produces a prime n=3). It follows that n∤x−1 and n∤x+1, thus prime factors of n are distributed between these two terms of a product. Therefore, gcd(x−1,n) and gcd(x+1,n) are both nontrivial divisors of n.
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).
b.
By equation (31.13), gcd(a,b) doesn't have a prime factor 2. Dividing b by 2 reduces the exponent of 2 in b, hence making it closer to zero. Consequently, gcd(a,b)=gcd(a,b/2).
c.
Let d1=gcd(a,b) and d2=gcd(a−b,b). We proceed by showing that d1∣d2 and d2∣d1, thus according to equation (31.5) they are equal.
We know that d1∣a and d1∣b, so by equation (31.4) we have d1∣a−b. Based on Corollary 31.3 we can conclude that d1∣d2
We know that d2∣a−b and d2∣b, so by equation (31.4) we have d2∣(a−b)+b=a. Based on Corollary 31.3 we can conclude that d2∣d1.
Since a−b is even and b is odd, the division of a−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<b≤a, so that q≥1.
a.
We do all the arithmetic in binary by processing the bits of a from the most significant to the least, accumulating the initial O(lgb) bits of a into a value (current residue r) greater or equal than b, 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 q is determined in one step of the algorithm. Thus, the number of steps required to compute all bits of q is proportional to it's number of bits ⌊lgq⌋+1=O(1+lgq). This expression is nonzero even if q≤1.
The algorithm still requires at least one step to compare a and b even if q=0 (when a<b). The expression 1+lgq=1+0=1 aims to encompass this case, despite lgq being undefined here.
At each step, the algorithm performs operations (e.g., comparisons or subtractions) involving the divisor b. Since it has approximately O(lgb) bits, these operations require that many bit operations per step. Therefore, the total count is O(1+lgq)×O(lgb)=O((1+lgq)lgb). 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 r gets calculated along the way (see also Exercise 31.1-12) and has O(lgb) 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+r, where 0≤r<b.
We have proven that O((1+lgq)lgb)=O(μ(a,b)−μ(b,r)).
c.
As shown in part (b), EUCLID takes at most c(μ(a,b)−μ(b,a modb)) bit operations on the first recursive call, at most c(μ(b,a modb)−μ(a modb,b mod(a modb))) on the second, and so on. This sum telescopes, hence we end up with cμ(a,b)−O(1)=O(μ(a,b)).
When applied to two β-bit inputs we get O(μ(a,b))=O((1+β)2)=O(β2).
31-3 Three algorithms for Fibonacci numbers
a.
The algorithmic recurrence of the running time is T(n)=T(n−1)+T(n−2)+Θ(1). We use a substitution method to prove that this version cannot do better than being exponential in n. The constant c1>0 denotes the lower bound in Θ(1).
Assume that T(n)≥cFn for n≥2.
This concludes the proof, as Fibonacci numbers grow exponentially.
b.
Here is the Python 3 script showcasing memoization. The recurrence T(n)=T(n−1)+Θ(1) resolves to 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 n, following the hint from the book.
The base case n=1 obviously holds. For n>1 we have
At the end, we need to read out the matching cell containing Fn.
d.
Fibonacci numbers have β=Θ(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(n−1)+Θ(n) that solves to T(n)=Θ(n2).
The matrix version employs both addition and multiplication of numbers, hence it has a new recurrence T(n)=T(n/2)+Θ(n2). Case 3 of the master theorem states that T(n)=Θ(n2).
31-4 Quadratic residues
a.
Theorem 31.32 implies that Zp∗ is a cyclic group, so it has a generator g, where all values gi modulo p are distinct for i=0,1,…,p−2. Every even index is associated with a quadratic residue, since (gi/2)2=gi (modp). Thus, the number of quadratic residues is at least (p−1)/2.
Suppose for the sake of contradiction that there is also some odd index i such that x2=gi (modp) for some x∈Zp∗. We must have that x=gj (modp) and g2j=gi (modp). Theorem 31.33 implies that 2j=i (modϕ(p)), where ϕ(p)=p−1. But this is impossible, since p−1∤2j−i due to mismatch in parity. We can conclude that the the number of quadratic residues is exactly (p−1)/2.
b.
If a is a quadratic residue modulo p, then there is an x such that
Otherwise, gi=a (modp) for some odd index i (see part(a)). According to Fermat's theorem gp−1=g0=1 (modp), so it must be that g(p−1)/2=−1 (modp), as powers of g are distinct for i=0,1,…,p−2. We have
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, then It requires O(β3) bit operations.
c.
By part (b), a(p−1)/2=a2k+1=1 (modp), so (ak+1)2=a2k+2=a⋅a2k+1=a (modp). Therefore, ak+1 is a square root of a modulo p. We can again use modular exponentiation to efficiently find it using O(β3) bit operations.
d.
Run the algorithm of part (b) by repeatedly trying random numbers a∈[2,p) until (pa)=−1 (modp). Half the elements in Zp∗ 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(β).
Last updated