What mathematical puzzle that consists of three pegs and a number of disks of different sizes in which the objective is to move the entire stack of disks from one peg to another is used in computer science to teach recursion to students of programming?
Recursion (computer science) algorithms are very similar in their steps. The Towers of Hanoi is a mathematical puzzle whose solution illustrates recursion. There are three pegs which can hold stacks of disks of different diameters. A larger disk may never be stacked on top of a smaller. Starting with "n" disks on one peg, they must be moved to another peg one at a time. What is the smallest number of steps to move the stack? "Function definition": "Recurrence relation for hanoi": Example implementations: Although not all recursive functions have an explicit solution, the Tower of Hanoi sequence can be reduced to an
Tower of Hanoi is always one possibility, except when all disks are on the same peg, but in that case either it is the smallest disk that must be moved or the objective has already been achieved. Luckily, there is a rule that does say where to move the smallest disk to. Let "f" be the starting peg, "t" the destination peg, and "r" the remaining third peg. If the number of disks is odd, the smallest disk cycles along the pegs in the order "f" → "t" → "r" → "f" → "t" → "r", etc. If the number of disks is