<s>
The	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
(	O
also	O
called	O
The	O
problem	O
of	O
Benares	O
Temple	O
or	O
Tower	B-Language
of	I-Language
Brahma	I-Language
or	O
Lucas	B-Language
 '	I-Language
Tower	I-Language
and	O
sometimes	O
pluralized	O
as	O
Towers	O
,	O
or	O
simply	O
pyramid	O
puzzle	O
)	O
is	O
a	O
mathematical	O
game	O
or	O
puzzle	O
consisting	O
of	O
three	O
rods	O
and	O
a	O
number	O
of	O
disks	O
of	O
various	O
diameters	O
,	O
which	O
can	O
slide	O
onto	O
any	O
rod	O
.	O
</s>
<s>
The	O
minimal	O
number	O
of	O
moves	O
required	O
to	O
solve	O
a	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
puzzle	I-Language
is	O
2n	O
−	O
1	O
,	O
where	O
n	O
is	O
the	O
number	O
of	O
disks	O
.	O
</s>
<s>
The	O
puzzle	O
is	O
therefore	O
also	O
known	O
as	O
the	O
Tower	B-Language
of	I-Language
Brahma	I-Language
.	O
</s>
<s>
The	O
minimal	O
number	O
of	O
moves	O
required	O
to	O
solve	O
a	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
puzzle	I-Language
is	O
,	O
where	O
n	O
is	O
the	O
number	O
of	O
disks	O
.	O
</s>
<s>
Make	O
the	O
legal	O
move	O
between	O
pegs	O
A	O
and	O
C	B-Language
(	O
in	O
either	O
direction	O
)	O
,	O
</s>
<s>
Make	O
the	O
legal	O
move	O
between	O
pegs	O
B	O
and	O
C	B-Language
(	O
in	O
either	O
direction	O
)	O
,	O
</s>
<s>
Make	O
the	O
legal	O
move	O
between	O
pegs	O
A	O
and	O
C	B-Language
(	O
in	O
either	O
direction	O
)	O
,	O
</s>
<s>
Make	O
the	O
legal	O
move	O
between	O
pegs	O
B	O
and	O
C	B-Language
(	O
in	O
either	O
direction	O
)	O
,	O
</s>
<s>
After	O
each	O
move	O
check	O
if	O
the	O
C	B-Language
peg	O
is	O
complete	O
.	O
</s>
<s>
If	O
n	O
is	O
odd	O
,	O
the	O
first	O
move	O
is	O
from	O
peg	O
A	O
to	O
peg	O
C	B-Language
.	O
</s>
<s>
Thence	O
,	O
for	O
the	O
Towers	B-Language
of	I-Language
Hanoi	I-Language
:	O
</s>
<s>
label	O
the	O
pegs	O
A	O
,	O
B	O
,	O
C	B-Language
,	O
</s>
<s>
The	O
full	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
solution	O
then	O
consists	O
of	O
moving	O
n	O
disks	O
from	O
the	O
source	O
peg	O
A	O
to	O
the	O
target	O
peg	O
C	B-Language
,	O
using	O
B	O
as	O
the	O
spare	O
peg	O
.	O
</s>
<s>
This	O
approach	O
can	O
be	O
given	O
a	O
rigorous	O
mathematical	O
proof	O
with	O
mathematical	B-Algorithm
induction	I-Algorithm
and	O
is	O
often	O
used	O
as	O
an	O
example	O
of	O
recursion	O
when	O
teaching	O
programming	O
.	O
</s>
<s>
As	O
in	O
many	O
mathematical	O
puzzles	O
,	O
finding	O
a	O
solution	O
is	O
made	O
easier	O
by	O
solving	O
a	O
slightly	O
more	O
general	O
problem	O
:	O
how	O
to	O
move	O
a	O
tower	O
of	O
h	O
(	O
height	O
)	O
disks	O
from	O
a	O
starting	O
peg	O
f	O
=	O
A	O
(	O
from	O
)	O
onto	O
a	O
destination	O
peg	O
t	O
=	O
C	B-Language
(	O
to	O
)	O
,	O
B	O
being	O
the	O
remaining	O
third	O
peg	O
and	O
assuming	O
t	O
≠	O
f	O
.	O
First	O
,	O
observe	O
that	O
the	O
problem	O
is	O
symmetric	O
for	O
permutations	O
of	O
the	O
names	O
of	O
the	O
pegs	O
(	O
symmetric	B-Algorithm
group	I-Algorithm
S3	O
)	O
.	O
</s>
<s>
If	O
a	O
solution	O
is	O
known	O
moving	O
from	O
peg	O
A	O
to	O
peg	O
C	B-Language
,	O
then	O
,	O
by	O
renaming	O
the	O
pegs	O
,	O
the	O
same	O
solution	O
can	O
be	O
used	O
for	O
every	O
other	O
choice	O
of	O
starting	O
and	O
destination	O
peg	O
.	O
</s>
<s>
If	O
h	O
=	O
1	O
,	O
then	O
simply	O
move	O
the	O
disk	O
from	O
peg	O
A	O
to	O
peg	O
C	B-Language
.	O
If	O
h	O
>	O
1	O
,	O
then	O
somewhere	O
along	O
the	O
sequence	O
of	O
moves	O
,	O
the	O
largest	O
disk	O
must	O
be	O
moved	O
from	O
peg	O
A	O
to	O
another	O
peg	O
,	O
preferably	O
to	O
peg	O
C	B-Language
.	O
The	O
only	O
situation	O
that	O
allows	O
this	O
move	O
is	O
when	O
all	O
smaller	O
h	O
−	O
1	O
disks	O
are	O
on	O
peg	O
B	O
.	O
</s>
<s>
Then	O
move	O
the	O
largest	O
disk	O
and	O
finally	O
move	O
the	O
h	O
−	O
1	O
smaller	O
disks	O
from	O
peg	O
B	O
to	O
peg	O
C	B-Language
.	O
The	O
presence	O
of	O
the	O
largest	O
disk	O
does	O
not	O
impede	O
any	O
move	O
of	O
the	O
h	O
−	O
1	O
smaller	O
disks	O
and	O
can	O
be	O
temporarily	O
ignored	O
.	O
</s>
<s>
Now	O
the	O
problem	O
is	O
reduced	O
to	O
moving	O
h	O
−	O
1	O
disks	O
from	O
one	O
peg	O
to	O
another	O
one	O
,	O
first	O
from	O
A	O
to	O
B	O
and	O
subsequently	O
from	O
B	O
to	O
C	B-Language
,	O
but	O
the	O
same	O
method	O
can	O
be	O
used	O
both	O
times	O
by	O
renaming	O
the	O
pegs	O
.	O
</s>
<s>
The	O
following	O
is	O
a	O
procedure	O
for	O
moving	O
a	O
tower	O
of	O
h	O
disks	O
from	O
a	O
peg	O
A	O
onto	O
a	O
peg	O
C	B-Language
,	O
with	O
B	O
being	O
the	O
remaining	O
third	O
peg	O
:	O
</s>
<s>
disk	O
h	O
can	O
be	O
moved	O
from	O
peg	O
A	O
to	O
peg	O
C	B-Language
.	O
</s>
<s>
If	O
h	O
>	O
1	O
,	O
then	O
again	O
use	O
this	O
procedure	O
to	O
move	O
the	O
h	O
−	O
1	O
smaller	O
disks	O
from	O
peg	O
B	O
to	O
peg	O
C	B-Language
.	O
</s>
<s>
By	O
means	O
of	O
mathematical	B-Algorithm
induction	I-Algorithm
,	O
it	O
is	O
easily	O
proven	O
that	O
the	O
above	O
procedure	O
requires	O
the	O
minimal	O
number	O
of	O
moves	O
possible	O
and	O
that	O
the	O
produced	O
solution	O
is	O
the	O
only	O
one	O
with	O
this	O
minimal	O
number	O
of	O
moves	O
.	O
</s>
<s>
The	O
following	O
Python	B-Language
code	I-Language
demonstrates	O
the	O
recursive	O
solution	O
.	O
</s>
<s>
To	O
use	O
the	O
syntax	O
of	O
the	O
C	B-Language
programming	I-Language
language	I-Language
,	O
move	O
m	O
is	O
from	O
peg	O
(	O
m	O
&	O
m	O
-	O
1	O
)	O
%	O
3	O
to	O
peg	O
( ( 	O
m	O
|	O
m	O
-	O
1	O
)	O
+	O
1	O
)	O
%	O
3	O
,	O
where	O
the	O
disks	O
begin	O
on	O
peg	O
0	O
and	O
finish	O
on	O
peg	O
1	O
or	O
2	O
according	O
as	O
whether	O
the	O
number	O
of	O
disks	O
is	O
even	O
or	O
odd	O
.	O
</s>
<s>
The	O
binary	O
numeral	O
system	O
of	O
Gray	B-Device
codes	I-Device
gives	O
an	O
alternative	O
way	O
of	O
solving	O
the	O
puzzle	O
.	O
</s>
<s>
In	O
the	O
Gray	O
system	O
,	O
numbers	O
are	O
expressed	O
in	O
a	O
binary	O
combination	O
of	O
0s	O
and	O
1s	O
,	O
but	O
rather	O
than	O
being	O
a	O
standard	O
positional	O
numeral	O
system	O
,	O
the	O
Gray	B-Device
code	I-Device
operates	O
on	O
the	O
premise	O
that	O
each	O
value	O
differs	O
from	O
its	O
predecessor	O
by	O
only	O
one	O
(	O
and	O
exactly	O
one	O
)	O
bit	O
changed	O
.	O
</s>
<s>
If	O
one	O
counts	O
in	O
Gray	B-Device
code	I-Device
of	O
a	O
bit	O
size	O
equal	O
to	O
the	O
number	O
of	O
disks	O
in	O
a	O
particular	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
,	O
begins	O
at	O
zero	O
and	O
counts	O
up	O
,	O
then	O
the	O
bit	O
changed	O
each	O
move	O
corresponds	O
to	O
the	O
disk	O
to	O
move	O
,	O
where	O
the	O
least-significant	O
bit	O
is	O
the	O
smallest	O
disk	O
,	O
and	O
the	O
most-significant	O
bit	O
is	O
the	O
largest	O
.	O
</s>
<s>
The	O
position	O
of	O
the	O
bit	O
change	O
in	O
the	O
Gray	B-Device
code	I-Device
solution	O
gives	O
the	O
size	O
of	O
the	O
disk	O
moved	O
at	O
each	O
step	O
:	O
1	O
,	O
2	O
,	O
1	O
,	O
3	O
,	O
1	O
,	O
2	O
,	O
1	O
,	O
4	O
,	O
1	O
,	O
2	O
,	O
1	O
,	O
3	O
,	O
1	O
,	O
2	O
,	O
1	O
,	O
...	O
,	O
a	O
sequence	O
also	O
known	O
as	O
the	O
ruler	O
function	O
,	O
or	O
one	O
more	O
than	O
the	O
power	O
of	O
2	O
within	O
the	O
move	O
number	O
.	O
</s>
<s>
In	O
the	O
Wolfram	B-Language
Language	I-Language
,	O
IntegerExponent[ Range[ 2^8	O
-	O
1 ]	O
,	O
2 ]	O
+	O
1	O
gives	O
moves	O
for	O
the	O
8-disk	O
puzzle	O
.	O
</s>
<s>
As	O
more	O
disks	O
are	O
added	O
,	O
the	O
graph	O
representation	O
of	O
the	O
game	O
will	O
resemble	O
a	O
fractal	B-Application
figure	O
,	O
the	O
Sierpiński	O
triangle	O
.	O
</s>
<s>
Incidentally	O
,	O
this	O
longest	O
non-repetitive	O
path	O
can	O
be	O
obtained	O
by	O
forbidding	O
all	O
moves	O
from	O
a	O
to	O
c	B-Language
.	O
</s>
<s>
given	O
pegs	O
A	O
,	O
B	O
,	O
C	B-Language
,	O
one	O
cannot	O
move	O
directly	O
between	O
pegs	O
A	O
and	O
C	B-Language
)	O
,	O
then	O
moving	O
a	O
stack	O
of	O
n	O
disks	O
from	O
peg	O
A	O
to	O
peg	O
C	B-Language
takes	O
3n	O
−	O
1	O
moves	O
.	O
</s>
<s>
In	O
Cyclic	O
Hanoi	O
,	O
we	O
are	O
given	O
three	O
pegs	O
(	O
A	O
,	O
B	O
,	O
C	B-Language
)	O
,	O
which	O
are	O
arranged	O
as	O
a	O
circle	O
with	O
the	O
clockwise	O
and	O
the	O
counterclockwise	O
directions	O
being	O
defined	O
as	O
A	O
–	O
B	O
–	O
C	B-Language
–	O
A	O
and	O
A	O
–	O
C	B-Language
–	O
B	O
–	O
A	O
respectively	O
.	O
</s>
<s>
Let	O
C(n )	O
and	O
A(n )	O
represent	O
moving	O
n	O
disks	O
clockwise	O
and	O
counterclockwise	O
,	O
then	O
we	O
can	O
write	O
down	O
both	O
formulas	O
:	O
</s>
<s>
C(n )	O
=	O
A(n-1 )	O
n	O
A(n-1 )	O
and	O
A(n )	O
=	O
A(n-1 )	O
n	O
C(n-1 )	O
n	O
A(n-1 )	O
.	O
</s>
<s>
Thus	O
C(1 )	O
=	O
1	O
and	O
A(1 )	O
=	O
1	O
1	O
,	O
</s>
<s>
C(2 )	O
=	O
1	O
1	O
2	O
1	O
1	O
and	O
A(2 )	O
=	O
1	O
1	O
2	O
1	O
2	O
1	O
1	O
.	O
</s>
<s>
4	O
)	O
The	O
number	O
of	O
disks	O
moves	O
specified	O
by	O
C(n )	O
and	O
A(n )	O
are	O
minimal	O
.	O
</s>
<s>
Although	O
the	O
three-peg	O
version	O
has	O
a	O
simple	O
recursive	O
solution	O
long	O
been	O
known	O
,	O
the	O
optimal	O
solution	O
for	O
the	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
problem	O
with	O
four	O
pegs	O
(	O
called	O
Reve	O
's	O
puzzle	O
)	O
was	O
not	O
verified	O
until	O
2014	O
,	O
by	O
Bousch	O
.	O
</s>
<s>
For	O
other	O
variants	O
of	O
the	O
four-peg	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
problem	O
,	O
see	O
Paul	O
Stockmeyer	O
's	O
survey	O
paper	O
.	O
</s>
<s>
The	O
so-called	O
Towers	O
of	O
Bucharest	O
and	O
Towers	O
of	O
Klagenfurt	O
game	O
configurations	O
yield	O
ternary	B-Algorithm
and	O
pentary	O
Gray	B-Device
codes	I-Device
.	O
</s>
<s>
In	O
the	O
4-peg	O
case	O
,	O
the	O
optimal	O
equals	O
,	O
where	O
is	O
the	O
nearest	B-Algorithm
integer	I-Algorithm
function	I-Algorithm
.	O
</s>
<s>
In	O
Magnetic	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
,	O
each	O
disk	O
has	O
two	O
distinct	O
sides	O
North	O
and	O
South	O
(	O
typically	O
colored	O
"	O
red	O
"	O
and	O
"	O
blue	O
"	O
)	O
.	O
</s>
<s>
This	O
variation	O
of	O
the	O
famous	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
puzzle	I-Language
was	O
offered	O
to	O
grade	O
3	O
–	O
6	O
students	O
at	O
2ème	O
Championnat	O
de	O
France	O
des	O
Jeux	O
Mathématiques	O
et	O
Logiques	O
held	O
in	O
July	O
1988	O
.	O
</s>
<s>
The	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
is	O
frequently	O
used	O
in	O
psychological	O
research	O
on	O
problem-solving	O
.	O
</s>
<s>
The	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
is	O
also	O
used	O
as	O
a	O
backup	B-Device
rotation	I-Device
scheme	I-Device
when	O
performing	O
computer	O
data	B-Protocol
backups	I-Protocol
where	O
multiple	O
tapes/media	O
are	O
involved	O
.	O
</s>
<s>
As	O
mentioned	O
above	O
,	O
the	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
is	O
popular	O
for	O
teaching	O
recursive	O
algorithms	O
to	O
beginning	O
programming	O
students	O
.	O
</s>
<s>
A	O
pictorial	O
version	O
of	O
this	O
puzzle	O
is	O
programmed	O
into	O
the	O
emacs	B-Application
editor	O
,	O
accessed	O
by	O
typing	O
M-x	O
hanoi	O
.	O
</s>
<s>
There	O
is	O
also	O
a	O
sample	O
algorithm	O
written	O
in	O
Prolog	B-Language
.	O
</s>
<s>
The	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
is	O
also	O
used	O
as	O
a	O
test	O
by	O
neuropsychologists	O
trying	O
to	O
evaluate	O
frontal	O
lobe	O
deficits	O
.	O
</s>
<s>
In	O
2010	O
,	O
researchers	O
published	O
the	O
results	O
of	O
an	O
experiment	O
that	O
found	O
that	O
the	O
ant	O
species	O
Linepithema	O
humile	O
were	O
successfully	O
able	O
to	O
solve	O
the	O
3-disk	O
version	O
of	O
the	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
problem	O
through	O
non-linear	O
dynamics	O
and	O
pheromone	O
signals	O
.	O
</s>
<s>
In	O
2014	O
,	O
scientists	O
synthesized	O
multilayered	O
palladium	O
nanosheets	O
with	O
a	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
like	O
structure	O
.	O
</s>
<s>
The	O
protagonist	O
knows	O
that	O
a	O
rescue	O
ship	O
might	O
take	O
a	O
year	O
or	O
more	O
to	O
arrive	O
,	O
so	O
he	O
chooses	O
to	O
play	O
Towers	B-Language
of	I-Language
Hanoi	I-Language
with	O
64	O
disks	O
.	O
</s>
<s>
In	O
the	O
1966	O
Doctor	O
Who	O
story	O
The	O
Celestial	O
Toymaker	O
,	O
the	O
eponymous	O
villain	O
forces	O
the	O
Doctor	O
to	O
play	O
a	O
ten-piece	O
1,023	O
-move	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
game	O
entitled	O
The	O
Trilogic	O
Game	O
with	O
the	O
pieces	O
forming	O
a	O
pyramid	O
shape	O
when	O
stacked	O
.	O
</s>
<s>
In	O
2007	O
,	O
the	O
concept	O
of	O
the	O
Towers	B-Language
Of	I-Language
Hanoi	I-Language
problem	O
was	O
used	O
in	O
Professor	B-Application
Layton	I-Application
and	I-Application
the	I-Application
Diabolical	I-Application
Box	I-Application
in	O
puzzles	O
6	O
,	O
83	O
,	O
and	O
84	O
,	O
but	O
the	O
disks	O
had	O
been	O
changed	O
to	O
pancakes	O
.	O
</s>
<s>
In	O
the	O
film	O
Rise	O
of	O
the	O
Planet	O
of	O
the	O
Apes	O
(	O
2011	O
)	O
,	O
this	O
puzzle	O
,	O
called	O
in	O
the	O
film	O
the	O
"	O
Lucas	B-Language
Tower	I-Language
"	O
,	O
is	O
used	O
as	O
a	O
test	O
to	O
study	O
the	O
intelligence	O
of	O
apes	O
.	O
</s>
<s>
Star	B-Application
Wars	I-Application
:	I-Application
Knights	I-Application
of	I-Application
the	I-Application
Old	I-Application
Republic	I-Application
and	O
Mass	B-Application
Effect	I-Application
)	O
.	O
</s>
<s>
A	O
15-disk	O
version	O
of	O
the	O
puzzle	O
appears	O
in	O
the	O
game	O
Sunless	B-Application
Sea	I-Application
as	O
a	O
lock	O
to	O
a	O
tomb	O
.	O
</s>
<s>
VRAINS	O
,	O
a	O
hacking	O
group	O
called	O
"	O
Knight	O
of	O
Hanoi	O
"	O
create	O
a	O
structure	O
named	O
"	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
"	O
within	O
the	O
eponymous	O
VRAINS	O
virtual	O
reality	O
network	O
.	O
</s>
<s>
In	O
Genshin	B-Application
Impact	I-Application
,	O
this	O
puzzle	O
is	O
shown	O
in	O
Faruzan	O
's	O
hangout	O
quest	O
,	O
"	O
Early	O
Learning	O
Mechanism	O
"	O
,	O
where	O
she	O
mention	O
it	O
seeing	O
it	O
as	O
mechanism	O
and	O
uses	O
it	O
make	O
toy	O
prototype	O
for	O
children	O
.	O
</s>
<s>
She	O
calls	O
it	O
Pagoda	B-Application
stacks	O
.	O
</s>
