On the Iterative Solution of the Towers of Hanoi Problem
Introduction. Recall the well-known problem of the Towers of Hanoi and its recursive
solution. Given are three places A,B,C and N disks, numbered 0. .N−1 in order of increasing
size, and initially they are stacked in decreasing size (smallest disk at the top) at place A.
The goal is to move the tower to place B, according to the following rules:
only one disk is moved at a time,
a larger disk is never placed on a smaller one, and
the disks are stacked only at the three given places.
Denoting a direct move of disk n from place a to b not touching c by (n, (a, b, c)), the shortest
sequence of moves to bring a tower of N disks from place A to B using place C along the
way, is expressed by HanoiN (A,B,C), where:
Hanoi0(a, b, c) = h i
Hanoin+1(a, b, c) = Hanoin(a, c, b) a h(n, (a, b, c))i a Hanoin(c, b, a)
The definition is non-trivially recursive, and is therefore not suitable as a recipe for human
beings. There exists an iterative solution, which is easy to execute by humans:
If N is odd, cycle the smallest disk through A,B,C (“clockwise”), and after every move
of the smallest disk, do one other move (for which there is only one possibility according
to the rule that a larger disk may not be placed on a smaller one).
If N is even, the smallest disk should be cycled through A,C,B (“anti-clockwise”).
We set out to formally prove that a crucial property of this iterative solution follows from
Hanoi’s definition: the smallest disk cycles in the way indicated. Actually, we prove in one
go, without extra effort, that all disks cycle. We do not prove that the smallest disk is moved
alternately with the others.
Anybody familiar with the principle of induction is able to prove the property themself
(try it!); so the existence of a proof is not at all surprising. What is surprising, however, is
the elegance of our proof: we use only elementary, everyday set calculus (expressed in the Z
notation [2]) and no case distinctions, and our proof is a calculation of a few lines long, and
1
consists of steps that are both “human readable” and “machine verifiable”. (Also, neither
powers-of-two nor lengths-of-sequences occur anywhere in our proof.) It was Backhouse’s
paper [1] (with a quite different formulation and proof of the same property) that prompted
us to do so.
Abstraction. As a preparation, we abstract from the places and keep only the direction of
the moves. To this end, we first write the definition of Hanoi as follows:
Hanoi0(d) = h i
Hanoin+1(d) = Hanoin(f d) a h(n, d)i a Hanoin(g d)
Here, d ranges over triples (a, b, c), and f = a, b, c • (a, c, b) and g = a, b, c • (c, b, a).
Now, identify (A,B,C), (B,C,A), (C,A,B) with each other and denote these with “direction
0”, and similarly for (A,C,B), (C,B,A), (B,A,C) and “direction 1”. Observe that,
under this identification, f and g become ‘the successor/predecessor modulo 2’, denoted with
a postfixed 1. Thus, we find the definition:
H0(d) = h i
Hn+1(d) = Hn(d1) a h(n, d)i a Hn(d1)
Here, and in the sequel, d varies over directions 0. . 1, and we let denote addition modulo 2.
We shall use the following laws from ‘mod 2’ calculus:
is associative and commutative
i n d = d if i = n
(n−1) d 1 = n d
Some set notation. (Here and in the remainder of the paper one may specialise “arbitrary
i” to just i = 0; our more general treatment is for free.)
We wish to talk about the set of directions of disk i in the sequence Hnd of moves: our
claim will be that this set is a singleton set if i < n (that is, “disk i cycles if it is one of the
disks 0 . . n−1”) and empty otherwise. More specifically, the singleton set contains direction
i (n−1) d, that is, “it contains direction d iff i and n−1 have the same parity”. In order
to express that singleton or empty set, recall Z’s set notation {D | P • E}, where D is a
collection of variable declarations, P is a predicate, and E is an expression; the set consists
of all values E where the variables range over sets as declared by D but only as far as they
satisfy predicate P. The set notation {| P • E} is a special case (neither tricky nor abuse
of notation, just a special case): it is equal to {E} if P holds and ? otherwise, and saves us
from explicitly making case distinctions. In particular, the set of interest is expressed by:
{| i<n • i (n−1) d}
It remains to define the set Di (S) of directions d that appear in an item (i, d) in sequence S.
Using the Z convention that a sequence is a function mapping indices to items, and that a
function is a set of (argument, result)-pairs, the definition reads:
Di (S) = {d | (i, d) 2 ran S}
2
= ran ({i}C ran S)
Here X CY is the Z notation for ‘domain restriction’: X CY = {u, v | u 2 X ^ (u, v) 2 Y }.
We shall use the following elementary properties about these notations:
ran hx i = {x}
ran (SaT) = ran S [ ranT
X C (Y [Z) = XCY [ XCZ
{i} C {(n, d)} = {| i = n • (n, d)}
ran{| P • (E,E0)} = {| P • E0}
{| P • E} [ {| Q • E} = {| P _ Q • E}
Theorem. For all naturals i and n:
Di (Hnd) = {| i<n • i (n−1) d}
Proof.
We use induction on n. For n = 0 we calculate:
Di (H0d)
= ran ({i}C ranh i) defs Di , H0
= ? ran on h i, XC on ?, ran on ?
= {| i < 0 • i (0−1) d} i 0
For n+1 > 0, not applying any other set theoretic law than mentioned above:
Di (Hn+1d)
= ran ({i}C (ran (Hn(d 1) a h(n, d)i a Hn(d 1)))) defs Di , Hn+1
= ran ({i}C (ranHn(d 1) [ ranh(n, d)i [ ranHn(d 1))) ran on a
= ran ({i}C (ranHn(d 1) [ ranh(n, d)i)) idempotency [
= ran ({i}C (ranHn(d 1) [ {(n, d)})) ran on h i
= ran ({i}C(ranHn(d 1)) [ {i}C{(n, d)}) {i}C on [
= ran ({i}C(ranHn(d 1)) [ {| i=n • (n, d)}) {i}C on { }
= ran({i}C(ranHn(d 1))) [ ran{| i=n • (n, d)} ran on [
= Di (Hn(d 1)) [ {| i=n • d} def Di , ran on {| }
= {| i<n • i (n−1) d 1} [ {| i=n • d} induction hypothesis
= {| i<n • i n d} [ {| i=n • i n d} ‘mod 2’ calculus
= {| i < n _ i = n • i n d} [ of similar sets
= {| i < n+1 • i ((n+1)−1) d} arithmetic
This completes the proof.
As corollary we have, for odd N, that D0(HN 0) = {0}: the smallest disk cycles in direction 0
(“clockwise” through A,B,C), and similarly for even N > 0 that D0(HN (0)) = {1}.