Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Towers of Hanoi: eager moves


1. Towers of Hanoi: eager moves

2. Towers of Hanoi
Suppose that one wants to return a list of the 10 first moves for a Towers of Hanoi problem with 5 disks (31 moves total). Note: The problem is more general and the numbers chosen are for example purposes only.

As a starting point, below is a solution to the Towers of Hanoi problem for 5 disks.

3. Five disks
Here is the JavaScript code.

Here is the output of the JavaScript code.


4. Eager approach
One eager approach is to generate a list of all moves and pick out the first 10. Here is the JavaScript code.

Here is the output of the JavaScript code.


5. Lazy approach
A lazy approach is to use a (global )variable stop when the limit of 10 has been reached.

Note that since any recursive call could cause the list to grow to the required size, there are three checks to insure that more moves are needed. Here is the JavaScript code.

Here is the output of the JavaScript code.


6. End of page