We just covered the Towers of Hanoi for Case 1 and Case 2, let's return to 3 disks. Can you describe an algorithm to move the 3 disks from a starting peg to the goal peg? You can refer back to solved cases when needed.

The Algorithm for 3 Disks

Now that we've covered 1, 2 and 3 disks, can you figure out an algorithm for solving the towers of Hanoi for any number of N disks? The Algorithm for N Disks

To solve it for N disks, you just move N-1 disks off. How do you move them? Refer to N-1-1. How do you solve that? Just refer to the case below that... Keep on going until you reach a base case. Then the results will return up the stack and you'll get a complete answer.

Sound familiar? This is similar to how Factorial was calculated, and is a classic case of Recursion. In this case recursion is crucial, since it would be quite difficult to follow the same algorithm without it.

Create a program that solves the tower of Hanoi. You will be given one number as input - the number of disks on Peg 1. Create and call a method that prints out the correct steps to solve the puzzle.

Output Format: Print the peg to move from, an arrow "->", and the peg to move to. For example, to move from peg 1 to peg 3, print:

Print all the steps for a given case on its own line.

Create a program that solves the Towers of Hanoi and prints out the solution.
[null, {"document_url": "https://www.learneroo.com/modules/71/nodes/403", "unformatted_src": "https://s3.amazonaws.com/learneroo-images/main/Turtles_all_the_way_down.png", "src": "https://s3.amazonaws.com/learneroo-images/main/Turtles_all_the_way_down.png", "formatted_filename": "Turtles all the way down", "alt_text": "towers of turtles"}, null]