<<

Algorithms and Data Structures 2014 Exercises week 12

1 Division

Give a division that computes both quotient and remainder, that is, given two numbers Pn+m−1 i Pn−1 i A = i=0 aiβ en B = i=0 biβ , your algorithm should return two numbers D en R such that A = D × B + R, of course with 0 ≤ R < B. Your algorithm should be based on the standard schoolbook method, also known as a ‘staartdeling’.

2 Fast

1. Give an algorithm that computes ak efficiently. Assume that both a and k are relatively small (i.e. they both fit in a single integer variable), but the result could be large. Hence you will need some big integer representation. Determine the time complexity of your algorithm as accurate as possible. You may assume that has complexity O(nα), α ≥ 1. 2. A standard implementation of the factorial function will involve n to com- pute n!. Again, if n is large enough, a 32 or 64 bits representation for the result will not be sufficient. Since you only need to be able to multiply a large number with a small num- ber, multiplication can be done in O(n) time. A more efficient method for computing n! can be obtained by using the prime factorization of 1 × 2 × · · · × n, that is, write n! as n2 n3 n5 2 × 3 × 5 × · · ·, for some n2, n3, n5,..., and use the previous operation to compute the powers. Give an algorithm that computes n! in this way.

3 Karatsuba

A naive implementation of Karatsuba’s multiplication method uses 3 auxiliary variables to store the intermediate results during each recursive step in the computation. Modify this naive al- gorithm such that O(n) space is reserved beforehand which is then carefully used during the computation itself. Any additional space allocations should be avoided. k/2 k/2 Hint: Suppose you are multiplying two k-digit numbers A = a0 + a1 · β and B = b0 + b1 · β . The result will be a 2k-digit number. Instead of allocating an array with 2k elements for storing this result you should create an array, say D, of size 6k. Then you can use the following layout for D not only for the final result, but also for storing intermediate values.

D[0..k − 1] = a0 × b0 D[k..2k − 1] = a1 × b1 D[2k..3k − 1] = x1 × x2 D[3k..5k − 1] = temporary space for recursion D[5k..11k/2 − 1] = a0 + a1 = x1 D[11k/2..6k − 1] = b0 + b1 = x2

4 Toom-Cook3

Karatsuba’s method generalizes to what is known as Toom-Cook r-way multiplication. Divide both multiplication operands A and B into r parts, combine these parts in a clever way, apply the algorithm recursively to these combinations, and compose the final results out of these in- termediate results. The case r = 2 corresponds to Karatsuba’s algorithm, and the case r = 3 is known as Toom-Cook 3-way, sometimes simply called the Toom-Cook algorithm. For r = 3 you 2 2 k write A = a0 + a1 · x + a2 · x ,B = b0 + b1 · x + b2 · x with x = β . The first step is to combine the coefficients in the following way: x0 = a0 y0 = b0 x1 = a0 + a1 + a2 y1 = b0 + b1 + b2 x2 = a0 − a1 + a2 y2 = b0 − b1 + b2 x3 = a0 + 2 · a1 + 4 · a2 y3 = b0 + 2 · b1 + 4 · b2 x4 = a2 y4 = b2

Then you compute vi = xi × yi, for each i. 2 3 4 Finally, the result C = c0 + c1 · x + c2 · x + c3 · x + a4 · x is obtained as follows:

t1 = (3 · v0 + 2 · v2 + v3)/6 − 2 · v4 t2 = (v1 + v2)/2 c0 = v0 c1 = v1 − t1 c2 = t2 − v0 − v4 c3 = t1 − t2 c4 = v4

1. Verify that the division in the computation of t1 and t2 are exact. We call a division exact if the remainder is zero. 2. Show that the final result is correct.

3. Determine the time complexity of this method. 4. Give a based on this method.