518 ANSWERS TO EXERCISES 3.1 Notes: Brent has also considered a more general method in which the successive values of Y = X,% satisfy ni = 0, ni+l = 1 + [pni] where p is any number greater than 1. He showed that the best choice of p, approximately 2.4771, saves about 3 percent of the iterations by comparison with p = 2. (See exercise 4.5.4-4.) The method in part (b) has a serious deficiency, however, since it might generate a lot of nonrandom numbers before shutting off. For example, we might have a particularly bad case such as X = 1, p = 2k. A method based on Floyd s idea in exercise 6(b), namely one that maintains Y = X sn and X = X, for n = 0, 1, 2, 9 will require a few more function evaluations than Brent s method, but it will stop before any number has been output twice. On the other hand if f is unknown (e.g., if we are receiving the values Xc, Xi, . . on-line from an outside source) or if f is difficult to apply, the following cycle detection algorithm due to R. W. Gosper will be preferable: Maintain an auxiliary table To, Tl, I T,, where m = Llg n] when receiving X,. Initially TO + X0. For n = 1, 2, .I compare X, with each of TO, . . . , Tllgnl; if no match is found, set T,(,) +-X,, where e(n) = max{ e 1 2e divides n + 1 }. But if a match X, = Tk is found, then X = n -max{ m ] m < n and e(m) = k}. This procedure does not stop before generating a number twice, but at most [3x] of the X s will have occurred more than once. [MIT AI Laboratory Memo 239 (Feb. 29, 1972), Hack 132.1 See also R. Sedgewick and T. G. Szymanski, Proc. ACM Symp. Th. Comp. 11 (1979) 74-80. 8. (a,b) OO,OO,. . . [62 starting values]; lO,lO,. . [19]; 60,60,. [15]; 50,50,. . [l]; 24,57,24,57,. . . [3]. (c) 42 or 69; these both lead to a set of fifteen distinct values, namely (42 or 69), 76, 77, 92, 46, 11, 12, 14, 19, 36, 29, 84, 05, 02, 00. 9. Since X < b , we have X2 < bzn, and the middle square is [X2/bnj 5 X2/b . If X > 0, then X2/bn < Xb lb = X. 10. If X = abn, the next number of the sequence has the same form; it is (a2 mod b )b . If a is a multiple of b or of all the prime factors of b, the sequence will soon degenerate to zero; if not, the sequence will degenerate into a cycle of numbers having the same general form as X. Further facts about the middle-square method have been found by B. Jansson, Random Number Generators (Stockholm: Almqvist & Wiksell, 1966), Section 3A. Numerologists will be interested to learn that the number 3792 is self-reproducing in the four-digit middle-square method, since 3792 = 14379264; furthermore (as Jansson has observed), it is self-reproducing in another sense, too, since its prime factorization is 3 79.24! 11. The probability that X = 1 and p = 0 is the probability that Xi = Xc, namely l/m. The probability that X = 1,~ = 1, or X = 2,~ = 0, is the probability that XI # Xc and that XZ has a certain value, so it is (1 -l/m)(l/m). Similarly, the probability that the sequence has any given ,U and X is a function only of p + X, namely P(P,1) = ; l