The New Millenium - The End of the World?

With the new millenium approaching fast (by the way, what is the first year of the new millenium?), scary stories about the end of the world multiplicate like mushrooms after a healthy rain.

Of course there are plenty of arguments pro and against such a terrible claim. While some people have the end of the century and of this millenium as the time target for their predictions, others have different ideas. Here is a (made up) legend about the end of the world.

``In the great temple at Benares, beneath the dome which marks the centre of the world, rests a brass plate in which are fixed three diamond needles, each a cubit high and as thick as the body of a bee. On one of these needles, at the creation, God placed sixty-four discs of pure gold, the largest disc resting on the brass plate, and the others getting smaller and smaller up to the top one. This is the Tower of Bramah. Day and night unceasingly the priests transfer the discs from one diamond needle to another according to the fixed and immutable laws of Bramah, which require that the priest on duty must not move more than one disc at a time and that he must place this disc on a needle so that there is no smaller disc below it. When the sixty-four discs shall have been thus transferred from the needle on which at the creation God placed them to one of the other needles, tower, temple, and Brahmins alike will crumble into dust, and with a thunderclap the world will vanish.'' [1]

This story is also known as the `Towers of Hanoi" problem. We wonder whether it is indeed possible to transfer the disks from one needle to another following the rules, and most important, how much this would take.

We will denote the three diamond needles by A, B and C. Initially the disks are on A. Their final position will be C. The figure below only shows four golden disks in their initial position.

Your main job is to find a computer algorithm which would do the same job the monks do. Here is a detailed description of what you have to do:

1. Implement the algorithm using C/C++. The program, called hanoi, will accept command line arguments as follows

   hanoi -n disks [ -v ] [ -h ]

where the square brackets indicate that the parameter is optional, the user may or my not use it. disks indicate the number of disks to start with on needle A. The flag -v indicates that the output should be verbose, your program will print a line at each move, indicating what move is performed as in this sample output:

   move disk 1 from A to B
   move disk 2 from A to C

When the -h flag is used in the command line, the program prints a statement about how the program should be used and stops.

The smallest disk is number 1. After the disks have been all moved, the program prints the number of moves it took to complete the job and the running time.

2. Find out how many moves are necessary to solve the problem if there are N disks.

3. Find the time complexity of the algorithm by measuring the running time (without the -v command line flag) for a number of disks between 10 and 25.

4. Assuming that the monks have already moved 32 disks from A to C, and that they move one disk every second, day and night with no rest whatsoever, when is the world set to colapse?


What you'll turn in

  1. A well commented listing of your program. Note that grading, as described below, considers both the functionality and the readability of your code. You may also want to have a look at a grading sheet used in the past for a similar assignment. If you are unsure about what the documentation of a program means, then you may want to read this document.
  2. A floppy disk (3.5") which contains the source program, the executable and a README file indicating what the program does, how to build the executable, the platform it's been tested on, and how to run it, plus any other information you consider useful.
  3. A memo with your findings. You will indicate what you have looked for and what you have found. Direct the memo to your class instructor. A hardcopy of the program and the tables with your measurements will be attached to the memo.

Grading

  1. A mark between 0 and 10 for functionality. The mark basically indicates in what measure your program works properly.
  2. A mark between 0 and 10 for readability. This mark indicates how well documented your program is.
  3. Multiply the above two marks to get the mark for this programming assignment.

Varia

The algorithm is a direct application of recursion. Before you write the program try to understand how it works by using a small number of disks (1, 2, 3, ...)


References

[1] R. R. W. Ball, Mathematical Recreations and Essays, 1982, Macmillan, London.


(c) 1996, Virgil Bistriceanu - virgil@csam.iit.edu
Posted: September 14, 1996
Last update: September 14, 1996