## Tower Of Hanoi

### October 11, 2011

According to legend, there is a temple in Hanoi where are located sixty-four golden rings of graduated sizes and three diamond towers. Each day, the monks of the temple move a ring from one tower to another according to the rule that only one ring may be moved each day, that a single move consists of taking the highest ring from one tower and placing it on another tower, and that no ring may be placed on top of a smaller ring. The rings and towers were placed at the beginning of the world, and the monks have toiled through the ages to move all the rings from the designated starting tower to the designated finishing tower, at each day making the move that minimizes the total number of moves required. The world will end when the monks complete their work.

Actually, there is no legend; the story was concocted as a mathematical puzzle by Edouard Lucas, whom we have met in our work on primality testing. The program that determines the sequence of moves is often used as a demonstration of recursion: to move five rings from the first tower to the second, first move four rings from the first tower to the third, then move the fifth ring from the first tower to the second, then move four rings from the third tower to the second. To move four rings from the first tower to the third, first move three rings from … There is also an iterative solution, but I can never remember it, and the recursive solution is so simple that it’s the one I always use.

Your task is to write the program that calculates the sequence of moves that solved the Tower of Hanoi using the fewest number of moves. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Here is my scheme solution:

purely iterative version with ASCII art in C:

Here’s an iterative solution in Python 3.

The way I remember it is:

1. Consider pairs of towers according to a repeating pattern.

2. The pattern for an even number of disks is the same as for 2 disks;

the pattern for an odd number of disks is reversed.

3. For any pair of towers, make the legal move.

#lang racket

#|

Shortest path of moving a Hanoian tower from one needle to another one.

Non recursive algorithm.

n : exact non-negative integer number

h : number of disks.

m : number of the move to be made counting from 1.

d : disk, identified in increasing order by 0 up to but not including h.

f : Position of the full tower at the start of the path (0, 1 or 2)

t : Position of the full tower after completion of the path (0, 1 or 2)

f and t must be different.

mcnt : number of moves made with disk d after m moves.

onto : needle a disk is moved to during move m.

from : needle a disk is taken from during move m.

thrd : needle not involved in move m.

posi : needle disk d is positioned at after m moves.

disk : disk being moved during move m.

|#

(require (only-in (lib “60.ss” “srfi”) log2-binary-factors))

(define (exp2 n ) (expt 2 n))

(define (mod2 n ) (modulo n 2))

(define (mod3 n ) (modulo n 3))

(define (pari n ) (add1 (mod2 (add1 n))))

(define (rotd h d f t) (mod3 (* (- t f) (pari (- h d)))))

(define (rot3 h f t) (rotd h 0 f t))

(define (mcnt m d ) (quotient (+ m (exp2 d)) (exp2 (add1 d))))

(define (thrd m h f t) (mod3 (- f (* m (rot3 h f t)))))

(define (onto m h f t) (mod3 (- (thrd m h f t) (rotd h (disk m) f t))))

(define (from m h f t) (mod3 (+ (thrd m h f t) (rotd h (disk m) f t))))

(define (posi m h d f t) (mod3 (+ f (* (rotd h d f t) (mcnt m d)))))

(define disk log2-binary-factors)

#| compute the distribution of the disks of a tower of 100 disks after

(expt 10 30) moves starting from needle 0 and going to needle 1.

|#

(time

(let ((m (expt 10 30)) (h 100) (f 0) (t 1))

(for/list ((d (in-range 0 h))) (posi m h d f t))))

; cpu time: 0 real time: 0 gc time: 0

; (1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 0 0 2 1 0 2 1 1 1 1 2 0 0 2 1 1 1 0 0 1 2 0 0 0 1 1 0 0 1 1 1 2 0 0 0 0 0 1 2 0 0 2 2 0 0 0 1 1 0 2 2 0 0 2 1 0 0 1 1 1 1 1 2 2 1 0 0 1 1)

Jos

[...] to the Tower of Hanoi puzzle, especially compared to the other languages: http://programmingpraxis.com/2011/10/11/tower-of-hanoi/ Tags: code, control, hacker, hacking, programming, [...]