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

Published on 12 May 2024 09:40 PM

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

Source code in the rascal programming language

public void hanoi(ndisks, startPeg, endPeg){
	if(ndisks>0){
		hanoi(ndisks-1, startPeg, 6 - startPeg - endPeg);
		println("Move disk  from peg  to peg ");
		hanoi(ndisks-1, 6 - startPeg - endPeg, endPeg);
	}
}

rascal>hanoi(4,1,3)
Move disk 1 from peg 1 to peg 2
Move disk 2 from peg 1 to peg 3
Move disk 1 from peg 2 to peg 3
Move disk 3 from peg 1 to peg 2
Move disk 1 from peg 3 to peg 1
Move disk 2 from peg 3 to peg 2
Move disk 1 from peg 1 to peg 2
Move disk 4 from peg 1 to peg 3
Move disk 1 from peg 2 to peg 3
Move disk 2 from peg 2 to peg 1
Move disk 1 from peg 3 to peg 1
Move disk 3 from peg 2 to peg 3
Move disk 1 from peg 1 to peg 2
Move disk 2 from peg 1 to peg 3
Move disk 1 from peg 2 to peg 3
ok

  

You may also check:How to resolve the algorithm Sorting algorithms/Bead sort step by step in the OCaml programming language
You may also check:How to resolve the algorithm Object serialization step by step in the Ruby programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the Clojure programming language
You may also check:How to resolve the algorithm Four is the number of letters in the ... step by step in the Phix programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Java programming language