TOWER OF HANOI GAME
Introduction
The tower of Hanoi (also called the Tower of Brahma or the Lucas tower) is a mathematical game or a puzzle.
invented by the French mathematician Edouard Lucas and sold as a toy in 1883.
It consists of three rods and a number of disks of different sizes which can slide onto any rod.
History
There is a history about an Indian temple in Kashi Vishwanath, which contains a large room with three time-worn posts in it surrounded by 64 golden disks. Brahmin priests, acting out the command of an ancient prophecy, have been moving these disks, in accordance with the immutable rules of the Brahma, since that time.
According to the legend, when the last move of the puzzle will be completed, the world will end.
The number of moves required to correctly move a tower of 64 disks is
264 _1=18,446,744,073,709,551,615.
At the rate of one move per second, that is 584,942,417,335 years.
Objective and Procedure
The objective of this game is to move the disks one by one from the first peg(rod) to the last peg(rod).
The number of disks can vary, but there are only three rods.
The game starts by having a few disks stacked in increasing order of size.
Start the game with your two smallest disks. Stack them on the rods B and C, with the smaller disk on top of the larger disk.
Rules
Only one disk can be moved at a time.
Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e a disk can only be moved if it is the uppermost disk on a stack.
No disk may be placed on top of a smaller disk.
Algorithm
Algorithm Recursive Solution for the Tower of Hanoi with an algorithm.
Let’s call the three peg Src(Source), Aux(Auxiliary) and st(Destination).
Move the top N – 1 disk from the Source to the Auxiliary tower.
Move the Nth disk from Source to Destination tower.
Move the N – 1 disks from the Auxiliary tower to the Destination tower. Transferring the top N – 1 disk from Source to Auxiliary tower can again be thought of as a fresh problem and can be solved in the same manner. So once you master solving Tower of Hanoi with three disks, you can solve it with any number of disks with the above algorithm.
solution
Tower(N, Beg, AUX, END)
If N=1 then BEGEnd.
[Move N-1 disks from rod BEG to rod AUX]
Call Tower(N-1, BEG, END, AUX)
Write BEGEND
[Move N-1 disks from rod AUX to rod END]
Call Tower(N-1, AUX, BEG, END)
Return
Time Complexity
T(n)=2T(n-1)+c
T(0)=0
T(n)=2{2T(n-2)+c}+c T(n-1)=2T(n-2)+c
T(n)= 4T(n-2)+2c+c
T(n)= 4{2T(n-3)+c}+2c+c T(n-2)=2T(n-3)+c
T(n)= 8T(n-3)+4c+2c+c
T(n)= 8{2T(n-4)+c}+4c+2c+c T(n-3)=2T(n-4)+c
T(n)= 16T(n-4)+8c+4c+2c+c
T(n)=2k T(n-k)+2k -1 if n-k=0, k=n
T(n)=2N T(0)+2N -1
T(n)=2N = 1
4. Introduction
The tower of Hanoi (also called the tower of Brahma
or the Lucas tower) is a mathematical game or a
puzzle.
invented by the French mathematician Edouard
Lucas and sold as a toy in 1883.
It consists of three rods and a numbers of disks of
different sizes which can slide onto any rod.
5. History
There is a history about an Indian temple in Kashi
Vishwanath, which contains a large room with three time-worn
posts in it surrounded by 64 golden disks. Brahmin priests,
acting out the command of an ancient prophecy, have been
moving these disks, in accordance with the immutable rules of
the Brahma, since that time.
According to the legend, when the last move of the puzzle will
be completed, the world will end.
The number of moves required to correctly move a tower
of 64 disks is 264 _1=18,446,744,073,709,551,615.
At rate of one move per second, that is 584,942,417,335
years.
6. Objective and Procedure
The objective of this game is to move the disks one
by one from first peg(rod) to last peg(rod).
The number of disks can vary, but there are only three
rods.
The game starts by having few disks stacked in increasing
order of size.
Start the game with your two smallest disks. Stack them
on the rod B and C, with the smaller disk on top of the
larger disk.
7. Rules
1. Only one disk can be move at a time.
2. Each move consists of taking the upper disk
from one of the stacks and placing it on top
of another stack i.e a disk can only be moved
if it is the uppermost disk on a stack.
3. No disk may be placed on top of a smaller
disk.
9. Algorithm
Algorithm Recursive Solution for the Tower of Hanoi with algorithm.
1. Let’s call the three peg Src(Source), Aux(Auxiliary) and
st(Destination).
2. Move the top N – 1 disks from the Source to Auxiliary tower.
3. Move the Nth disk from Source to Destination tower.
4. Move the N – 1 disks from Auxiliary tower to Destination tower.
Transferring the top N – 1 disks from Source to Auxiliary tower can
again be thought of as a fresh problem and can be solved in the
same manner. So once you master solving Tower of Hanoi with
three disks, you can solve it with any number of disks with the
above algorithm.
10. solution
Tower(N, Beg, AUX, END)
1. If N=1 then BEGEnd.
2. [Move N-1 disks from rod BEG to rod AUX]
3. Call Tower(N-1, BEG, END, AUX)
4. Write BEGEND
5. [Move N-1 disks from rod AUX to rod END]
6. Call Tower(N-1, AUX, BEG, END)
7. Return