Unformatted text preview: Fibi computes each value in the series exactly once, and so its running time is proportional to n . 2.11 // Array curr[i] indicates current position of ring i. void GenTOH(int n, POLE goal, POLE t1, POLE t2, POLE* curr) { if (curr[n] == goal) // Get top n1 rings set up GenTOH(n1, goal, t1, t2, curr); else { if (curr[n] == t1) swap(t1, t2); // Get names right // Now, ring n is on pole t2. Put others on t1. GenTOH(n1, t1, goal, t2, curr); move(t2, goal); GenTOH(n1, goal, t1, t2, curr); // Move n1 back } } 2.12 At each step of the way, the reduction toward the base case is only half as far as the previous time. In theory, this series approaches, but never reaches, 0, so it will go on forever. In practice, the value should become computationally indistinguishable from zero, and terminate. However, this is terrible programming practice....
View
Full Document
 Fall '08
 BELL,D
 Recursion, Void type, Long integer, Pole, Fibi, pole t2

Click to edit the document details