336 ARITHMETIC 4.5.2 This completes our analysis of (Web hosting services)
Thursday, January 31st, 2008336 ARITHMETIC 4.5.2 This completes our analysis of the average values of C and D. The other three quantities appearing in the running time of Algorithm B are rather easily analyzed; see exercises 6, 7, and 8. Thus we know approximately how Algorithm B behaves on the average. Let us now consider a worst case analysis: What values of u and v are in some sense the hardest to handle? If we assume as before that LlguJ = m and 1kvJ = n, let us try to find (u, v) that make the algorithm run most slowly. In view of the fact that the subtractions take somewhat longer than the shifts, when the auxiliary bookkeeping is considered, this question may be rephrased by asking for the inputs u and v that require most subtractions. The answer is somewhat surprising; the maximum value of C is exactly max(m, n) + 1, (46) although the lattice-point model would predict that substantially higher values of C are possible (see exercise 26). The derivation of the worst case (46) is quite interesting, so it has been left as an amusing problem for the reader to work out by himself (see exercises 27 and 28). EXERCISES 1. [A&21] How can (8), (9), (lo), (11) and (12) be derived easily from (6) and (7)? 2. [A4.86] Given that u divides zlizl~ . . . v,, prove that u divides gcd(u, VI) gcd(u, ~2). . . gcd(u, v,). 3. [Mz%] Show that the number of ordered pairs of positive integers (u, v) such that lcm(u, v) = n is the number of divisors of n2. 4. [A&Z] Given positive integers u and v, show that there are divisors u of u and v of v such that gcd(u , v ) = 1 and u v = lcm(u, v). b 5. [AL%] Invent an algorithm (analogous to Algorithm B) for calculating the greatest common divisor of two integers based on their balanced ternary representation. Dem- onstrate your algorithm by applying it to the calculation of gcd(40902,24140). 6. [MZ?] Given that u and v are random positive integers, find the mean and the standard deviation of the quantity A that enters into the timing of Program B. (This is the number of right shifts applied to both u and v during the preparatory phase.) 7. [A4,20] Analyze the quantity B that enters into the timing of Program B. b 8. [MEY] Show that in Program B, the average value of E is approximately equal to +C,,,, where C,,, is the average value of C. 9. [18] Using Algorithm B and hand calculation, find gcd(31408,2718). Also find integers m and n such that 31408m + 2718n = gcd(31408,2718), using Algorithm X.