How to resolve the algorithm Towers of Hanoi step by step in the Picat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Towers of Hanoi step by step in the Picat 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 Picat programming language

Source code in the picat programming language

main => 
    hanoi(3, left, center, right).

hanoi(0, _From, _To, _Via) => true.
hanoi(N, From, To, Via) =>
    hanoi(N - 1, From, Via, To),
    printf("Move disk %w from pole %w to pole %w\n", N, From, To),
    hanoi(N - 1, Via, To, From).

main => 
   hanoi(64).

hanoi(N) => 
   printf("N=%d\n", N),
   Count = move(N, left, center, right) ,
   printf("count=%w, theoretical=%w\n", Count, 2**N-1).
 
table
move(0, _From, _To, _Via) = 0.
move(N, From, To, Via) = Count => 
    Count1 = move(N - 1, From, Via, To),
    Count2 = move(N - 1, Via, To, From),
    Count = Count1+Count2+1.

  

You may also check:How to resolve the algorithm Decorate-sort-undecorate idiom step by step in the Quackery programming language
You may also check:How to resolve the algorithm Penney's game step by step in the Quackery programming language
You may also check:How to resolve the algorithm Menu step by step in the Delphi programming language
You may also check:How to resolve the algorithm ISBN13 check digit step by step in the AppleScript programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the M2000 Interpreter programming language