Towers of Hanoi: Difference between revisions

m
→‎simple text moves: added/changed whitespace and comments, changed the wording of the output.
m (Added the Sidef language)
m (→‎simple text moves: added/changed whitespace and comments, changed the wording of the output.)
Line 2,506:
=={{header|REXX}}==
===simple text moves===
<lang rexx>/*REXX program shows the moves to solve the Tower of Hanoi (with N towersdisks).*/
parse arg N . /*get optional number of disks from CL.*/
if N=='' then N=3 /*Not given? Then use default 3 towers*/
#=0; z=2**N - 1 /*# ringdisk moves so far; # of min moves.*/
call mov 1, 3, N /*move the top ringdisk, then recurse ··· */
say
say 'The minimum number of moves to solve a ' N"-disk " Tower of Hanoi is " z
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
/*─────────────────────────────DSK subroutine─────────────────────────────────*/
dsk: #=#+1 /*bump the (ringdisk) move counter by one. */
say 'step' right(#,length(z))": move disk on tower" arg(1) '───►' arg(2)
return return /* [↑] display the move message (text)*/
/*────────────────────────────────────────────────────────────────────────────*/
/*─────────────────────────────MOV subroutine─────────────────────────────────*/
mov: procedure expose # z; parse arg @1, @2, @3
if @3==1 then call dsk @1, @2
else do
call mov @1, 6-@1-@2, @3-1
call mov @1, @2, 1
call mov 6-@1-@2, @2, @3-1
end
return</lang>
{{out}}'''output''' &nbsp; when using the default input was used:
<pre>
step 1: move disk on tower 1 ───► 3
Line 2,537:
step 7: move disk on tower 1 ───► 3
 
The minimum number of moves to solve a 3-disk Tower of Hanoi is 7
 
</pre>
{{out}}'''output''' &nbsp; when the following was entered (to solve with four towersdisks): &nbsp; <tt> 4 </tt>
<pre>
step 1: move disk on tower 1 ───► 2
Line 2,558 ⟶ 2,557:
step 15: move disk on tower 2 ───► 3
 
The minimum number of moves to solve a 4-disk Tower of Hanoi is 15
</pre>