
Tower of Hanoi
Tower of Hanoi
There is a legend about a Hindu temple in the Indian city of Benares,
whose priests, the Brahmins, are engaged in moving a tower of 64 gold
disks.
The discs lie on three diamond needles, a cubit long and as thick as the body of a bee.
The object is to move the entire tower of discs to another needle,
one disk at a time where
no larger disk may rest on a smaller one ever.
And then, according to legend, the world comes to an end when the work is done.
Assuming the priests move 1 checker per second, it'll take 2 ** 64 − 1,
which is about 1.84×10 ** 19 seconds
to complete the task.
This corresponds to roughly 585 billion years,
about 43 times the estimated age
of the universe.
The game was invented by the French mathematician Édouard Lucas in 1883.
It is not clear whether Lucas invented this legend or was merely inspired by it.
The game consists of a board with three pins on it and a number of discs
of different diameters,
each having a hole in the middle.
At the start of the game, a cone-shaped tower of a number of discs is placed on the
first pin in such a way
that there is always a smaller disk on top of a larger disk,
the other two pins stay empty.
The object of the game is now to move the entire tower of discs to the last pin,
observing the following 2 rules:
1. Only the top disk of any tower may be moved, one disk at a time.
2. A disk can be moved to any pin, but a larger disk never may rest on a smaller one.
The 3 pins are numbered 1, 2 and 3.
Your task is to write a generator function (named hanoi)
that takes the number of disks as input and from where and
to which pin a disk should be moved to move the entire tower from the first pin [1]
to the last pin[3]
in as less moves as possible as an output.
Input: The number of discs in play as an integer.
Output: A series tuples, containing two integers indicating from and to which pin
a disk shall be moved to complete
the task in a minimum number of moves as a generator object.
example: hanoi(2) generates (0, 1), (0, 2), (1, 2)