19
Exercise 1.19: There is a clever algorithm for computing the Fibonacci numbers in a logarithmic number of steps. Recall the transformation of the state variables a and b in the fib-iter process of 1.2.2: a ← a + b and b ← a. Call this transformation T, and observe that applying T over and over again n times, starting with 1 and 0, produces the pair Fib(n+1) and Fib(n). In other words, the Fibonacci numbers are produced by applying Tn, the $| n^{th} |$ power of the transformation T, starting with the pair (1, 0). Now consider T to be the special case of p=0 and q=1 in a family of transformations Tpq, where Tpq transforms the pair (a,b) according to a←bq+aq+ap and b←bp+aq. Show that if we apply such a transformation Tpq twice, the effect is the same as using a single transformation Tp′q′ of the same form, and compute p′ and q′ in terms of p and q. This gives us an explicit way to square these transformations, and thus we can compute $| T^n |$ using successive squaring, as in the fast-expt
procedure. Put this all together to complete the following procedure, which runs in a logarithmic number of steps:
fast-expt
procedure. Put this all together to complete the following procedure, which runs in a logarithmic number of steps:T is the transformation of applying
Tpq is
and T is T10 actually.
Applying Tpq twice, it transforms (a, b) into (a2, b2)
let p′ = pp + qq, and q′ = 2pq + qq, we can simplify the above to
That is to say: $| T_{pq}^{2} = T_{p'q'} |$.
The completed procedure is
Last updated
Was this helpful?