Quote:
Could anybody give me some pointers?
The point of the invariant is to see the iteration as changing the variables in the invariant in such a way that the invariant, erm, remains invariant. At the end, one of the variables in the invariant will be the answer you seek.
Let's say we want to find the answer to b ^ n0. Our iterative version will use three variables: a, b and n. The invariant will be the expression:
a * (b ^ n) ;; In general= 1 * (b ^ n0) ;; The initial condition=a * (b ^ 0) ;; The stop condition
Obviously, when n=0, a = b ^ n0 must hold, and a is the answer we seek.
Our algorithm will change the values of a and n until the halting condition (n = 0) holds.
IF our iterative algorithm preserves the invariant a * (b ^ n) at every step, then a MUST be the correct answer when n = 0. That's why the invariant is useful. Now, all that remains is to find a way so that n gets smaller at each step and a gets larger
in a way that preserves the invariant.
The recursive version should give you a BIG hint on how to do this.