How to resolve the algorithm Towers of Hanoi step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Towers of Hanoi step by step in the Prolog programming language
Table of Contents
Problem Statement
Solve the Towers of Hanoi problem with recursion.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Towers of Hanoi step by step in the Prolog programming language
Source code in the prolog programming language
hanoi(N) :- move(N,left,center,right).
move(0,_,_,_) :- !.
move(N,A,B,C) :-
M is N-1,
move(M,A,C,B),
inform(A,B),
move(M,C,B,A).
inform(X,Y) :- write([move,a,disk,from,the,X,pole,to,Y,pole]), nl.
hanoi(N, Src, Aux, Dest, Moves-NMoves) :-
NMoves is 2^N - 1,
length(Moves, NMoves),
phrase(move(N, Src, Aux, Dest), Moves).
move(1, Src, _, Dest) --> !,
[Src->Dest].
move(2, Src, Aux, Dest) --> !,
[Src->Aux,Src->Dest,Aux->Dest].
move(N, Src, Aux, Dest) -->
{ succ(N0, N) },
move(N0, Src, Dest, Aux),
move(1, Src, Aux, Dest),
move(N0, Aux, Src, Dest).
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Base64 decode data step by step in the Groovy programming language
You may also check:How to resolve the algorithm Arrays step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Partition an integer x into n primes step by step in the Racket programming language
You may also check:How to resolve the algorithm Unicode variable names step by step in the Lily programming language