Towers of Hanoi: Difference between revisions

Content added Content deleted
(→‎{{header|Pascal}}: adding PARI/GP version)
m (→‎simple text moves: added/changed comments and whitespace.)
Line 2,804: Line 2,804:
=={{header|REXX}}==
=={{header|REXX}}==
===simple text moves===
===simple text moves===
<lang rexx>/*REXX program shows the moves to solve the Tower of Hanoi (with N disks).*/
<lang rexx>/*REXX program displays the moves to solve the Tower of Hanoi (with N disks). */
parse arg N . /*get optional number of disks from CL.*/
parse arg N . /*get optional number of disks from CL.*/
if N=='' then N=3 /*Not given? Then use default 3 towers*/
if N=='' | N=="," then N=3 /*Not specified? Then use the default.*/
#=0; z=2**N - 1 /*# disk moves so far; # of min moves.*/
#=0 /*#: the number of disk moves (so far)*/
call mov 1, 3, N /*move the top disk, then recurse ··· */
z=2**N - 1 /*Z: " " " minimum # of moves.*/
call mov 1, 3, N /*move the top disk, then recurse ··· */
say
say
say 'The minimum number of moves to solve a ' N"-disk Tower of Hanoi is " z
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. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
dsk: #=#+1 /*bump the (disk) move counter by one. */
dsk: #=#+1 /*bump the (disk) move counter by one. */
say 'step' right(#,length(z))": move disk on tower" arg(1) '───►' arg(2)
say 'step' right(#, length(z))": move disk on tower" arg(1) '───►' arg(2)
return /* [↑] display the move message (text)*/
return /* [↑] display the move message (text)*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
mov: procedure expose # z; parse arg @1, @2, @3
mov: procedure expose # z; parse arg @1, @2, @3
if @3==1 then call dsk @1, @2
if @3==1 then call dsk @1, @2
else do
else do; call mov @1, 6-@1-@2, @3-1
call mov @1, 6-@1-@2, @3-1
call mov @1, @2, 1
call mov @1, @2, 1
call mov 6-@1-@2, @2, @3-1
call mov 6-@1-@2, @2, @3-1
end
end
return</lang>
return</lang>