Towers of Hanoi
From Math Images
- Towers of Hanoi is a well known puzzle game publicized in 1883 by Édouard Lucas, a French mathematician. According to legend, priests of the Hindu god Brahma were instructed to move 64 golden disks from one of 3 poles to another, and when they completed it, the world would end.^{[1]}
Towers of Hanoi |
---|
Contents |
Basic Description
The standard game begins with 3 poles, one of which (the starting pole) has some natural number, k, disks placed around it in increasing size from top to bottom, while the other poles start out empty.- Goal: Move all k disks from the starting pole (typically the leftmost) to the specified end pole (typically the rightmost), following these rules:
- Only one disk may be moved at one time.
- Each move consists of moving the top disk of one pole onto the top of another pole.
- Each disk must be placed on a larger disk.
The game is solvable and has a shortest solution (fewest moves) for any number of disks. To fully understand the game's legend, one must understand the relationship between the number of disks and the number of moves. As it turns out, this relationship is exponential (see A More Mathematical Section for details).
We can see the effects of this exponential relationship in the legend about the priests, who had to move 64 disks from one pole to the other. If they were to move one disk per second, then they would need 18,446,744,073,709,551,615 seconds, or 584.54 billion years, to complete the game. Given that this is about 58.5 times the life span of the Sun^{[2]}, the legend probably overestimates the amount of time we have until the world ends (unless we can survive without the Sun and the Earth).
Play the Game
You can play the game Towers of Hanoi right here! This applet will help you understand how the game works.
Example of Solving the Game
Solving Towers of Hanoi with 4 disks.
A More Mathematical Explanation
The Recursive Solution
This game becomes more interesting when we try to figure out the shortest [...]The Recursive Solution
This game becomes more interesting when we try to figure out the shortest solution for any number of disks. We will first state an algorithm that solves the game, then show that this algorithm is the shortest solution. We will refer to the start pole as Pole A, the end pole as Pole C, and the remaining pole as Pole B. Define T_{k} to be the number of moves our algorithm needs to solve the game for k disks. To solve the game with 1 disk, simply move the disk from Pole A to Pole C, so
- .
Assume that we know an algorithm to move k -1 disks for k > 1 (so we also know the value of T_{k-1}). To solve the game with k disks, follow these steps:
- 1. Move the top k -1 disks on Pole A to Pole B using our algorithm, taking T_{k-1} moves, as in Image 1.
- 2. Move the k^{th} disk to Pole C, taking 1 move, as in Image 2.
- 3. Move the k -1 disks on Pole B on top of the k^{th} disk on Pole C using our algorithm, taking T_{k-1} moves.
Hence we can describe the number of moves in our algorithm by the recursive formula:
We will now show by induction that our algorithm uses the least number of moves. The base case (T_{1} = 1) is satisfied because there is no way to solve the game without moving any disks. Assume that our algorithm takes the least number of moves to solve for k -1 disks for k > 1, so T_{k-1} is minimal.
To solve the game with k disks, the k^{th} disk must be moved at some point, and moving it requires the top k -1 disks to be on the pole that is not the starting or ending pole. Since our algorithm needs the least number of moves to solve for k -1 disks, we will assume that moving the top k -1 disks takes T_{k-1} moves.
Note that it will be most efficient to move the kth disk directly from pole A to pole C. The other option would be to move the other k-1 disks to pole C, move the kth disk to pole B, then move the other k-1 disks back to pole A before the kth disk can reach pole C. This requires transferring those k-1 disks three times (to pole C, then pole A, then back to C), whereas our method of moving the kth disk directly to pole C only requires two such transfers.
Thus the minimum number of moves required to solve for k disks assuming T_{k-1} is minimized is
- .
The Explicit Formula
We will show by induction that the explicit formula for the number of moves in this minimal algorithm is
- , where k is the number of disks.
The base case is satisfied:
- as desired.
Assume the (k -1)^{th} case holds. Then
Thus we have shown the minimal algorithm for any Towers of Hanoi game with k disks requires T_{k} = 2^{k} – 1 steps.
Back to the Legend
Now that we have the equation to determine the number of moves needed for any number of disks, we can verify the time we said was needed to complete the game in the legend. A Towers of Hanoi game with 64 disks will take:
If the priests move 1 disk per second, then they will need:
Assuming that the Sun has a life span of 10 billion years^{[2]}, the time predicted by the legend is times longer than the Sun's lifespan.
Why It's Interesting
Towers of Hanoi is a popular example in introductory computer science courses because programming it is instructional but relatively simple. The mathematical nature of the game teaches how to create recursive programs^{[3]}, while the simple user interface of the game (which can be made using only rectangles) introduces the basics of programming graphics. In addition, the game's exponential solution is an example of why computer scientists dislike exponential time; it takes far too long to compute. Assuming the average computer can do 100 million operations per minute, then solving the game with 64 disks would take the computer 5845.42 years to complete.Towers of Hanoi is also a popular tool in psychological research; it can test problem solving capabilities.^{[4]} Consequently, the game is often used to test intelligence. In the 2011 film Rise of the Planet of the Apes, scientists test the intelligence of apes with Towers of Hanoi.^{[5]} The faster the apes solve the game, the more intelligent they are perceived to be.
Teaching Materials
- There are currently no teaching materials for this page. Add teaching materials.
References
- ↑ http://www.lawrencehallofscience.org/java/tower/
- ↑ ^{2.0} ^{2.1} http://helios.gsfc.nasa.gov/qa_sun.html
- ↑ http://apcentral.collegeboard.com/apc/members/courses/teachers_corner/45418.html
- ↑ http://cognitivepsychology.wikidot.com/problem-solving:tower-of-hanoi
- ↑ http://www.imdb.com/title/tt1318514/synopsis
Leave a message on the discussion page by clicking the 'discussion' tab at the top of this image page.