Towers of Hanoi: Difference between revisions
→{{header|Imp77}}: added tested output
(→{{header|Imp77}}: added tested output) |
|||
(19 intermediate revisions by 14 users not shown) | |||
Line 10:
{{trans|Python}}
<syntaxhighlight lang="11l">F hanoi(ndisks, startPeg = 1, endPeg = 3) ->
I ndisks
hanoi(ndisks - 1, startPeg, 6 - startPeg - endPeg)
Line 419:
</syntaxhighlight>
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO MOVE n DISKS FROM src VIA via TO dest:
IF n>0:
MOVE n-1 DISKS FROM src VIA dest TO via
WRITE "Move disk from pole", src, "to pole", dest/
MOVE n-1 DISKS FROM via VIA dest TO src
MOVE 4 DISKS FROM 1 VIA 2 TO 3</syntaxhighlight>
{{out}}
<pre>Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 2 to pole 1
Move disk from pole 1 to pole 2
Move disk from pole 3 to pole 2
Move disk from pole 3 to pole 1
Move disk from pole 2 to pole 3
Move disk from pole 1 to pole 3
Move disk from pole 2 to pole 1
Move disk from pole 2 to pole 3
Move disk from pole 1 to pole 2
Move disk from pole 2 to pole 1
Move disk from pole 3 to pole 1
Move disk from pole 3 to pole 2
Move disk from pole 1 to pole 3</pre>
=={{header|Action!}}==
Line 1,146 ⟶ 1,171:
15: 3 TO 2 </pre>
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">call move(4,1,2,3)
print "Towers of Hanoi puzzle completed!"
Line 1,178 ⟶ 1,203:
Towers of Hanoi puzzle completed!
</pre>
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "Hanoi.bas"
110 CALL HANOI(4,1,3,2)
120 DEF HANOI(DISK,FRO,TO,WITH)
130 IF DISK>0 THEN
140 CALL HANOI(DISK-1,FRO,WITH,TO)
150 PRINT "Move disk";DISK;"from";FRO;"to";TO
160 CALL HANOI(DISK-1,WITH,TO,FRO)
170 END IF
180 END DEF</syntaxhighlight>
=={{header|Batch File}}==
Line 1,531 ⟶ 1,567:
<
]</syntaxhighlight>
=={{header|Bruijn}}==
{{trans|Python}}
<syntaxhighlight lang="bruijn">
:import std/Combinator .
:import std/Number .
:import std/String .
hanoi y [[[[=?2 empty go]]]]
go [(4 --3 2 0) ++ str ++ (4 --3 0 1)] ((+6) - 1 - 0)
str "Move " ++ disk ++ " from " ++ source ++ " to " ++ destination ++ "\n"
disk number→string 3
source number→string 2
destination number→string 1
</syntaxhighlight>
=={{header|C}}==
Line 2,189 ⟶ 2,240:
proc hanoi n src dst aux . .
if n >= 1
print "Move " & src & " to " & dst
.
.
</syntaxhighlight>
Line 2,470 ⟶ 2,521:
(message "Move from %S to %S" from to)
(move (- n 1) via to from)))</syntaxhighlight>
=={{header|EMal}}==
{{trans|C#}}
<syntaxhighlight lang="emal">
fun move = void by int n, int from, int to, int via
if n == 1
writeLine("Move disk from pole " + from + " to pole " + to)
return
end
move(n - 1, from, via, to)
move(1, from, to, via)
move(n - 1, via, to, from)
end
move(3, 1, 2, 3)
</syntaxhighlight>
{{out}}
<pre>
Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 2 to pole 3
Move disk from pole 1 to pole 2
Move disk from pole 3 to pole 1
Move disk from pole 3 to pole 2
Move disk from pole 1 to pole 2
</pre>
=={{header|Erlang}}==
Line 2,973 ⟶ 3,049:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Tower_of_Hanoi}}
'''Solution'''
[[File:Fōrmulæ - Tower of Hanoi 01.png]]
'''Test case'''
[[File:Fōrmulæ - Tower of Hanoi 02.png]]
[[File:Fōrmulæ - Tower of Hanoi 03.png]]
=={{header|Gambas}}==
Line 3,326 ⟶ 3,408:
return
end</syntaxhighlight>
=={{header|Imp77}}==
<syntaxhighlight lang="Imp77">
%begin
%routine do hanoi(%integer n, f, t, u)
do hanoi(n - 1, f, u, t) %if n >= 2
print string("Move disk from ".itos(f,0)." to ".itos(t,0).to string(nl))
do hanoi(n - 1, u, t, f) %if n >= 2
%end
do hanoi(4, 1, 2, 3)
print string("Towers of Hanoi puzzle completed!".to string(nl))
%end %of %program
</syntaxhighlight>
{{out}}
<pre>
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 2 to 1
Move disk from 2 to 3
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 3 to 1
Move disk from 2 to 1
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Towers of Hanoi puzzle completed!
</pre>
=={{header|Inform 7}}==
Line 3,379 ⟶ 3,493:
H(n, 1, 2, 3)
)</syntaxhighlight>
=={{header|J}}==
Line 3,508 ⟶ 3,611:
const go = hanoi(n - 1);
return
...
[a, b],
};
Line 3,520 ⟶ 3,623:
// ---------------------- TEST -----------------------
return hanoi(3)("left", "right", "mid")
})();</syntaxhighlight>
{{Out}}
Line 4,067 ⟶ 4,170:
{{out}}
same as in FreeBasic
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .TITLE HANOI
.MCALL .PRINT,.EXIT
HANOI:: MOV #4,R2
MOV #61,R3
MOV #62,R4
MOV #63,R5
JSR PC,MOVE
.EXIT
MOVE: DEC R2
BLT 1$
MOV R2,-(SP)
MOV R3,-(SP)
MOV R4,-(SP)
MOV R5,-(SP)
MOV R5,R0
MOV R4,R5
MOV R0,R4
JSR PC,MOVE
MOV (SP)+,R5
MOV (SP)+,R4
MOV (SP)+,R3
MOV (SP)+,R2
MOVB R3,3$
MOVB R4,4$
.PRINT #2$
MOV R3,R0
MOV R4,R3
MOV R5,R4
MOV R0,R5
BR MOVE
1$: RTS PC
2$: .ASCII /MOVE DISK FROM PEG /
3$: .ASCII /* TO PEG /
4$: .ASCIZ /*/
.EVEN
.END HANOI</syntaxhighlight>
{{out}}
<pre>MOVE DISK FROM PEG 1 TO PEG 3
MOVE DISK FROM PEG 1 TO PEG 2
MOVE DISK FROM PEG 2 TO PEG 3
MOVE DISK FROM PEG 1 TO PEG 3
MOVE DISK FROM PEG 3 TO PEG 1
MOVE DISK FROM PEG 3 TO PEG 2
MOVE DISK FROM PEG 2 TO PEG 1
MOVE DISK FROM PEG 1 TO PEG 2
MOVE DISK FROM PEG 2 TO PEG 3
MOVE DISK FROM PEG 2 TO PEG 1
MOVE DISK FROM PEG 1 TO PEG 3
MOVE DISK FROM PEG 2 TO PEG 3
MOVE DISK FROM PEG 3 TO PEG 2
MOVE DISK FROM PEG 3 TO PEG 1
MOVE DISK FROM PEG 1 TO PEG 2</pre>
=={{header|MAD}}==
Line 4,126 ⟶ 4,283:
MOVE DISK FROM POLE 1 TO POLE 3
MOVE DISK FROM POLE 2 TO POLE 3</pre>
=={{header|Maple}}==
Line 5,974 ⟶ 6,130:
left -> middle
right -> middle</pre>
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Move 4 1 2 3>;
};
Move {
0 e.X = ;
s.N s.Src s.Via s.Dest, <- s.N 1>: s.Next =
<Move s.Next s.Src s.Dest s.Via>
<Prout "Move disk from pole" s.Src "to pole" s.Dest>
<Move s.Next s.Via s.Src s.Dest>;
};</syntaxhighlight>
{{out}}
<pre>Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 2 to pole 3
Move disk from pole 1 to pole 2
Move disk from pole 3 to pole 1
Move disk from pole 3 to pole 2
Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 2 to pole 3
Move disk from pole 2 to pole 1
Move disk from pole 3 to pole 1
Move disk from pole 2 to pole 3
Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 2 to pole 3</pre>
=={{header|Retro}}==
Line 6,470 ⟶ 6,655:
end if;
end func;</syntaxhighlight>
=={{header|SETL}}==
<syntaxhighlight lang="setl">program hanoi;
loop for [src, dest] in move(4, 1, 2, 3) do
print("Move disk from pole " + src + " to pole " + dest);
end loop;
proc move(n, src, via, dest);
if n=0 then return []; end if;
return move(n-1, src, dest, via)
+ [[src, dest]]
+ move(n-1, via, src, dest);
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 2 to pole 3
Move disk from pole 1 to pole 2
Move disk from pole 3 to pole 1
Move disk from pole 3 to pole 2
Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 2 to pole 3
Move disk from pole 2 to pole 1
Move disk from pole 3 to pole 1
Move disk from pole 2 to pole 3
Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 2 to pole 3</pre>
=={{header|Sidef}}==
Line 6,867 ⟶ 7,082:
EndIf
Return</syntaxhighlight>
=={{header|Uiua}}==
{{works with|Uiua|0.10.0}}
<syntaxhighlight lang="bash">
F ← |1.0 (
⟨
&p ⊏[1 2] &pf "Move disc [from to]: "
| F⍜(⊡0|-1)⍜(⊏[2 3]|⇌).
F⍜(⊡0|1◌).
F⍜(⊡0|-1)⍜(⊏[1 3]|⇌)
⟩≠1⊢.
)
F [4 1 2 3]
</syntaxhighlight>
{{out}}
<pre>
Move disc [from to]: [1 3]
Move disc [from to]: [1 2]
Move disc [from to]: [3 2]
Move disc [from to]: [1 3]
Move disc [from to]: [2 1]
Move disc [from to]: [2 3]
Move disc [from to]: [1 3]
Move disc [from to]: [1 2]
Move disc [from to]: [3 2]
Move disc [from to]: [3 1]
Move disc [from to]: [2 1]
Move disc [from to]: [3 2]
Move disc [from to]: [1 3]
Move disc [from to]: [1 2]
Move disc [from to]: [3 2]
</pre>
=={{header|UNIX Shell}}==
Line 6,941 ⟶ 7,188:
start -> end
middle -> end</pre>
=={{header|Uxntal}}==
<syntaxhighlight lang="Uxntal">|10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1
|0100 ( -> )
#0102 [ LIT2 03 &count 04 ] hanoi
POP2 POP2 BRK
@hanoi ( from spare to count -: from spare to count )
( moving 0 disks is no-op )
DUP ?{ JMP2r }
( move disks 1..count-1 to the spare peg )
#01 SUB ROT SWP hanoi
( from to spare count-1 )
( print the current move )
;dict/move print-str
INCk #30 ORA .Console/write DEO
STH2
;dict/from print-str
OVR #30 ORA .Console/write DEO
;dict/to print-str
DUP #30 ORA .Console/write DEO
[ LIT2 0a -Console/write ] DEO
STH2r
( move disks 1..count-1 from the spare peg to the goal peg )
STH ROT ROT STHr hanoi
( restore original parameters for convenient recursion )
STH2 SWP STH2r INC
JMP2r
@print-str
&loop
LDAk .Console/write DEO
INC2 LDAk ?&loop
POP2 JMP2r
@dict
&move "Move 20 "disk 2000
&from 20 "from 20 "pole 2000
&to 20 "to 20 "pole 2000</syntaxhighlight>
=={{header|VBScript}}==
Line 7,046 ⟶ 7,338:
End Sub
End Module</syntaxhighlight>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Zig">
fn main() {
hanoi(4, "A", "B", "C")
}
fn hanoi(n u64, a string, b string, c string) {
if n > 0 {
hanoi(n - 1, a, c, b)
println("Move disk from ${a} to ${c}")
hanoi(n - 1, b, a, c)
}
}
</syntaxhighlight>
{{out}}
<pre>
Move disk from A to B
Move disk from A to C
Move disk from B to C
Move disk from A to B
Move disk from C to A
Move disk from C to B
Move disk from A to B
Move disk from A to C
Move disk from B to C
Move disk from B to A
Move disk from C to A
Move disk from B to C
Move disk from A to B
Move disk from A to C
Move disk from B to C
</pre>
=={{header|VTL-2}}==
Line 7,121 ⟶ 7,447:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="
construct new(disks) {
_moves = 0
Line 7,374 ⟶ 7,700:
hanoi2(22, 1, 3, 2)
print peek("millisrunning") - t2, " ms"</syntaxhighlight>
=={{header|Z80 Assembly}}==
{{works with|CP/M 3.1|YAZE-AG-2.51.2 Z80 emulator}}
{{works with|ZSM4 macro assembler|YAZE-AG-2.51.2 Z80 emulator}}
Use the /S8 switch on the ZSM4 assembler for 8 significant characters for labels and names<br><br>
<syntaxhighlight lang="z80">
;
; Towers of Hanoi using Z80 assembly language
;
; Runs under CP/M 3.1 on YAZE-AG-2.51.2 Z80 emulator
; Assembled with zsm4 on same emulator/OS, uses macro capabilities of said assembler
; Created with vim under Windows
;
; 2023-05-29 Xorph
;
;
; Useful definitions
;
bdos equ 05h ; Call to CP/M BDOS function
strdel equ 6eh ; Set string delimiter
wrtstr equ 09h ; Write string to console
nul equ 00h ; ASCII control characters
cr equ 0dh
lf equ 0ah
cnull equ '0' ; ASCII character constants
ca equ 'A'
cb equ 'B'
cc equ 'C'
disks equ 4 ; Number of disks to move
;
; Macros for BDOS calls
;
setdel macro char ; Set string delimiter to char
ld c,strdel
ld e,char
call bdos
endm
print macro msg ; Output string to console
ld c,wrtstr
ld de,msg
call bdos
endm
pushall macro ; Save required registers to stack
push af
push bc
push de
endm
popall macro ; Recall required registers from stack
pop de
pop bc
pop af
endm
;
; =====================
; Start of main program
; =====================
;
cseg
setdel nul ; Set string delimiter to 00h
ld a,disks ; Initialization:
ld b,ca ; Tower A is source
ld c,cb ; Tower B is target
ld d,cc ; Tower C is intermediate
hanoi:
;
; Parameters in registers:
; Move a disks from b (source) to c (target) via d (intermediate)
;
or a ; If 0 disks to move, return
ret z
dec a ; Move all but lowest disk from source to intermediate via target
pushall ; Save registers
ld e,c ; Exchange c and d (target and intermediate)
ld c,d
ld d,e
call hanoi ; First recursion
popall ; Restore registers
ld hl,source ; Print move of lowest disk from source to target, save registers during BDOS call
ld (hl),b ; Source is still in b
ld hl,target
ld (hl),c ; Target is back in c due to popall
pushall
print movement
popall
ld e,b ; Now move stack from intermediate to target via source
ld b,d ; Source is still in b, target in c and intermediate in d
ld d,e
jr hanoi ; Optimize tail recursion
;
; ================
; Data definitions
; ================
;
dseg
movement:
defb 'Move disk from tower '
source:
defs 1
defb ' to tower '
target:
defs 1
crlf:
defb cr,lf,nul
</syntaxhighlight>
{{out}}
<pre>
E>hanoi
Move disk from tower A to tower C
Move disk from tower A to tower B
Move disk from tower C to tower B
Move disk from tower A to tower C
Move disk from tower B to tower A
Move disk from tower B to tower C
Move disk from tower A to tower C
Move disk from tower A to tower B
Move disk from tower C to tower B
Move disk from tower C to tower A
Move disk from tower B to tower A
Move disk from tower C to tower B
Move disk from tower A to tower C
Move disk from tower A to tower B
Move disk from tower C to tower B
E>
</pre>
=={{header|Zig}}==
|