3hanoi([], [], _). 4hanoi(A, B, C):- 5 [A,B] \= [[],[]], 6 any_move(A, B, C, A1, B1, C1), 7% display(hanoi(A1, B1, C1)),nl, 8 hanoi(A1, B1, C1). 9% display(ok),nl. 10 11any_move(A, B, C, A1, B, C1):- swap(A, C, A1, C1). 12any_move(A, B, C, A1, B1, C):- swap(A, B, A1, B1). 13any_move(A, B, C, A, B1, C1):- swap(C, B, C1, B1). 14 15swap([A|A1], B, A1, [A|B]):- moveto([A|B]). 16swap(A, [B|B1], [B|A], B1):- moveto([B|A]). 17 18%moveto(A) :- not inverted(A). 19 20moveto(A) :- not negmoveto(A). 21negmoveto(A) :- not moveto(A). 22 23% moveto(A, B, A1, B1) :- not negmoveto(A, B, A1, B1). 24% negmoveto(A, B, A1, B1) :- not moveto(A, B, A1, B1). 25 26:- moveto(A), inverted(A). 27 28 29%:- moveto(A, B, A1, B1), inverted(B1). 30 31inverted([A,B|_]):- A > B. 32 33%inverted([A,B|Cs]):- A < B, inverted([B|Cs]). 34 35 36?- hanoi([1,2,3], [], []). 37 38 39#show hanoi/3, moveto/4. 40 41 42 43/* 44?- ?? hanoi([1,2,3],[],[]). 45 46Answer 1 (in [23.284] ms): 47{ hanoi([1,2,3],[],[]) , hanoi([2,3],[],[1]) , hanoi([3],[2],[1]) , hanoi([1,3],[2],[]) , hanoi([3],[1,2],[]) , hanoi([],[1,2],[3]) , hanoi([1],[2],[3]) , hanoi([],[2],[1,3]) , hanoi([2],[],[1,3]) , hanoi([1,2],[],[3]) , hanoi([2],[1],[3]) , hanoi([],[1],[2,3]) , hanoi([1],[],[2,3]) , hanoi([],[],[1,2,3]) } 48 49 50next ? ; 51 52Answer 2 (in [13.626] ms): 53{ hanoi([1,2,3],[],[]) , hanoi([2,3],[],[1]) , hanoi([3],[2],[1]) , hanoi([1,3],[2],[]) , hanoi([3],[1,2],[]) , hanoi([],[1,2],[3]) , hanoi([1],[2],[3]) , hanoi([],[2],[1,3]) , hanoi([2],[],[1,3]) , hanoi([1,2],[],[3]) , hanoi([2],[1],[3]) , hanoi([],[1],[2,3]) , hanoi([],[],[1,2,3]) } 54 55 56next ? 57 */