Anonymous user
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
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 /*#
call mov 1, 3, N /*move the top
say
say 'The minimum number of moves to solve a '
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
dsk: #=#+1 /*bump the (
say 'step'
/*────────────────────────────────────────────────────────────────────────────*/
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>
<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>
<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>
|