Towers Of Hanoi

���������������
�������������������������������������������
����������������
�������������
���������
�������
This week’s problem is called the “The Two Towers.” The original problem was invented by the French
Mathematician Edouard Lucas in 1883. “Lucas developed a romantic legend surrounding the Tower of
Brahma, which supposedly has 64 disks of pure gold resting on three diamond needles. At the beginning of time, God placed these golden disks on the first needle and ordained that a group of priests
should transfer them to the third, moving only one disk at a time and never moving a larger one onto a
smaller. The priests repeatedly worked day and night at their task. When they finish, the Tower will
crumble and the world will end.”
The Tower of Hanoi is a game based off of this legend, but only includes eight disks, initially stacked in
decreasing size on one of three pegs. The objective is to transfer the entire tower to one of the other
pegs, moving only one disk at a time and never moving a larger one onto a smaller.
1. How many moves are necessary and sufficient to transfer the entire tower of Hanoi to
one of the other pegs, moving only one disk at a time and never moving a larger disk
onto a smaller one?
2. How many moves are necessary and sufficient to transfer the entire tower of Brahma to
one of the other pegs, moving only one disk at a time and never moving a larger disk
onto a smaller one?
3. Assume the Priests of Brahma started moving the rings at 0600 on Jan 1st, 1883 and
that it takes the priests 12 minutes to move one disk. What is the exact date and time
(include hours and minutes) that the world is predicted to end?
2 ���
HanoiTowers1718.nb
��������
������
One way to solve the problem is by using graph theory. In the case of a 3 peg puzzle the minimum
number of moves needed to complete the puzzle is a problem which is isomorphic to finding the length
of a Hamiltonian path on an n - hypercube graph. Using Mathematica we define a hypercube graph of
dimension 8.
��������
g = HypercubeGraph[8];
We find a Hamiltonian path on this graph.
��������
��������
path = FindHamiltonianPath[g]
{256, 254, 250, 252, 251, 243, 244, 228, 196, 212, 211, 219, 220, 218, 222, 224, 223, 221,
217, 209, 213, 215, 216, 214, 210, 194, 198, 200, 199, 197, 133, 135, 136, 134, 6,
8, 7, 5, 69, 71, 72, 70, 66, 82, 86, 88, 87, 85, 81, 89, 93, 95, 96, 94, 90, 92, 91,
83, 84, 68, 100, 116, 115, 123, 124, 122, 126, 128, 127, 125, 121, 113, 117, 119,
120, 118, 114, 98, 102, 104, 103, 101, 97, 105, 109, 111, 112, 110, 106, 108, 107,
99, 67, 75, 76, 74, 78, 80, 79, 77, 73, 65, 1, 9, 13, 15, 16, 14, 10, 12, 11, 3, 35,
43, 44, 42, 46, 48, 47, 45, 41, 33, 37, 39, 40, 38, 34, 50, 54, 56, 55, 53, 49, 57,
61, 63, 64, 62, 58, 60, 59, 51, 52, 36, 4, 20, 19, 27, 28, 26, 30, 32, 31, 29, 25, 17,
21, 23, 24, 22, 18, 2, 130, 146, 150, 152, 151, 149, 145, 153, 157, 159, 160, 158,
154, 156, 155, 147, 148, 132, 164, 180, 179, 187, 188, 186, 190, 192, 191, 189, 185,
177, 181, 183, 184, 182, 178, 162, 166, 168, 167, 165, 161, 169, 173, 175, 176,
174, 170, 172, 171, 163, 131, 139, 140, 138, 142, 144, 143, 141, 137, 129, 193,
201, 205, 207, 208, 206, 202, 204, 203, 195, 227, 235, 236, 234, 238, 240, 239,
237, 233, 225, 229, 231, 232, 230, 226, 242, 246, 248, 247, 245, 241, 249, 253, 255}
The length of this path is:
��������
��������
Length[path] - 1
255
We visualize the path.
HanoiTowers1718.nb
��������
HighlightGraph[g, PathGraph[path], ImageSize → Large]
��������
So the 3 peg 8 disk Towers of Hanoi puzzle can be solved in a minimum of 255 moves.
������
To solve the 64 disk puzzle we’ll find a sequence function giving the minimum number of moves to
solve a 3 peg n disk problem for n = 1, 2 ..., 12.
��������
��������
seq = Table[Length[FindHamiltonianPath[HypercubeGraph[n]]] - 1, {n, 1, 12}]
{1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095}
Now we can find the sequence function.
��������
��������
FindSequenceFunction[seq, n]
- 1 + 2n
So our formula is 2n - 1, where n is the number of disks in a 3 peg puzzle. Numbers of this type are
���
3
4 ���
HanoiTowers1718.nb
called Mersenne numbers. In the case of the 64 disk puzzle the minimum number of moves needed to
solve the puzzle is:
��������
��������
moves = 2n - 1 /. n → 64
18 446 744 073 709 551 615
������
Since no particular time zone is stated we’ll assume that all times are reckoned in terms of Greenwich
Mean Time. Hoping that we can trust Mathematica’s time functions to work over such a long period, we
employ AbsoluteTime which converts everything to seconds. The 64 disk Towers of Hanoi problem
should be completed, and the world will end, at the following time:
��������
$TimeZone = 0;
FromAbsoluteTime[AbsoluteTime[{1883, 1, 1, 6}] + 720 moves]
��������
Mon 29 Nov 420878915464658 09:00:00 GMT
This date being Monday, November 29 at 09:00 in the year 420,878,915,464,658
This is little more than a nonsense calculation for many reasons. First, a main sequence star like the
sun has a total estimated lifetime of only about 10 billion years. The estimated time to complete the
puzzle is over 42,000 times this long!
420 878 915 464 658
��������
��������
10 000 000 000
// N
42 087.9
Second, even if the human race persisted this long inaccuracies in the Gregorian calendar would
necessitate it being replaced after some thousands of years. If this was not done the accumulated effect
of the Earth’s precision about its axis of rotation would cause the equinoxes to be months away their
currently expected dates.
Third, over thousands of years the Earth slowly transfers angular momentum to the Moon causing the
length of the solar day to increase by a second every few years. In the present system of time keeping
this is handled by inserting leap seconds as needed into the atomic time standard to keep the rate of
the Earth’s rotation in synchronization with the clocks we use. The magnitude of the needed corrections
more than a decade or so in the future is totally unpredictable. This factor alone would surely render the
time of day when the puzzle is complete meaningless.