The puzzle Tower of Hanoi was introduced in 1883. It consisted of three pegs fastened to a stand. There were eight circular disks of wood, each of which had a hole in the middle through which a peg could be passed. These disks were of different radii and initally they were all placed on one of the pegs with the biggest disk on the bottom and successively smaller disks on the top.
The problem was to shift all of the disks from the initial peg to any other peg according to the following two rules:
This puzzle came with a story that was published by De Parville in La Nature, Paris, 1884, pp. 285-286.
De Parville gave an account of the origin of the toy which is a sufficiently pretty idea to deserve repetition. In the great temple at Benares, says he, 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 disks of pure gold, the largest disk resting on the brass plate, and the others getting smaller and smaller up to the top one. This is the Tower of Brahma. Day and night unceasingly the priests transfer the disks from one diamond needle to another according to the fixed and immutable laws of Brahma, which require that the priest on duty must not move more than one disk at a time and that he must place this disk on a needle so that there is no smaller disk below it. When the sixty four disks 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.
The number of separate transfers of single disks which the priests must make to effect the transfer of the tower is 264 - 1 , that is 18,446,744,073,709,551,615. If each transfer takes 1 second, then the time taken is 585 billion years where our current estimate of the age of the universe is 13.8 billion years.
This was going on for eons, until one day one of the priests who was more computationally minded than the others informed his fellow-priests that they could achieve the transfer in a single afternoon at the one disk-per-second rhythm by using an additional needle. He proposed the following strategy having four needles - source, spare1, spare2, and destination.
He calculated the value of k which minimized the number of movements and found that 18,433 transfers would suffice. Thus they could spend just 5 hours, 7 minutes, and 13 seconds with this scheme versus 585 billion years without the additional needle.
Implement this priest / computational scientist's algorithm. The value
of k that minimizes the number of transfers is
k = n - √(2 * n + 1) + 1
Use this value of k to compute the the total number of transfers
using four needles where the priest can move only one disk at a time
and must place each disk on a needle such that there is no smaller
disk below it.
Input: You must read the input file from stdin. The input file contains several lines of input. Each line contains a single integer 0 ≤ N ≤ 10,000 giving the number of disks to be transferred. Input is terminated by the end of file.
1 2 28 64
Output: For each line of input produce one line of output which indicates the number of movements required to transfer N disks to the final needle using four needles. For the above input file, the output will be:
1 3 769 18433
Here is a template of the code that you will find useful. You will be making additions to the code to fulfill the requirements. Feel free to write auxiliary functions if you need them.
In the function main() you will read the number of disks from a file tower.in. Assume that the number of disks is between 0 and 10000 inclusive. You do not have to do any error checking.
The file that you will be submitting will be called Tower.py. We will be looking for good documentation, descriptive variable names, clean logical structure, and adherence to the standard coding conventions in Python.
You may work with a partner on this assignment. Both of you must read the paper on Pair Programming and abide by the ground rules as stated in that paper. The file will have a header of the following form:
# File: Tower.py # Description: # Student's Name: # Student's UT EID: # Partner's Name: # Partner's UT EID: # Course Name: CS 313E # Unique Number: # Date Created: # Date Last Modified:
Use the Canvas system to submit your Tower.py file. We should receive your work by 11 PM on Monday, 07 Mar 2022. There will be substantial penalties if you do not adhere to the guidelines. Remember Python is case sensitive. The name of your file must match exactly what we have specified.