/* The Towers of Hanoi problem: */ /* 3 pegs, n discs */ /* n discs are stacked in order, bigggest at bottom, smallest at top */ /* all on one peg ... move the n discs to one of the other 2 pegs */ /* moving only one disc at a time and never resting a larger disc on */ /* a smaller one. The aim is to solve the 64-disc problem */ xfer(0,_,_,_). xfer(N,Init,Dest,Temp) :- N>0, N1 is N-1, xfer(N1,Init,Temp,Dest), write('Move disc '), write(N), write(' from '), write(Init), write(' to '), write(Dest), nl, xfer(N1,Temp,Dest,Init). /* Solution to the 64 discs problem is given by... */ /* ?- xfer(64,a,b,c). */ /* but DONT run this: it takes quite a while to complete!! */ /* instead try ?- xfer(3,left,middle,right) */ /* try ?- xfer(4,left,middle,right) */ /* try ?- xfer(5,left,middle,right) */ /* try even ?- xfer(9,left,middle,right) */