Professional Documents
Culture Documents
Chapter 13 - Algo Design and Recursion
Chapter 13 - Algo Design and Recursion
An Introduction to
Computer Science
Chapter 13
Algorithm Design and Recursion
curr = 1
prev = 1
for i in range(n-2):
curr, prev = curr+prev, curr
return curr
Note the use of simultaneous assignment to
calculate the new values of curr and prev.
The loop executes only n-2 since the first two
values have already been “determined”.
>>> hanoi(3)
Move disk from A to C.
Move disk from A to B.
Move disk from C to B.
Move disk from A to C.
Move disk from B to A.
Move disk from B to C.
Move disk from A to C.