{============================================================== hanoi.p Started by Jeff Ondich some time in 1993 Last modified 5/13/96 ==============================================================} program hanoi(input,output); var nRings : integer; {============================================================== MoveRings solves the Towers of Hanoi problem for moving N rings from peg "start" to peg "destination," using peg "spare" for intermediate steps. The solution consists of a printed list of instructions. Normally, the peg numbers are 1, 2, and 3, but that is not strictly necessary. ==============================================================} procedure MoveRings( N, start, desination, spare : integer ); begin if N >= 1 then begin MoveRings( N - 1, start, spare, desination ); write( 'Move the top ring from peg ' ); writeln( start:1, ' to peg ', desination:1 ); MoveRings( N - 1, spare, desination, start ) end end; {============================================================== main program ==============================================================} begin write( 'Enter the number of rings: ' ); readln( nRings ); writeln( 'To move ', nRings:1, ' rings from peg 1 to peg 3, ' ); writeln; MoveRings( nRings, 1, 3, 2 ) end.