
Therefore the minimum number of moves needed to solve the puzzle with N disks equals T N-1 + 1 + T N-1 = 2T N-1 + 1 moves. One then needs exactly T N-1 more steps to finish the task. It takes just one move to move the biggest disk from Src to Dst. Since we are trying to use as few steps as possible, we may assume that that portion of the task took exactly T N-1 moves. Indeed, when the time comes to move the bottom disk, (N - 1) smaller disks will have been moved from Src to Aux in at least T N-1 moves. The inequality suggests that it might be possible to move N disks with fewer than 2T N-1 + 1 moves. The recursive solution above involves moving twice (N - 1) disks from one peg to another and making Now let us try to derive a general formula. A trained mathematician would also note that T 0 = 0. One can easily convince oneself that T 2 = 3 and T 1 = 1. From the previous section T 3 = 7 and T 4 = 15. Let T N be the minimum number of moves needed to solve the puzzle with N disks. Of course "Move" means moving the topmost disk.

To me the sheer simplicity of the solution is breathtaking. The function is recursive in that it calls itself repeatedly with decreasing values of N until a terminating condition (in our case N = 0) has been met. This actually serves as the definition of the function Solve. Then the body of the function might look like Move N - 1 disks from Aux to Dst (using Src as an intermediary peg)Īssume there is a function Solve with four arguments - number of disks and three pegs (source, intermediary and destination - in this order).Move the top N - 1 disks from Src to Aux (using Dst as an intermediary peg).

Hanoi towers function how to#
Know how to accomplish the following tasks: Therefore, for a given number N of disks, the problem appears to be solved if we After moving the bottom disk from Src to Dst these disks will have to be At this point in time all the remaining disks will have to be stacked However one solves the problem, sooner or later the bottom disk will To better understandĪnd appreciate the following solution you should try solving the puzzle for small number of disks, Let call the three pegs Src (Source), Aux (Auxiliary) and Dst (Destination). Try IE11 or Safari and declare the site as trusted in the Java setup.

If you are reading this, your browser is not set to run Java applets. The applet expects you to move disks from the leftmost peg to the rightmost peg. You can drop a disk on to a peg when its center is sufficiently close to the center of the peg. To solve the puzzle drag disks from one peg to another following the rules. The applet has several controls that allow one to select the number of disks and observe the solution in a Fast or Slow manner. Its solution touches on two important topics discussed later on: The puzzle is well known to students of Computer Science since it appears in virtually any introductory text on data structures or algorithms. The objective is to transfer the entire tower to one of the other pegs (the rightmost one in the applet below), moving only one disk at a time and never a larger one onto a smaller. We are given a tower of eight disks (initially four in the applet below), initially stacked in increasing size on one of three pegs. The Tower of Hanoi puzzle was invented by the French mathematician Edouard Lucas in 1883.
