I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

# Towers of Hanoi

Towers of Hanoi
You are encouraged to solve this task according to the task description, using any language you may know.

Solve the   Towers of Hanoi   problem with recursion.

## 11l

Translation of: Python
F hanoi(ndisks, startPeg = 1, endPeg = 3) -> N   I ndisks      hanoi(ndisks - 1, startPeg, 6 - startPeg - endPeg)      print(‘Move disk #. from peg #. to peg #.’.format(ndisks, startPeg, endPeg))      hanoi(ndisks - 1, 6 - startPeg - endPeg, endPeg) hanoi(ndisks' 3)
Output:
Move disk 1 from peg 1 to peg 3
Move disk 2 from peg 1 to peg 2
Move disk 1 from peg 3 to peg 2
Move disk 3 from peg 1 to peg 3
Move disk 1 from peg 2 to peg 1
Move disk 2 from peg 2 to peg 3
Move disk 1 from peg 1 to peg 3


## 360 Assembly

Translation of: PL/I
*        Towers of Hanoi           08/09/2015HANOITOW CSECT         USING  HANOITOW,R12       r12 : base register         LR     R12,R15            establish base register         ST     R14,SAVE14         save r14BEGIN    LH     R2,=H'4'           n <===         L      R3,=C'123 '        stating position         BAL    R14,MOVE           r1=move(m,n)RETURN   L      R14,SAVE14         restore r14         BR     R14                return to callerSAVE14   DS     F                  static save r14PG       DC     CL44'xxxxxxxxxxxx Move disc from pole X to pole Y' NN       DC     F'0'POLEX    DS     F                  current polesPOLEN    DS     F                  new poles*        ....   recursive          subroutine move(n, poles)  [r2,r3]MOVE     LR     R10,R11            save stackptr (r11) in r10 temp         LA     R1,STACKLEN        amount of storage required         GETMAIN RU,LV=(R1)        allocate storage for stack         USING  STACKDS,R11        make storage addressable         LR     R11,R1             establish stack addressability         ST     R14,SAVE14M        save previous r14         ST     R10,SAVE11M        save previous r11         LR     R1,R5              restore saved argument r5BEGINM   STM    R2,R3,STACK        push arguments to stack         ST     R3,POLEX         CH     R2,=H'1'           if n<>1         BNE    RECURSE            then goto recurse         L      R1,NN         LA     R1,1(R1)           nn=nn+1         ST     R1,NN         XDECO  R1,PG              nn         MVC    PG+33(1),POLEX+0   from         MVC    PG+43(1),POLEX+1   to         XPRNT  PG,44              print "move disk from to"         B      RETURNMRECURSE  L      R2,N               n         BCTR   R2,0               n=n-1         MVC    POLEN+0(1),POLES+0 from         MVC    POLEN+1(1),POLES+2 via         MVC    POLEN+2(1),POLES+1 to         L      R3,POLEN           new poles         BAL    R14,MOVE           call move(n-1,from,via,to)         LA     R2,1               n=1         MVC    POLEN,POLES          L      R3,POLEN           new poles         BAL    R14,MOVE           call move(1,from,to,via)         L      R2,N               n         BCTR   R2,0               n=n-1         MVC    POLEN+0(1),POLES+2 via         MVC    POLEN+1(1),POLES+1 to         MVC    POLEN+2(1),POLES+0 from         L      R3,POLEN           new poles         BAL    R14,MOVE           call move(n-1,via,to,from)RETURNM  LM     R2,R3,STACK        pull arguments from stack         LR     R1,R11             current stack         L      R14,SAVE14M        restore r14         L      R11,SAVE11M        restore r11         LA     R0,STACKLEN        amount of storage to free         FREEMAIN A=(R1),LV=(R0)   free allocated storage         BR     R14                return to caller         LTORG         DROP   R12                base no longer neededSTACKDS  DSECT                     dynamic areaSAVE14M  DS     F                  saved r14SAVE11M  DS     F                  saved r11STACK    DS     0F                 stackN        DS     F                  r2 nPOLES    DS     F                  r3 polesSTACKLEN EQU    *-STACKDS         YREGS           END    HANOITOW
Output:
           1 Move disc from pole 1 to pole 3
2 Move disc from pole 1 to pole 2
3 Move disc from pole 3 to pole 2
4 Move disc from pole 1 to pole 3
5 Move disc from pole 2 to pole 1
6 Move disc from pole 2 to pole 3
7 Move disc from pole 1 to pole 3
8 Move disc from pole 1 to pole 2
9 Move disc from pole 3 to pole 2
10 Move disc from pole 3 to pole 1
11 Move disc from pole 2 to pole 1
12 Move disc from pole 3 to pole 2
13 Move disc from pole 1 to pole 3
14 Move disc from pole 1 to pole 2
15 Move disc from pole 3 to pole 2


	org	100h	lhld	6	; Top of CP/M usable memory	sphl		; Put the stack there	lxi	b,0401h	; Set up first arguments to move()	lxi	d,0203h	call	move	; move(4, 1, 2, 3)	rst	0	; quit program	;;;	Move B disks from C via D to E. move:	dcr	b	; One fewer disk in next iteration	jz	mvout	; If this was the last disk, print move and stop	push	b	; Otherwise, save registers,	push	d 	mov	a,d	; First recursive call	mov	d,e	mov	e,a	call	move	; move(B-1, C, E, D)	pop	d	; Restore registers	pop	b	call	mvout	; Print current move	mov	a,c	; Second recursive call	mov	c,d	mov	d,a	jmp	move	; move(B-1, D, C, E) - tail call optimization	;;;	Print move, saving registers.mvout:	push	b	; Save registers on stack	push	d	mov	a,c	; Store 'from' as ASCII digit in 'from' space	adi	'0'	sta	out1	mov	a,e	; Store 'to' as ASCII digit in 'to' space	adi	'0'	sta	out2	lxi	d,outstr	mvi	c,9	; CP/M call to print the string	call	5	pop	d	; Restore register contents	pop	b	ret	;;;	Move output with placeholder for pole numbersoutstr:	db	'Move disk from pole 'out1:	db	'* to pole 'out2:	db	'*',13,10,'$' Output: 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  ## 8086 Assembly  cpu 8086 bits 16 org 100hsection .text mov bx,0402h ; Set up first arguments to move() mov cx,0103h ; Registers chosen s.t. CX contains output ;;; Move BH disks from CH via BL to CLmove: dec bh ; One fewer disk in next iteration jz .out ; If this was last disk, just print move push bx ; Save the registers for a recursive call push cx xchg bl,cl ; Swap the 'to' and 'via' registers call move ; move(BH, CH, CL, BL) pop cx ; Restore the registers from the stack pop bx call .out ; Print the move xchg ch,bl ; Swap the 'from' and 'via' registers jmp move ; move(BH, BL, CH, CL) ;;; Print the move.out: mov ax,'00' ; Add ASCII 0 to both 'from' and 'to' add ax,cx ; in one 16-bit operation mov [out1],ah ; Store 'from' field in output mov [out2],al ; Store 'to' field in output mov dx,outstr ; MS-DOS system call to print string mov ah,9 int 21h retsection .dataoutstr: db 'Move disk from pole 'out1: db '* to pole 'out2: db '*',13,10,'$'
Output:
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


## 8th

 5 var, disksvar savar sbvar sc : save sc ! sb ! sa ! disks ! ;: get sa @ sb @ sc @ ;: get2 get swap ;: hanoi	save disks @ not if ;; then	disks @ get	disks @ n:1- get2 hanoi save	cr 	" move a ring from " .  sa @ . " to " . sb @ .	disks @ n:1- get2 rot hanoi; " Tower of Hanoi, with " . disks @ . " rings: " . disks @ 1 2 3 hanoi cr bye

## ActionScript

public function move(n:int, from:int, to:int, via:int):void{    if (n > 0)    {        move(n - 1, from, via, to);        trace("Move disk from pole " + from + " to pole " + to);        move(n - 1, via, to, from);    }}

with Ada.Text_Io; use Ada.Text_Io; procedure Towers is   type Pegs is (Left, Center, Right);   procedure Hanoi (Ndisks : Natural; Start_Peg : Pegs := Left; End_Peg : Pegs := Right; Via_Peg : Pegs := Center) is   begin      if Ndisks > 0 then         Hanoi(Ndisks - 1, Start_Peg, Via_Peg, End_Peg);         Put_Line("Move disk" & Natural'Image(Ndisks) & " from " & Pegs'Image(Start_Peg) & " to " & Pegs'Image(End_Peg));         Hanoi(Ndisks - 1, Via_Peg, End_Peg, Start_Peg);      end if;   end Hanoi;begin   Hanoi(4);end Towers;

## Agena

move := proc(n::number, src::number, dst::number, via::number) is   if n > 0 then      move(n - 1, src, via, dst)      print(src & ' to ' & dst)      move(n - 1, via, dst, src)   fiend move(4, 1, 2, 3)

## ALGOL 68

PROC move = (INT n, from, to, via) VOID:  IF n > 0 THEN    move(n - 1, from, via, to);    printf(($"Move disk from pole "g" to pole "gl$, from, to));    move(n - 1, via, to, from)  FI; main: (  move(4, 1,2,3))

COMMENT Disk number is also printed in this code (works with a68g): COMMENT

 PROC move = (INT n, from, to, via) VOID:  IF n > 0 THEN    move(n - 1, from, via, to);    printf(($"Move disk "g" from pole "g" to pole "gl$, n,  from, to));    move(n - 1, via, to, from)  FI ;main: (  move(4, 1,2,3))

## ALGOL-M

beginprocedure move(n, src, via, dest);integer n;string(1) src, via, dest;begin    if n > 0 then    begin        move(n-1, src, dest, via);        write("Move disk from pole ");        writeon(src);        writeon(" to pole ");        writeon(dest);        move(n-1, via, src, dest);    end;end; move(4, "1", "2", "3");end
Output:
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

## ALGOL W

Following Agena, Algol 68, AmigaE...

begin    procedure move ( integer value n, from, to, via ) ;        if n > 0 then begin            move( n - 1, from, via, to );            write( i_w := 1, s_w := 0, "Move disk from peg: ", from, " to peg: ", to );            move( n - 1, via, to, from )        end move ;     move( 4, 1, 2, 3 )end.

## AmigaE

PROC move(n, from, to, via)  IF n > 0    move(n-1, from, via, to)    WriteF('Move disk from pole \d to pole \d\n', from, to)    move(n-1, via, to, from)  ENDIFENDPROC PROC main()  move(4, 1,2,3)ENDPROC

## APL

Works with: Dyalog APL
hanoi←{    move←{        n from to via←⍵        n≤0:⍬        l←∇(n-1) from via to        r←∇(n-1) via to from        l,(⊂from to),r    }    '⊂Move disk from pole ⊃,I1,⊂ to pole ⊃,I1'⎕FMT↑move ⍵}
Output:
      hanoi 4 1 2 3
Move disk from pole 1 to pole 3
Move disk from pole 1 to pole 2
Move disk from pole 3 to pole 2
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 3
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 1
Move disk from pole 3 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 1 to pole 2
Move disk from pole 3 to pole 2

## AppleScript

---------------------- TOWERS OF HANOI --------------------- -- hanoi :: Int -> (String, String, String) -> [(String, String)]on hanoi(n, abc)    script go        on |λ|(n, {x, y, z})            if n > 0 then                |λ|(n - 1, {x, z, y}) & ¬                    {{x, y}} & |λ|(n - 1, {z, y, x})            else                {}            end if        end |λ|    end script    go's |λ|(n, abc)end hanoi  ---------------------------- TEST --------------------------on run    script arrow        on |λ|(abc)            item 1 of abc & " -> " & item 2 of abc        end |λ|    end script     unlines(map(arrow, ¬        hanoi(3, {"left", "right", "mid"})))end run  --------------------- GENERIC FUNCTIONS -------------------- -- Lift 2nd class handler function into 1st class script wrapper -- mReturn :: First-class m => (a -> b) -> m (a -> b)on mReturn(f)    if class of f is script then        f    else        script            property |λ| : f        end script    end ifend mReturn -- map :: (a -> b) -> [a] -> [b]on map(f, xs)    tell mReturn(f)        set lng to length of xs        set lst to {}        repeat with i from 1 to lng            set end of lst to |λ|(item i of xs, i, xs)        end repeat        return lst    end tellend map -- unlines :: [String] -> Stringon unlines(xs)    set {dlm, my text item delimiters} to ¬        {my text item delimiters, linefeed}    set str to xs as text    set my text item delimiters to dlm    strend unlines
Output:
left -> right
left -> mid
right -> mid
left -> right
mid -> left
mid -> right
left -> right

More illustratively:

(I've now eliminated the recursive |move|() handler's tail calls. So it's now only called 2 ^ (n - 1) times as opposed to 2 ^ (n + 1) - 1 with full recursion. The maximum call depth of n is only reached once, whereas with full recursion, the maximum depth was n + 1 and this was reached 2 ^ n times.)

on hanoi(n, source, target)    set t1 to tab & "tower 1: " & tab    set t2 to tab & "tower 2: " & tab    set t3 to tab & "tower 3: " & tab     script o        property m : 0        property tower1 : {}        property tower2 : {}        property tower3 : {}        property towerRefs : {a reference to tower1, a reference to tower2, a reference to tower3}        property process : missing value         on |move|(n, source, target)            set aux to 6 - source - target            repeat with n from n to 2 by -1 -- Tail call elimination repeat.                |move|(n - 1, source, aux)                set end of item target of my towerRefs to n                tell item source of my towerRefs to set its contents to reverse of rest of its reverse                set m to m + 1                set end of my process to ¬                    {(m as text) & ". move disc " & n & (" from tower " & source) & (" to tower " & target & ":"), ¬                        t1 & tower1, ¬                        t2 & tower2, ¬                        t3 & tower3}                tell source                    set source to aux                    set aux to it                end tell            end repeat            -- Specific code for n = 1:            set end of item target of my towerRefs to 1            tell item source of my towerRefs to set its contents to reverse of rest of its reverse            set m to m + 1            set end of my process to ¬                {(m as text) & ". move disc 1 from tower " & source & (" to tower " & target & ":"), ¬                    t1 & tower1, ¬                    t2 & tower2, ¬                    t3 & tower3}        end |move|    end script     repeat with i from n to 1 by -1        set end of item source of o's towerRefs to i    end repeat     set astid to AppleScript's text item delimiters    set AppleScript's text item delimiters to ", "    set o's process to {"Starting with " & n & (" discs on tower " & (source & ":")), ¬        t1 & o's tower1, t2 & o's tower2, t3 & o's tower3}    if (n > 0) then tell o to |move|(n, source, target)    set end of o's process to "That's it!"    set AppleScript's text item delimiters to linefeed    set process to o's process as text    set AppleScript's text item delimiters to astid     return processend hanoi -- Test:set numberOfDiscs to 3set sourceTower to 1set destinationTower to 2hanoi(numberOfDiscs, sourceTower, destinationTower)
Output:
"Starting with 3 discs on tower 1:
tower 1:     3, 2, 1
tower 2:
tower 3:
1. move disc 1 from tower 1 to tower 2:
tower 1:     3, 2
tower 2:     1
tower 3:
2. move disc 2 from tower 1 to tower 3:
tower 1:     3
tower 2:     1
tower 3:     2
3. move disc 1 from tower 2 to tower 3:
tower 1:     3
tower 2:
tower 3:     2, 1
4. move disc 3 from tower 1 to tower 2:
tower 1:
tower 2:     3
tower 3:     2, 1
5. move disc 1 from tower 3 to tower 1:
tower 1:     1
tower 2:     3
tower 3:     2
6. move disc 2 from tower 3 to tower 2:
tower 1:     1
tower 2:     3, 2
tower 3:
7. move disc 1 from tower 1 to tower 2:
tower 1:
tower 2:     3, 2, 1
tower 3:
That's it!"

## ARM Assembly

.text.global	_start_start:	mov	r0,#4		@ 4 disks,	mov	r1,#1		@ from pole 1,	mov	r2,#2		@ via pole 2,	mov	r3,#3		@ to pole 3.	bl	move	mov	r0,#0		@ Exit to Linux afterwards	mov	r7,#1	swi	#0	@@@	Move r0 disks from r1 via r2 to r3move:	subs	r0,r0,#1	@ One fewer disk in next iteration	beq	show		@ If last disk, just print move	push	{r0-r3,lr}	@ Save all the registers incl. link register	eor	r2,r2,r3	@ Swap the 'to' and 'via' registers	eor	r3,r2,r3	eor	r2,r2,r3	bl	move		@ Recursive call	pop	{r0-r3}		@ Restore all the registers except LR	bl	show		@ Show current move	eor	r1,r1,r3	@ Swap the 'to' and 'via' registers	eor	r3,r1,r3	eor	r1,r1,r3	pop	{lr}		@ Restore link register	b	move		@ Tail call	@@@	Show moveshow:	push	{r0-r3,lr}	@ Save all the registers	add	r1,r1,#'0	@ Write the source pole	ldr	lr,=spole	strb	r1,[lr] 	add	r3,r3,#'0	@ Write the destination pole	ldr	lr,=dpole	strb	r3,[lr]	mov	r0,#1		@ 1 = stdout	ldr	r1,=moves	@ Pointer to string	ldr	r2,=mlen	@ Length of string	mov	r7,#4		@ 4 = Linux write syscall	swi	#0 		@ Print the move	pop	{r0-r3,pc}	@ Restore all the registers and return.datamoves:	.ascii	"Move disk from pole "spole:	.ascii	"* to pole "dpole:	.ascii	"*\n"mlen	=	. - moves
Output:
Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 3 to pole 1
Move disk from pole 1 to pole 2
Move disk from pole 2 to pole 3
Move disk from pole 2 to pole 1
Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 3 to pole 1
Move disk from pole 3 to pole 2
Move disk from pole 2 to pole 3
Move disk from pole 3 to pole 1
Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 3 to pole 1

## Arturo

Translation of: D
hanoi: function [n f dir via][	if n>0 [		hanoi n-1 f via dir		print ["Move disk" n "from" f "to" dir]		hanoi n-1 via dir f	]] hanoi 3 'L 'M 'R
Output:
Move disk 1 from L to M
Move disk 2 from L to R
Move disk 1 from M to R
Move disk 3 from L to M
Move disk 1 from R to L
Move disk 2 from R to M
Move disk 1 from L to M

## AutoHotkey

move(n, from, to, via)  ;n = # of disks, from = start pole, to = end pole, via = remaining pole {  if (n = 1)  {    msgbox , Move disk from pole %from% to pole %to%   }  else  {    move(n-1, from, via, to)    move(1, from, to, via)    move(n-1, via, to, from)  }}move(64, 1, 3, 2)

## AutoIt

Func move($n,$from, $to,$via)	If ($n = 1) Then ConsoleWrite(StringFormat("Move disk from pole "&$from&" To pole "&$to&"\n")) Else move($n - 1, $from,$via, $to) move(1,$from, $to,$via)		move($n - 1,$via, $to,$from)	EndIfEndFunc move(4, 1,2,3)

## AWK

Translation of: Logo

## BCPL

get "libhdr" let start() be move(4, 1, 2, 3)and move(n, src, via, dest) be if n > 0 do $( move(n-1, src, dest, via) writef("Move disk from pole %N to pole %N*N", src, dest); move(n-1, via, src, dest)$)
Output:
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

## Befunge

This is loosely based on the Python sample. The number of disks is specified by the first integer on the stack (the initial character 4 in the example below). If you want the program to prompt the user for that value, you can replace the 4 with a & (the read integer command).

48*2+1>#v_:!#@_0" ksid evoM">:#,_$:8/:.v>8v8:<$#<+9-+*2%3\*3/3:,+55.+1%3:$_,#!>#:<: >/!#^_:0\:8/1-8vv,_$8%:3/1+.>0" gep ot"^^++3-%3\*2/3:%8\*<>:^:"from peg "0\*8-1<
Output:
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

## Bracmat

( ( move  =   n from to via    .   !arg:(?n,?from,?to,?via)      & (   !n:>0          & move$(!n+-1,!from,!via,!to) & out$("Move disk from pole " !from " to pole " !to)          & move$(!n+-1,!via,!to,!from) | ) )& move$(4,1,2,3));
Output:
Move disk from pole  1  to pole  3
Move disk from pole  1  to pole  2
Move disk from pole  3  to pole  2
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  3
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  1
Move disk from pole  3  to pole  2
Move disk from pole  1  to pole  3
Move disk from pole  1  to pole  2
Move disk from pole  3  to pole  2

## Brainf***

[This implementation is recursive and usesa stack, consisting of frames that are 8bytes long. The layout is as follows: Byte   Description   0   recursion flag       (the program stops if the flag is        zero)   1   the step which is currently       executed       4 means a call to               move(a, c, b, n - 1)       3 means a call to               move(a, b, c, 1)       2 means a call to               move(b, a, c, n - 1)       1 prints the source and dest pile   2   flag to check whether the current       step has already been done or if       it still must be executed   3   the step which will be executed       in the next loop   4   the source pile   5   the helper pile   6   the destination pile   7   the number of disks to move The first stack frame (0 0 0 0 0 0 0 0)is used to abort the recursion.] >>>>>>>> These are the parameters for the program(1 4 1 0 'a 'b 'c 5)+>++++>+>>>>>>++++++++[<++++++++++++>-]<[<<<+>+>+>-]<<<+>++>+++>+++++><<<<<<<< [> while (recurse)  [- if (step gt 0)    >[-]+< todo = 1    [- if (step gt 1)      [- if (step gt 2)        [- if (step gt 3)          >>+++<< next = 3          >-< todo = 0          >>>>>>[>+>+<<-]>[<+>-]> n dup          -          [[-] if (sub(n 1) gt 0)            <+>>>++++> push (1 0 0 4)             copy and push a            <<<<<<<<[>>>>>>>>+>+            <<<<<<<<<-]>>>>>>>>            >[<<<<<<<<<+>>>>>>>>>-]< >             copy and push c            <<<<<<<[>>>>>>>+>+            <<<<<<<<-]>>>>>>>            >[<<<<<<<<+>>>>>>>>-]< >             copy and push b            <<<<<<<<<[>>>>>>>>>+>+            <<<<<<<<<<-]>>>>>>>>>            >[<<<<<<<<<<+>>>>>>>>>>-]< >             copy n and push sub(n 1)            <<<<<<<<[>>>>>>>>+>+            <<<<<<<<<-]>>>>>>>>            >[<<<<<<<<<+>>>>>>>>>-]< -            >>          ]          <<<<<<<<        ]        >[-< if ((step gt 2) and todo)          >>++<< next = 2          >>>>>>>          +>>>+> push 1 0 0 1 a b c 1          <<<<<<<<[>>>>>>>>+>+          <<<<<<<<<-]>>>>>>>>          >[<<<<<<<<<+>>>>>>>>>-]< > a          <<<<<<<<[>>>>>>>>+>+          <<<<<<<<<-]>>>>>>>>          >[<<<<<<<<<+>>>>>>>>>-]< > b          <<<<<<<<[>>>>>>>>+>+          <<<<<<<<<-]>>>>>>>>          >[<<<<<<<<<+>>>>>>>>>-]< > c          + >>        >]<      ]      >[-< if ((step gt 1) and todo)        >>>>>>[>+>+<<-]>[<+>-]> n dup        -        [[-] if (n sub 1 gt 0)          <+>>>++++> push (1 0 0 4)           copy and push b          <<<<<<<[>>>>>>>+          <<<<<<<-]>>>>>>>          >[<<<<<<<<+>>>>>>>>-]< >           copy and push a          <<<<<<<<<[>>>>>>>>>+          <<<<<<<<<-]>>>>>>>>>          >[<<<<<<<<<<+>>>>>>>>>>-]< >           copy and push c          <<<<<<<<[>>>>>>>>+          <<<<<<<<-]>>>>>>>>          >[<<<<<<<<<+>>>>>>>>>-]< >           copy n and push sub(n 1)          <<<<<<<<[>>>>>>>>+>+          <<<<<<<<<-]>>>>>>>>          >[<<<<<<<<<+>>>>>>>>>-]< -          >>        ]        <<<<<<<<      >]<    ]    >[-< if ((step gt 0) and todo)      >>>>>>>      >++++[<++++++++>-]<      >>++++++++[<+++++++++>-]<++++      >>++++++++[<++++++++++++>-]<+++++      >>+++++++++[<++++++++++++>-]<+++      <<<      >.+++++++>.++.--.<<.      >>-.+++++.----.<<.      >>>.<---.+++.>+++.+.+.<.<<.      >.>--.+++++.---.++++.        -------.+++.<<.      >>>++.-------.-.<<<.      >+.>>+++++++.---.-----.<<<.      <<<<.>>>>.      >>----.>++++++++.<+++++.<<.      >.>>.---.-----.<<<.      <<.>>++++++++++++++.      >>>[-]<[-]<[-]<[-]      +++++++++++++.---.[-]      <<<<<<<    >]<    >>[<<+>>-]<< step = next  ]  return with clear stack frame  <[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<  <<<<<<<<  >>[<<+>>-]<< step = next  <]

## C

#include <stdio.h> void move(int n, int from, int via, int to){  if (n > 1) {    move(n - 1, from, to, via);    printf("Move disk from pole %d to pole %d\n", from, to);    move(n - 1, via, from, to);  } else {    printf("Move disk from pole %d to pole %d\n", from, to);  }}int main(){  move(4, 1,2,3);  return 0;}
Animate it for fun:
#include <stdio.h>#include <stdlib.h>#include <unistd.h> typedef struct { int *x, n; } tower;tower *new_tower(int cap){	tower *t = calloc(1, sizeof(tower) + sizeof(int) * cap);	t->x = (int*)(t + 1);	return t;} tower *t;int height; void text(int y, int i, int d, const char *s){	printf("\033[%d;%dH", height - y + 1, (height + 1) * (2 * i + 1) - d);	while (d--) printf("%s", s);} void add_disk(int i, int d){	t[i]->x[t[i]->n++] = d;	text(t[i]->n, i, d, "=="); 	usleep(100000);	fflush(stdout);} int remove_disk(int i){	int d = t[i]->x[--t[i]->n];	text(t[i]->n + 1, i, d, "  ");	return d;} void move(int n, int from, int to, int via){	if (!n) return; 	move(n - 1, from, via, to);	add_disk(to, remove_disk(from));	move(n - 1, via, to, from);} int main(int c, char *v[]){	puts("\033[H\033[J"); 	if (c <= 1 || (height = atoi(v)) <= 0)		height = 8;	for (c = 0; c < 3; c++)	 t[c] = new_tower(height);	for (c = height; c; c--) add_disk(0, c); 	move(height, 0, 2, 1); 	text(1, 0, 1, "\n");	return 0;}

## C#

public  void move(int n, int from, int to, int via) {   if (n == 1) {     System.Console.WriteLine("Move disk from pole " + from + " to pole " + to);   } else {     move(n - 1, from, via, to);     move(1, from, to, via);     move(n - 1, via, to, from);   } }

## C++

Works with: g++
void move(int n, int from, int to, int via) {  if (n == 1) {    std::cout << "Move disk from pole " << from << " to pole " << to << std::endl;  } else {    move(n - 1, from, via, to);    move(1, from, to, via);    move(n - 1, via, to, from);  }}

## Clojure

### Side-Effecting Solution

(defn towers-of-hanoi [n from to via]  (when (pos? n)    (towers-of-hanoi (dec n) from via to)    (printf "Move from %s to %s\n" from to)    (recur (dec n) via to from)))

### Lazy Solution

(defn towers-of-hanoi [n from to via]  (when (pos? n)    (lazy-cat (towers-of-hanoi (dec n) from via to)              (cons [from '-> to]                    (towers-of-hanoi (dec n) via to from)))))

## COBOL

Translation of: C
Works with: OpenCOBOL version 2.0
       >>SOURCE FREEIDENTIFICATION DIVISION.PROGRAM-ID. towers-of-hanoi. PROCEDURE DIVISION.    CALL "move-disk" USING 4, 1, 2, 3    .END PROGRAM towers-of-hanoi. IDENTIFICATION DIVISION.PROGRAM-ID. move-disk RECURSIVE. DATA DIVISION.LINKAGE SECTION.01  n                         PIC 9 USAGE COMP.01  from-pole                 PIC 9 USAGE COMP.01  to-pole                   PIC 9 USAGE COMP.01  via-pole                  PIC 9 USAGE COMP. PROCEDURE DIVISION USING n, from-pole, to-pole, via-pole.    IF n > 0       SUBTRACT 1 FROM n       CALL "move-disk" USING CONTENT n, from-pole, via-pole, to-pole       DISPLAY "Move disk from pole " from-pole " to pole " to-pole       CALL "move-disk" USING CONTENT n, via-pole, to-pole, from-pole    END-IF    .END PROGRAM move-disk.
 IDENTIFICATION DIVISION.PROGRAM-ID. towers-of-hanoi. PROCEDURE DIVISION.    CALL "move-disk" USING 4, 1, 2, 3    .END PROGRAM towers-of-hanoi. IDENTIFICATION DIVISION.PROGRAM-ID. move-disk RECURSIVE. DATA DIVISION.LINKAGE SECTION.01  n                         PIC 9 USAGE COMP.01  from-pole                 PIC 9 USAGE COMP.01  to-pole                   PIC 9 USAGE COMP.01  via-pole                  PIC 9 USAGE COMP. PROCEDURE DIVISION USING n, from-pole, to-pole, via-pole.    IF n > 0       SUBTRACT 1 FROM n       CALL "move-disk" USING CONTENT n, from-pole, via-pole, to-pole       ADD 1 TO n       DISPLAY "Move disk number "n " from pole " from-pole " to pole " to-pole       SUBTRACT 1 FROM n       CALL "move-disk" USING CONTENT n, via-pole, to-pole, from-pole    END-IF    .END PROGRAM move-disk.

### ANSI-74 solution

Early versions of COBOL did not have recursion. There are no locally-scoped variables and the call of a procedure does not have to use a stack to save return state. Recursion would cause undefined results. It is therefore necessary to use an iterative algorithm. This solution is an adaptation of Kolar's Hanoi Tower algorithm no. 1.

Works with: CIS COBOL version 4.2
Works with: GnuCOBOL version 3.0-rc1.0
       IDENTIFICATION DIVISION.       PROGRAM-ID. ITERATIVE-TOWERS-OF-HANOI.       AUTHOR. SOREN ROUG.       DATE-WRITTEN. 2019-06-28.       ENVIRONMENT DIVISION.       CONFIGURATION SECTION.       SOURCE-COMPUTER. LINUX.       OBJECT-COMPUTER. KAYPRO4.       INPUT-OUTPUT SECTION.       FILE-CONTROL.       DATA DIVISION.       WORKING-STORAGE SECTION.       77  NUM-DISKS                   PIC 9 VALUE 4.       77  N1                          PIC 9 COMP.       77  N2                          PIC 9 COMP.       77  FROM-POLE                   PIC 9 COMP.       77  TO-POLE                     PIC 9 COMP.       77  VIA-POLE                    PIC 9 COMP.       77  FP-TMP                      PIC 9 COMP.       77  TO-TMP                      PIC 9 COMP.       77  P-TMP                       PIC 9 COMP.       77  TMP-P                       PIC 9 COMP.       77  I                           PIC 9 COMP.       77  DIV                         PIC 9 COMP.       01  STACKNUMS.           05  NUMSET OCCURS 3 TIMES.               10  DNUM                PIC 9 COMP.       01  GAMESET.           05  POLES OCCURS 3 TIMES.               10  STACK OCCURS 10 TIMES.                   15  POLE            PIC 9 USAGE COMP.        PROCEDURE DIVISION.       HANOI.           DISPLAY "TOWERS OF HANOI PUZZLE WITH ", NUM-DISKS, " DISKS.".           ADD NUM-DISKS, 1 GIVING N1.           ADD NUM-DISKS, 2 GIVING N2.           MOVE 1 TO DNUM (1).           MOVE N1 TO DNUM (2), DNUM (3).           MOVE N1 TO POLE (1, N1), POLE (2, N1), POLE (3, N1).           MOVE 1 TO POLE (1, N2).           MOVE 2 TO POLE (2, N2).           MOVE 3 TO POLE (3, N2).           MOVE 1 TO I.           PERFORM INIT-PUZZLE UNTIL I = N1.           MOVE 1 TO FROM-POLE.           DIVIDE 2 INTO NUM-DISKS GIVING DIV.           MULTIPLY 2 BY DIV.           IF DIV NOT = NUM-DISKS PERFORM INITODD ELSE PERFORM INITEVEN.           PERFORM MOVE-DISK UNTIL DNUM (3) NOT > 1.           DISPLAY "TOWERS OF HANOI PUZZLE COMPLETED!".           STOP RUN.       INIT-PUZZLE.           MOVE I TO POLE (1, I).           MOVE 0 TO POLE (2, I), POLE (3, I).           ADD 1 TO I.       INITEVEN.           MOVE 2 TO TO-POLE.           MOVE 3 TO VIA-POLE.       INITODD.           MOVE 3 TO TO-POLE.           MOVE 2 TO VIA-POLE.       MOVE-DISK.           MOVE DNUM (FROM-POLE) TO FP-TMP.           MOVE POLE (FROM-POLE, FP-TMP) TO I.           DISPLAY "MOVE DISK FROM ", POLE (FROM-POLE, N2),               " TO ", POLE (TO-POLE, N2).           ADD 1 TO DNUM (FROM-POLE).           MOVE VIA-POLE TO TMP-P.           SUBTRACT 1 FROM DNUM (TO-POLE).           MOVE DNUM (TO-POLE) TO TO-TMP.           MOVE I TO POLE (TO-POLE, TO-TMP).           DIVIDE 2 INTO I GIVING DIV.           MULTIPLY 2 BY DIV.           IF I NOT = DIV PERFORM MOVE-TO-VIA ELSE               PERFORM MOVE-FROM-VIA.       MOVE-TO-VIA.           MOVE TO-POLE TO VIA-POLE.           MOVE DNUM (FROM-POLE) TO FP-TMP.           MOVE DNUM (TMP-P) TO P-TMP.           IF POLE (FROM-POLE, FP-TMP) > POLE (TMP-P, P-TMP)               PERFORM MOVE-FROM-TO           ELSE MOVE TMP-P TO TO-POLE.       MOVE-FROM-TO.           MOVE FROM-POLE TO TO-POLE.           MOVE TMP-P TO FROM-POLE.           MOVE DNUM (FROM-POLE) TO FP-TMP.           MOVE DNUM (TMP-P) TO P-TMP.       MOVE-FROM-VIA.           MOVE FROM-POLE TO VIA-POLE.           MOVE TMP-P TO FROM-POLE.

## CoffeeScript

hanoi = (ndisks, start_peg=1, end_peg=3) ->  if ndisks    staging_peg = 1 + 2 + 3 - start_peg - end_peg    hanoi(ndisks-1, start_peg, staging_peg)    console.log "Move disk #{ndisks} from peg #{start_peg} to #{end_peg}"    hanoi(ndisks-1, staging_peg, end_peg) hanoi(4)

## Common Lisp

(defun move (n from to via)  (cond ((= n 1)         (format t "Move from ~A to ~A.~%" from to))        (t         (move (- n 1) from via to)         (format t "Move from ~A to ~A.~%" from to)         (move (- n 1) via to from))))

## D

### Recursive Version

import std.stdio; void hanoi(in int n, in char from, in char to, in char via) {    if (n > 0) {        hanoi(n - 1, from, via, to);        writefln("Move disk %d from %s to %s", n, from, to);        hanoi(n - 1, via, to, from);    }} void main() {    hanoi(3, 'L', 'M', 'R');}
Output:
Move disk 1 from L to M
Move disk 2 from L to R
Move disk 1 from M to R
Move disk 3 from L to M
Move disk 1 from R to L
Move disk 2 from R to M
Move disk 1 from L to M

### Fast Iterative Version

// Code found and then improved by Glenn C. Rhoads,// then some more by M. Kolar (2000).void main(in string[] args) {    import core.stdc.stdio, std.conv, std.typetuple;     immutable size_t n = (args.length > 1) ? args.to!size_t : 3;    size_t p = [(1 << n) - 1, 0, 0];     // Show the start configuration of the pegs.    '|'.putchar;    foreach_reverse (immutable i; 1 .. n + 1)        printf(" %d", i);    "\n|\n|".puts;     foreach (immutable size_t x; 1 .. (1 << n)) {        {            immutable size_t i1 = x & (x - 1);            immutable size_t fr = (i1 + i1 / 3) & 3;            immutable size_t i2 = (x | (x - 1)) + 1;            immutable size_t to = (i2 + i2 / 3) & 3;             size_t j = 1;            for (size_t w = x; !(w & 1); w >>= 1, j <<= 1) {}             // Now j is not the number of the disk to move,            // it contains the single bit to be moved:            p[fr] &= ~j;            p[to] |= j;        }         // Show the current configuration of pegs.        foreach (immutable size_t k; TypeTuple!(0, 1, 2)) {            "\n|".printf;            size_t j = 1 << n;            foreach_reverse (immutable size_t w; 1 .. n + 1) {                j >>= 1;                if (j & p[k])                    printf(" %zd", w);            }        }        '\n'.putchar;    }}
Output:
| 3 2 1
|
|

| 3 2
|
| 1

| 3
| 2
| 1

| 3
| 2 1
|

|
| 2 1
| 3

| 1
| 2
| 3

| 1
|
| 3 2

|
|
| 3 2 1


## Dart

main() {   moveit(from,to) {    print("move ${from} --->${to}");  }   hanoi(height,toPole,fromPole,usePole) {    if (height>0) {      hanoi(height-1,usePole,fromPole,toPole);        moveit(fromPole,toPole);      hanoi(height-1,toPole,usePole,fromPole);    }  }   hanoi(3,3,1,2);}

The same as above, with optional static type annotations and styled according to http://www.dartlang.org/articles/style-guide/

main() {  String say(String from, String to) => "$from --->$to";    hanoi(int height, int toPole, int fromPole, int usePole) {    if (height > 0) {      hanoi(height - 1, usePole, fromPole, toPole);        print(say(fromPole.toString(), toPole.toString()));      hanoi(height - 1, toPole, usePole, fromPole);    }  }   hanoi(3, 3, 1, 2);}
Output:
move 1 ---> 3
move 1 ---> 2
move 3 ---> 2
move 1 ---> 3
move 2 ---> 1
move 2 ---> 3
move 1 ---> 3


## Dc

From Here

 [ # move(from, to)
n           # print from
[ --> ]n    # print " --> "
p           # print to\n
sw          # p doesn't pop, so get rid of the value
]sm

[ # init(n)
sw          # tuck n away temporarily
9           # sentinel as bottom of stack
lw          # bring n back
1           # "from" tower's label
3           # "to" tower's label
0           # processed marker
]si

[ # Move()
lt          # push to
lf          # push from
lmx         # call move(from, to)
]sM

[ # code block <d>
ln          # push n
lf          # push from
lt          # push to
1           # push processed marker 1
ln          # push n
1           # push 1
-           # n - 1
lf          # push from
ll          # push left
0           # push processed marker 0
]sd

[ # code block <e>
ln          # push n
1           # push 1
-           # n - 1
ll          # push left
lt          # push to
0           # push processed marker 0
]se

[ # code block <x>
ln 1 =M
ln 1 !=d
]sx

[ # code block <y>
lMx
lex
]sy

[ # quit()
q           # exit the program
]sq

[ # run()
d 9 =q      # if stack empty, quit()
sp          # processed
st          # to
sf          # from
sn          # n
6           #
lf          #
-           #
lt          #
-           # 6 - from - to
sl          #
lp 0 =x     #
lp 0 !=y    #
lrx         # loop
]sr

5lix # init(n)
lrx # run()


See Pascal.

## Dyalect

Translation of: Swift
func hanoi(n, a, b, c) {    if n > 0 {        hanoi(n - 1, a, c, b)        print("Move disk from \(a) to \(c)")        hanoi(n - 1, b, a, c)    }} hanoi(4, "A", "B", "C")
Output:
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

## Elena

ELENA 4.x :

move = (n,from,to,via){    if (n == 1)    {        console.printLine("Move disk from pole ",from," to pole ",to)    }    else    {        move(n-1,from,via,to);        move(1,from,to,via);        move(n-1,via,to,from)    }};

## Elixir

defmodule RC do  def hanoi(n) when 0<n and n<10, do: hanoi(n, 1, 2, 3)   defp hanoi(1, f, _, t), do: move(f, t)  defp hanoi(n, f, u, t) do    hanoi(n-1, f, t, u)    move(f, t)    hanoi(n-1, u, f, t)  end   defp move(f, t), do: IO.puts "Move disk from #{f} to #{t}"end RC.hanoi(3)
Output:
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


## Emacs Lisp

Translation of: Common Lisp
 (defun move (n from to via)  (cond ((= n 1)         (print (format "Move from %S to %S" from to)))        (t	 (progn	   (move (- n 1) from via to)	   (print (format "Move from %S to %S" from to))	   (move (- n 1) via to from)))))

## Erlang

move(1, F, T, _V) ->   io:format("Move from ~p to ~p~n", [F, T]);move(N, F, T, V) ->   move(N-1, F, V, T),   move(1  , F, T, V),  move(N-1, V, T, F).

## ERRE

 !-----------------------------------------------------------! HANOI.R : solve tower of Hanoi puzzle using a recursive ! modified algorithm.!----------------------------------------------------------- PROGRAM HANOI !$INTEGER !VAR I,J,MOSSE,NUMBER PROCEDURE PRINTMOVE LOCAL SOURCE$,DEST$MOSSE=MOSSE+1 CASE I OF 1-> SOURCE$="Left" END ->     2-> SOURCE$="Center" END -> 3-> SOURCE$="Right" END ->  END CASE  CASE J OF     1-> DEST$="Left" END -> 2-> DEST$="Center" END ->     3-> DEST$="Right" END -> END CASE PRINT("I move a disk from ";SOURCE$;" to ";DEST$)END PROCEDURE PROCEDURE MOVE IF NUMBER<>0 THEN NUMBER=NUMBER-1 J=6-I-J MOVE J=6-I-J PRINTMOVE I=6-I-J MOVE I=6-I-J NUMBER=NUMBER+1 END IFEND PROCEDURE BEGIN MAXNUM=12 MOSSE=0 PRINT(CHR$(12);TAB(25);"--- TOWERS OF HANOI ---")  REPEAT     PRINT("Number of disks ";)     INPUT(NUMBER)  UNTIL NUMBER>1 AND NUMBER<=MAXNUM  PRINT  PRINT("For ";NUMBER;"disks the total number of moves is";2^NUMBER-1)  I=1  ! number of source pole  J=3  ! number of destination pole  MOVEEND PROGRAM
Output:
                        --- TOWER OF HANOI ---
Number of disks ? 3

For  3 disks the total number of moves is 7
I move a disk from Left to Right
I move a disk from Left to Center
I move a disk from Right to Center
I move a disk from Left to Right
I move a disk from Center to Left
I move a disk from Center to Right
I move a disk from Left to Right


## Excel

### LAMBDA

With the names HANOI and SHOWHANOI bound to the following lambdas in the Excel worksheet Name Manager:

SHOWHANOI=LAMBDA(n,    FILTERP(        LAMBDA(x, "" <> x)    )(        HANOI(n)("left")("right")("mid")    ))  HANOI=LAMBDA(n,    LAMBDA(l,        LAMBDA(r,            LAMBDA(m,                IF(0 = n,                    "",                    LET(                        next, n - 1,                        APPEND(                            APPEND(                                HANOI(next)(l)(m)(r)                            )(                                CONCAT(l, " -> ", r)                            )                        )(                            HANOI(next)(m)(r)(l)                        )                    )                )            )        )    ))

And assuming that these generic lambdas are also bound to the following names in Name Manager:

APPEND=LAMBDA(xs,    LAMBDA(ys,        LET(            nx, ROWS(xs),            rowIndexes, SEQUENCE(nx + ROWS(ys)),            colIndexes, SEQUENCE(                1,                MAX(COLUMNS(xs), COLUMNS(ys))            ),            IF(                rowIndexes <= nx,                INDEX(xs, rowIndexes, colIndexes),                INDEX(ys, rowIndexes - nx, colIndexes)            )        )    ))  FILTERP=LAMBDA(p,    LAMBDA(xs,        FILTER(xs, p(xs))    ))

In the output below, the expression in B2 defines an array of strings which additionally populate the following cells.

Output:
 =SHOWHANOI(A2) fx A B 1 Disks Steps 2 3 left -> right 3 left -> mid 4 right -> mid 5 left -> right 6 mid -> left 7 mid -> right 8 left -> right

## Ezhil

 # (C) 2013 Ezhil Language Project# Tower of Hanoi – recursive solution நிரல்பாகம் ஹோனாய்(வட்டுகள், முதல்அச்சு, இறுதிஅச்சு,வட்டு)   @(வட்டுகள் == 1 ) ஆனால்     பதிப்பி  “வட்டு ” + str(வட்டு) + “ஐ \t  (” + str(முதல்அச்சு) + “  —> ” +  str(இறுதிஅச்சு)+ “) அச்சிற்கு நகர்த்துக.”  இல்லை   @( ["இ", "அ",  "ஆ"]  இல் அச்சு ) ஒவ்வொன்றாக          @( (முதல்அச்சு != அச்சு)  && (இறுதிஅச்சு  != அச்சு) ) ஆனால்              நடு = அச்சு          முடி  முடி     # solve problem for n-1 again between src and temp pegs                          ஹோனாய்(வட்டுகள்-1,   முதல்அச்சு,நடு,வட்டுகள்-1)     # move largest disk from src to destination    ஹோனாய்(1, முதல்அச்சு, இறுதிஅச்சு,வட்டுகள்)     # solve problem for n-1 again between different pegs    ஹோனாய்(வட்டுகள்-1, நடு, இறுதிஅச்சு,வட்டுகள்-1)  முடிமுடி ஹோனாய்(4,”அ”,”ஆ”,0)

## F#

#lightlet rec hanoi num start finish =  match num with  | 0 -> [ ]  | _ -> let temp = (6 - start - finish)         (hanoi (num-1) start temp) @ [ start, finish ] @ (hanoi (num-1) temp finish) [<EntryPoint>]let main args =  (hanoi 4 1 2) |> List.iter (fun pair -> match pair with                                          | a, b -> printf "Move disc from %A to %A\n" a b)  0

## Factor

USING: formatting kernel locals math ;IN: rosettacode.hanoi : move ( from to -- )    "%d->%d\n" printf ;:: hanoi ( n from to other -- )    n 0 > [        n 1 - from other to hanoi        from to move        n 1 - other to from hanoi    ] when ;

In the REPL:

( scratchpad ) 3 1 3 2 hanoi
1->3
1->2
3->2
1->3
2->1
2->3
1->3

## FALSE

["Move disk from "$!\" to "$!\""]p:  { to from }[n;0>[n;1-n: @\ h;! @\ p;! \@ h;! \@ n;1+n:]?]h:  { via to from }4n:["right"]["middle"]["left"]h;!%%%

## Fermat

Func Hanoi( n, f, t, v ) = if n = 0 then     !'';else     Hanoi(n - 1, f, v, t);     !f;!' -> ';!t;!',   ';    Hanoi(n - 1, v, t, f)  fi.
Output:
1 -> 3,   1 -> 2,   3 -> 2,   1 -> 3,   2 -> 1,   2 -> 3,   1 -> 3,   1 -> 2,   3 -> 2,   3 -> 1,   2 -> 1,   3 -> 2,   1 -> 3,   1 -> 2,   3 -> 2,

## FOCAL

01.10 S N=4;S S=1;S V=2;S T=301.20 D 201.30 Q 02.02 S N(D)=N(D)-1;I (N(D)),2.2,2.0402.04 S D=D+102.06 S N(D)=N(D-1);S S(D)=S(D-1)02.08 S T(D)=V(D-1);S V(D)=T(D-1)02.10 D 202.12 S D=D-102.14 D 302.16 S A=S(D);S S(D)=V(D);S V(D)=A02.18 G 2.0202.20 D 3 03.10 T %1,"MOVE DISK FROM POLE",S(D)03.20 T " TO POLE",T(D),!
Output:
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

With locals:

CREATE peg1 ," left "   CREATE peg2 ," middle " CREATE peg3 ," right "  : .$COUNT TYPE ;: MOVE-DISK LOCALS| via to from n | n 1 = IF CR ." Move disk from " from .$ ." to " to .$ELSE n 1- from via to RECURSE 1 from to via RECURSE n 1- via to from RECURSE THEN ; Without locals, executable pegs: : left ." left" ;: right ." right" ;: middle ." middle" ; : move-disk ( v t f n -- v t f ) dup 0= if drop exit then 1- >R rot swap [email protected] ( t v f n-1 ) recurse rot swap 2dup cr ." Move disk from " execute ." to " execute swap rot R> ( f t v n-1 ) recurse swap rot ;: hanoi ( n -- ) 1 max >R ['] right ['] middle ['] left R> move-disk drop drop drop ; ## Fortran Works with: Fortran version 90 and later PROGRAM TOWER CALL Move(4, 1, 2, 3) CONTAINS RECURSIVE SUBROUTINE Move(ndisks, from, to, via) INTEGER, INTENT (IN) :: ndisks, from, to, via IF (ndisks == 1) THEN WRITE(*, "(A,I1,A,I1)") "Move disk from pole ", from, " to pole ", to ELSE CALL Move(ndisks-1, from, via, to) CALL Move(1, from, to, via) CALL Move(ndisks-1, via, to, from) END IF END SUBROUTINE Move END PROGRAM TOWER  PROGRAM TOWER2 CALL Move(4, 1, 2, 3) CONTAINS RECURSIVE SUBROUTINE Move(ndisks, from, via, to) INTEGER, INTENT (IN) :: ndisks, from, via, to IF (ndisks > 1) THEN CALL Move(ndisks-1, from, to, via) WRITE(*, "(A,I1,A,I1,A,I1)") "Move disk ", ndisks, " from pole ", from, " to pole ", to Call Move(ndisks-1,via,from,to) ELSE WRITE(*, "(A,I1,A,I1,A,I1)") "Move disk ", ndisks, " from pole ", from, " to pole ", to END IF END SUBROUTINE Move END PROGRAM TOWER2  ## FreeBASIC ' FB 1.05.0 Win64 Sub move(n As Integer, from As Integer, to_ As Integer, via As Integer) If n > 0 Then move(n - 1, from, via, to_) Print "Move disk"; n; " from pole"; from; " to pole"; to_ move(n - 1, via, to_, from) End IfEnd Sub Print "Three disks" : Printmove 3, 1, 2, 3 Print Print "Four disks" : Printmove 4, 1, 2, 3Print "Press any key to quit"Sleep Output: Three disks Move disk 1 from pole 1 to pole 2 Move disk 2 from pole 1 to pole 3 Move disk 1 from pole 2 to pole 3 Move disk 3 from pole 1 to pole 2 Move disk 1 from pole 3 to pole 1 Move disk 2 from pole 3 to pole 2 Move disk 1 from pole 1 to pole 2 Four disks Move disk 1 from pole 1 to pole 3 Move disk 2 from pole 1 to pole 2 Move disk 1 from pole 3 to pole 2 Move disk 3 from pole 1 to pole 3 Move disk 1 from pole 2 to pole 1 Move disk 2 from pole 2 to pole 3 Move disk 1 from pole 1 to pole 3 Move disk 4 from pole 1 to pole 2 Move disk 1 from pole 3 to pole 2 Move disk 2 from pole 3 to pole 1 Move disk 1 from pole 2 to pole 1 Move disk 3 from pole 3 to pole 2 Move disk 1 from pole 1 to pole 3 Move disk 2 from pole 1 to pole 2 Move disk 1 from pole 3 to pole 2  ## Frink  /** Set up the recursive call for n disks */hanoi[n] := hanoi[n, 1, 3, 2] /** The recursive call. */hanoi[n, source, target, aux] :={ if n > 0 { hanoi[n-1, source, aux, target] println["Move from$source to $target"] hanoi[n-1, aux, target, source] }} hanoi  ## FutureBasic  include "ConsoleWindow" void local fn move( n as long, fromPeg as long, toPeg as long, viaPeg as long )if n > 0 fn move( n-1, fromPeg, viaPeg, toPeg ) print "Move disk from "; fromPeg; " to "; toPeg fn move( n-1, viaPeg, toPeg, fromPeg )end ifend fn fn move( 4, 1, 2, 3 )printprint "Towers of Hanoi puzzle solved."end  Output: 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 solved.  ## Fōrmulæ Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition. Programs in Fōrmulæ are created/edited online in its website, However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used. In this page you can see the program(s) related to this task and their results. ## GAP Hanoi := function(n) local move; move := function(n, a, b, c) # from, through, to if n = 1 then Print(a, " -> ", c, "\n"); else move(n - 1, a, c, b); move(1, a, b, c); move(n - 1, b, a, c); fi; end; move(n, "A", "B", "C");end; Hanoi(1);# A -> C Hanoi(2);# A -> B# A -> C# B -> C Hanoi(3);# A -> C# A -> B# C -> B# A -> C# B -> A# B -> C# A -> C ## Go package main import "fmt" // a towers of hanoi solver just has one method, playtype solver interface { play(int)} func main() { var t solver // declare variable of solver type t = new(towers) // type towers must satisfy solver interface t.play(4)} // towers is example of type satisfying solver interfacetype towers struct { // an empty struct. some other solver might fill this with some // data representation, maybe for algorithm validation, or maybe for // visualization.} // play is sole method required to implement solver typefunc (t *towers) play(n int) { // drive recursive solution, per task description t.moveN(n, 1, 2, 3)} // recursive algorithmfunc (t *towers) moveN(n, from, to, via int) { if n > 0 { t.moveN(n-1, from, via, to) t.move1(from, to) t.moveN(n-1, via, to, from) }} // example function prints actions to screen.// enhance with validation or visualization as needed.func (t *towers) move1(from, to int) { fmt.Println("move disk from rod", from, "to rod", to)} In other words: package main import "fmt" func main() { move(3, "A", "B", "C")} func move(n uint64, a, b, c string) { if n > 0 { move(n-1, a, c, b) fmt.Println("Move disk from " + a + " to " + c) move(n-1, b, a, c) }} ## Groovy Unlike most solutions here this solution manipulates more-or-less actual stacks of more-or-less actual rings. def tail = { list, n -> def m = list.size(); list.subList([m - n, 0].max(),m) } final STACK = [A:[],B:[],C:[]].asImmutable() def report = { it -> }def check = { it -> } def moveRing = { from, to -> to << from.pop(); report(); check(to) } def moveStackmoveStack = { from, to, using = STACK.values().find { !(it.is(from) || it.is(to)) } -> if (!from) return def n = from.size() moveStack(tail(from, n-1), using, to) moveRing(from, to) moveStack(tail(using, n-1), to, from)} Test program: enum Ring { S('°'), M('o'), L('O'), XL('( )'); private sym private Ring(sym) { this.sym=sym } String toString() { sym }} report = { STACK.each { k, v -> println "${k}: ${v}" }; println() }check = { to -> assert to == ([] + to).sort().reverse() } Ring.values().reverseEach { STACK.A << it }report()check(STACK.A)moveStack(STACK.A, STACK.C) Output: A: [( ), O, o, °] B: [] C: [] A: [( ), O, o] B: [°] C: [] A: [( ), O] B: [°] C: [o] A: [( ), O] B: [] C: [o, °] A: [( )] B: [O] C: [o, °] A: [( ), °] B: [O] C: [o] A: [( ), °] B: [O, o] C: [] A: [( )] B: [O, o, °] C: [] A: [] B: [O, o, °] C: [( )] A: [] B: [O, o] C: [( ), °] A: [o] B: [O] C: [( ), °] A: [o, °] B: [O] C: [( )] A: [o, °] B: [] C: [( ), O] A: [o] B: [°] C: [( ), O] A: [] B: [°] C: [( ), O, o] A: [] B: [] C: [( ), O, o, °] ## Haskell Most of the programs on this page use an imperative approach (i.e., print out movements as side effects during program execution). Haskell favors a purely functional approach, where you would for example return a (lazy) list of movements from a to b via c: hanoi :: Integer -> a -> a -> a -> [(a, a)]hanoi 0 _ _ _ = []hanoi n a b c = hanoi (n-1) a c b ++ [(a,b)] ++ hanoi (n-1) c b a You can also do the above with one tail-recursion call: hanoi :: Integer -> a -> a -> a -> [(a, a)] hanoi n a b c = hanoiToList n a b c [] where hanoiToList 0 _ _ _ l = l hanoiToList n a b c l = hanoiToList (n-1) a c b ((a, b) : hanoiToList (n-1) c b a l) One can use this function to produce output, just like the other programs: hanoiIO n = mapM_ f$ hanoi n 1 2 3 where  f (x,y) = putStrLn $"Move " ++ show x ++ " to " ++ show y or, instead, one can of course also program imperatively, using the IO monad directly: hanoiM :: Integer -> IO ()hanoiM n = hanoiM' n 1 2 3 where hanoiM' 0 _ _ _ = return () hanoiM' n a b c = do hanoiM' (n-1) a c b putStrLn$ "Move " ++ show a ++ " to " ++ show b    hanoiM' (n-1) c b a

or, defining it as a monoid, and adding some output:

-------------------------- HANOI ------------------------- hanoi ::  Int ->  String ->  String ->  String ->  [(String, String)]hanoi 0 _ _ _ = memptyhanoi n l r m =  hanoi (n - 1) l m r    <> [(l, r)]    <> hanoi (n - 1) m r l --------------------------- TEST -------------------------main :: IO ()main = putStrLn $showHanoi 5 ------------------------- DISPLAY ------------------------showHanoi :: Int -> StringshowHanoi n = unlines$    fmap      ( \(from, to) ->          concat [justifyRight 5 ' ' from, " -> ", to]      )      (hanoi n "left" "right" "mid") justifyRight :: Int -> Char -> String -> StringjustifyRight n c = (drop . length) <*> (replicate n c <>)
Output:
 left -> right
left -> mid
right -> mid
left -> right
mid -> left
mid -> right
left -> right
left -> mid
right -> mid
right -> left
mid -> left
right -> mid
left -> right
left -> mid
right -> mid
left -> right
mid -> left
mid -> right
left -> right
mid -> left
right -> mid
right -> left
mid -> left
mid -> right
left -> right
left -> mid
right -> mid
left -> right
mid -> left
mid -> right
left -> right

## HolyC

Translation of: C
U0 Move(U8 n, U8 from, U8 to, U8 via) {  if (n > 0) {    Move(n - 1, from, via, to);    Print("Move disk from pole %d to pole %d\n", from, to);    Move(n - 1, via, to, from);  }} Move(4, 1, 2, 3);

## Icon and Unicon

The following is based on a solution in the Unicon book.

procedure main(arglist)hanoi(arglist) | stop("Usage: hanoi n\n\rWhere n is the number of disks to move.")end #procedure hanoi(n:integer, needle1:1, needle2:2)   # unicon shorthand for icon code 1,2,3 below procedure hanoi(n, needle1, needle2)   #: solve towers of hanoi by moving  n disks from needle 1 to needle2 via otherlocal other n := integer(0 < n) | runerr(n,101)       # 1 ensure integer (this also ensures it's positive too)/needle1 := 1                             # 2 default/needle2 := 2                             # 3 default if n = 1 then   write("Move disk from ", needle1, " to ", needle2)else {   other := 6 - needle1 - needle2         # clever but somewhat un-iconish way to find other   hanoi(n-1, needle1, other)                write("Move disk from ", needle1, " to ", needle2)   hanoi(n-1, other, needle2)            }returnend

## Inform 7

Hanoi is a room. A post is a kind of supporter. A post is always fixed in place. The left post, the middle post, and the right post are posts in Hanoi. A disk is a kind of supporter.The red disk is a disk on the left post.The orange disk is a disk on the red disk.The yellow disk is a disk on the orange disk.The green disk is a disk on the yellow disk. Definition: a disk is topmost if nothing is on it. When play begins:	move 4 disks from the left post to the right post via the middle post. To move (N - number) disk/disks from (FP - post) to (TP - post) via (VP - post):	if N > 0:		move N - 1 disks from FP to VP via TP;		say "Moving a disk from [FP] to [TP]...";		let D be a random topmost disk enclosed by FP;		if a topmost disk (called TD) is enclosed by TP, now D is on TD;		otherwise now D is on TP;		move N - 1 disks from VP to TP via FP.

## Io

hanoi := method(n, from, to, via,  if (n == 1) then (    writeln("Move from ", from, " to ", to)  ) else (    hanoi(n - 1, from, via, to  )    hanoi(1    , from, to , via )    hanoi(n - 1, via , to , from)  ))

## Ioke

 = method(n, f, u, t,  if(n < 2,    "#{f} --> #{t}" println,     H(n - 1, f, t, u)    "#{f} --> #{t}" println    H(n - 1, u, f, t)  )) hanoi = method(n,  H(n, 1, 2, 3))

## IS-BASIC

100 PROGRAM "Hanoi.bas"110 CALL HANOI(4,1,3,2)120 DEF HANOI(DISK,FRO,TO,WITH)130   IF DISK>0 THEN140     CALL HANOI(DISK-1,FRO,WITH,TO)150     PRINT "Move disk";DISK;"from";FRO;"to";TO160     CALL HANOI(DISK-1,WITH,TO,FRO)170   END IF180 END DEF

## J

Solutions

H =: [email protected],&2  (({&0 2 1,0 2,{&1 0 2)@$:@<:) @. * NB. tacit using anonymous recursion Example use:  H 30 20 12 10 21 21 02 0 The result is a 2-column table; a row i,j is interpreted as: move a disk (the top disk) from peg i to peg j . Or, using explicit rather than implicit code: H1=: monad define NB. explicit equivalent of H if. y do. ({&0 2 1 , 0 2 , {&1 0 2) H1 y-1 else. i.0 2 end.) The usage here is the same:  H1 2 0 1 0 2 1 2 Alternative solution If a textual display is desired, similar to some of the other solutions here (counting from 1 instead of 0, tracking which disk is on the top of the stack, and of course formatting the result for a human reader instead of providing a numeric result): hanoi=: monad define moves=. H y disks=.$~ ((],[,]) $:@<:) @.* y ('move disk ';' from peg ';' to peg ');@,."1 ":&.>disks,.1+moves) Demonstration:  hanoi 3move disk 1 from peg 1 to peg 3move disk 2 from peg 1 to peg 2move disk 1 from peg 3 to peg 2move disk 3 from peg 1 to peg 3move disk 1 from peg 2 to peg 1move disk 2 from peg 2 to peg 3move disk 1 from peg 1 to peg 3 ## Java public void move(int n, int from, int to, int via) { if (n == 1) { System.out.println("Move disk from pole " + from + " to pole " + to); } else { move(n - 1, from, via, to); move(1, from, to, via); move(n - 1, via, to, from); }} ## JavaScript ### ES5 function move(n, a, b, c) { if (n > 0) { move(n-1, a, c, b); console.log("Move disk from " + a + " to " + c); move(n-1, b, a, c); }}move(4, "A", "B", "C"); Or, as a functional expression, rather than a statement with side effects: (function () { // hanoi :: Int -> String -> String -> String -> [[String, String]] function hanoi(n, a, b, c) { return n ? hanoi(n - 1, a, c, b) .concat([ [a, b] ]) .concat(hanoi(n - 1, c, b, a)) : []; } return hanoi(3, 'left', 'right', 'mid') .map(function (d) { return d + ' -> ' + d; });})(); Output: ["left -> right", "left -> mid", "right -> mid", "left -> right", "mid -> left", "mid -> right", "left -> right"] ### ES6 (() => { 'use strict'; // hanoi :: Int -> String -> String -> String -> [[String, String]] const hanoi = (n, a, b, c) => n ? hanoi(n - 1, a, c, b) .concat([ [a, b] ]) .concat(hanoi(n - 1, c, b, a)) : []; // show :: a -> String const show = x => JSON.stringify(x, null, 2); return show( hanoi(3, 'left', 'right', 'mid') .map(d => d + ' -> ' + d) );})(); Output: [ "left -> right", "left -> mid", "right -> mid", "left -> right", "mid -> left", "mid -> right", "left -> right" ] ## Joy From here DEFINE hanoi == [[rolldown] infra] dip [ [ [null] [pop pop] ] [ [dup2 [[rotate] infra] dip pred] [ [dup rest put] dip [[swap] infra] dip pred ] [] ] ] condnestrec. Using it (5 is the number of disks.) [source destination temp] 5 hanoi. ## jq Works with: jq version 1.4 The algorithm used here is used elsewhere on this page but it is worthwhile pointing out that it can also be read as a proof that: (a) move(n;"A";"B";"C") will logically succeed for n>=0; and (b) move(n;"A";"B";"C") will generate the sequence of moves, assuming sufficient computing resources. The proof of (a) is by induction: • As explained in the comments, the algorithm establishes that move(n;x;y;z) is possible for all n>=0 and distinct x,y,z if move(n-1;x;y;z)) is possible; • Since move(0;x;y;z) evidently succeeds, (a) is established by induction. The truth of (b) follows from the fact that the algorithm emits an instruction of what to do when moving a single disk. # n is the number of disks to move from From to Todef move(n; From; To; Via): if n > 0 then # move all but the largest at From to Via (according to the rules): move(n-1; From; Via; To), # ... so the largest disk at From is now free to move to its final destination: "Move disk from \(From) to \(To)", # Move the remaining disks at Via to To: move(n-1; Via; To; From) else empty end; Example: move(5; "A"; "B"; "C")  ## Jsish From Javascript ES5 entry. /* Towers of Hanoi, in Jsish */ function move(n, a, b, c) { if (n > 0) { move(n-1, a, c, b); puts("Move disk from " + a + " to " + c); move(n-1, b, a, c); }} if (Interp.conf('unitTest')) move(4, "A", "B", "C"); /*=!EXPECTSTART!=Move disk from A to BMove disk from A to CMove disk from B to CMove disk from A to BMove disk from C to AMove disk from C to BMove disk from A to BMove disk from A to CMove disk from B to CMove disk from B to AMove disk from C to AMove disk from B to CMove disk from A to BMove disk from A to CMove disk from B to C=!EXPECTEND!=*/ Output: prompt$ jsish -u towersOfHanoi.jsi
[PASS] towersOfHanoi.jsi

## Julia

Translation of: R
 function solve(n::Integer, from::Integer, to::Integer, via::Integer)  if n == 1    println("Move disk from $from to$to")  else    solve(n - 1, from, via, to)    solve(1, from, to, via)    solve(n - 1, via, to, from)  endend solve(4, 1, 2, 3)
Output:
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


## K

   h:{[n;a;b;c]if[n>0;_f[n-1;a;c;b];0:,//$($n,":",$a,"->",$b,"\n");_f[n-1;c;b;a]]}   h[4;1;2;3]1:1->32:1->21:3->23:1->31:2->12:2->31:1->34:1->21:3->22:3->11:2->13:3->21:1->32:1->21:3->2

The disk to move in the i'th step is the same as the position of the leftmost 1 in the binary representation of 1..2^n.

   s:();{[n;a;b;c]if[n>0;_f[n-1;a;c;b];s,:n;_f[n-1;c;b;a]]}[4;1;2;3];s1 2 1 3 1 2 1 4 1 2 1 3 1 2 1    1_{*1+&|x}'a:(2_vs!_2^4)1 2 1 3 1 2 1 4 1 2 1 3 1 2 1

## Klingphix

Translation of: MiniScript
include ..\Utilitys.tlhy :moveDisc %B !B %C !C %A !A %n !n { n A C B }    $n [$n 1 - $A$B $C moveDisc ( "Move disc "$n " from pole " $A " to pole "$C ) lprint nl        $n 1 -$B $C$A moveDisc    ] if; { Move disc 3 from pole 1 to pole 3, with pole 2 as spare }3 1 3 2 moveDisc " " input
Output:
Move disc 1 from pole 1 to pole 3
Move disc 2 from pole 1 to pole 2
Move disc 1 from pole 3 to pole 2
Move disc 3 from pole 1 to pole 3
Move disc 1 from pole 2 to pole 1
Move disc 2 from pole 2 to pole 3
Move disc 1 from pole 1 to pole 3

// version 1.1.0 class Hanoi(disks: Int) {    private var moves = 0     init {        println("Towers of Hanoi with $disks disks:\n") move(disks, 'L', 'C', 'R') println("\nCompleted in$moves moves\n")    }     private fun move(n: Int, from: Char, to: Char, via: Char) {        if (n > 0) {            move(n - 1, from, via, to)            moves++            println("Move disk $n from$from to $to") move(n - 1, via, to, from) } }} fun main(args: Array<String>) { Hanoi(3) Hanoi(4)} Output: Towers of Hanoi with 3 disks: Move disk 1 from L to C Move disk 2 from L to R Move disk 1 from C to R Move disk 3 from L to C Move disk 1 from R to L Move disk 2 from R to C Move disk 1 from L to C Completed in 7 moves Towers of Hanoi with 4 disks: Move disk 1 from L to R Move disk 2 from L to C Move disk 1 from R to C Move disk 3 from L to R Move disk 1 from C to L Move disk 2 from C to R Move disk 1 from L to R Move disk 4 from L to C Move disk 1 from R to C Move disk 2 from R to L Move disk 1 from C to L Move disk 3 from R to C Move disk 1 from L to R Move disk 2 from L to C Move disk 1 from R to C Completed in 15 moves  ## lambdatalk (Following NewLisp, PicoLisp, Racket, Scheme)  {def move {lambda {:n :from :to :via} {if {<= :n 0} then > else {move {- :n 1} :from :via :to} move disk :n from :from to :to {br} {move {- :n 1} :via :to :from} }}}-> move{move 4 A B C}> move disk 1 from A to C> move disk 2 from A to B> move disk 1 from C to B> move disk 3 from A to C> move disk 1 from B to A> move disk 2 from B to C> move disk 1 from A to C> move disk 4 from A to B> move disk 1 from C to B> move disk 2 from C to A> move disk 1 from B to A> move disk 3 from C to B> move disk 1 from A to C> move disk 2 from A to B> move disk 1 from C to B  ## Lasso #!/usr/bin/lasso9 define towermove( disks::integer, a,b,c) => { if(#disks > 0) => { towermove(#disks - 1, #a, #c, #b ) stdoutnl("Move disk from " + #a + " to " + #c) towermove(#disks - 1, #b, #a, #c ) }} towermove((integer($argv -> second || 3)), "A", "B", "C")

Called from command line:

./towers
Output:
Move disk from A to C
Move disk from A to B
Move disk from C to B
Move disk from A to C
Move disk from B to A
Move disk from B to C
Move disk from A to C

Called from command line:

./towers 4
Output:
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

## Liberty BASIC

This looks much better with a GUI interface.

## MoonScript

hanoi = (n, src, dest, via) ->  if n > 1    hanoi n-1, src, via, dest  print "#{src} -> #{dest}"  if n > 1    hanoi n-1, via, dest, src hanoi 4,1,3,2
Output:
1 -> 2
1 -> 3
2 -> 3
1 -> 2
3 -> 1
3 -> 2
1 -> 2
1 -> 3
2 -> 3
2 -> 1
3 -> 1
2 -> 3
1 -> 2
1 -> 3
2 -> 3

## Nemerle

using System; using System.Console; module Towers{    Hanoi(n : int, from = 1, to = 3, via = 2) : void    {        when (n > 0)        {            Hanoi(n - 1, from, via, to);            WriteLine("Move disk from peg {0} to peg {1}", from, to);            Hanoi(n - 1, via, to, from);        }    }     Main() : void    {        Hanoi(4)    } }

## NetRexx

/* NetRexx */options replace format comments java crossref symbols binary runSample(arg)return -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~method runSample(arg) private static  parse arg discs .  if discs = '', discs < 1 then discs = 4  say 'Minimum moves to solution:' 2 ** discs - 1  moves = move(discs)  say 'Solved in' moves 'moves.'  return -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~method move(discs = int 4, towerFrom = int 1, towerTo = int 2, towerVia = int 3, moves = int 0) public static  if discs == 1 then do    moves = moves + 1    say 'Move disc from peg' towerFrom 'to peg' towerTo '- Move No:' Rexx(moves).right(5)    end  else do    moves = move(discs - 1, towerFrom, towerVia, towerTo, moves)    moves = move(1, towerFrom, towerTo, towerVia, moves)    moves = move(discs - 1, towerVia, towerTo, towerFrom, moves)    end  return moves 
Output:
Minimum moves to solution: 15
Move disc from peg 1 to peg 3 - Move No:     1
Move disc from peg 1 to peg 2 - Move No:     2
Move disc from peg 3 to peg 2 - Move No:     3
Move disc from peg 1 to peg 3 - Move No:     4
Move disc from peg 2 to peg 1 - Move No:     5
Move disc from peg 2 to peg 3 - Move No:     6
Move disc from peg 1 to peg 3 - Move No:     7
Move disc from peg 1 to peg 2 - Move No:     8
Move disc from peg 3 to peg 2 - Move No:     9
Move disc from peg 3 to peg 1 - Move No:    10
Move disc from peg 2 to peg 1 - Move No:    11
Move disc from peg 3 to peg 2 - Move No:    12
Move disc from peg 1 to peg 3 - Move No:    13
Move disc from peg 1 to peg 2 - Move No:    14
Move disc from peg 3 to peg 2 - Move No:    15
Solved in 15 moves.


## NewLISP

(define (move n from to via)			(if (> n 0) 				(move (- n 1) from via to				(print "move disk from pole " from " to pole " to "\n")				(move (- n 1) via to from)))) (move 4 1 2 3)

## Nim

proc hanoi(disks: int; fromTower, toTower, viaTower: string) =  if disks != 0:    hanoi(disks - 1, fromTower, viaTower, toTower)    echo("Move disk ", disks, " from ", fromTower, " to ", toTower)    hanoi(disks - 1, viaTower, toTower, fromTower) hanoi(4, "1", "2", "3")
Output:
Move disk 1 from 1 to 3
Move disk 2 from 1 to 2
Move disk 1 from 3 to 2
Move disk 3 from 1 to 3
Move disk 1 from 2 to 1
Move disk 2 from 2 to 3
Move disk 1 from 1 to 3
Move disk 4 from 1 to 2
Move disk 1 from 3 to 2
Move disk 2 from 3 to 1
Move disk 1 from 2 to 1
Move disk 3 from 3 to 2
Move disk 1 from 1 to 3
Move disk 2 from 1 to 2
Move disk 1 from 3 to 2

## Objeck

class Hanoi {  function : Main(args : String[]) ~ Nil {    Move(4, 1, 2, 3);  }   function: Move(n:Int, f:Int, t:Int, v:Int) ~ Nil {    if(n = 1) {      "Move disk from pole {$f} to pole {$t}"->PrintLine();    }    else {      Move(n - 1, f, v, t);      Move(1, f, t, v);      Move(n - 1, v, t, f);    };  }}

## Objective-C

From here

Works with: GNUstep

It should be compatible with XCode/Cocoa on MacOS too.

The Interface - TowersOfHanoi.h:

#import <Foundation/NSObject.h> @interface TowersOfHanoi: NSObject {	int pegFrom;	int pegTo;	int pegVia;	int numDisks;} -(void) setPegFrom: (int) from andSetPegTo: (int) to andSetPegVia: (int) via andSetNumDisks: (int) disks;-(void) movePegFrom: (int) from andMovePegTo: (int) to andMovePegVia: (int) via andWithNumDisks: (int) disks;@end

The Implementation - TowersOfHanoi.m:

#import "TowersOfHanoi.h"@implementation TowersOfHanoi -(void) setPegFrom: (int) from andSetPegTo: (int) to andSetPegVia: (int) via andSetNumDisks: (int) disks {	pegFrom = from;	pegTo = to;	pegVia = via;	numDisks = disks;} -(void) movePegFrom: (int) from andMovePegTo: (int) to andMovePegVia: (int) via andWithNumDisks: (int) disks {	if (disks == 1) {            printf("Move disk from pole %i to pole %i\n", from, to);        } else { 			[self movePegFrom: from andMovePegTo: via andMovePegVia: to andWithNumDisks: disks-1];			[self movePegFrom: from andMovePegTo: to andMovePegVia: via andWithNumDisks: 1];			[self movePegFrom: via andMovePegTo: to andMovePegVia: from andWithNumDisks: disks-1];        }} @end

Test code: TowersTest.m:

#import <stdio.h>#import "TowersOfHanoi.h" int main( int argc, const char *argv[] ) {	@autoreleasepool { 		TowersOfHanoi *tower = [[TowersOfHanoi alloc] init]; 		int from = 1;		int to = 3;		int via = 2;		int disks = 3; 		[tower setPegFrom: from andSetPegTo: to andSetPegVia: via andSetNumDisks: disks]; 		[tower movePegFrom: from andMovePegTo: to andMovePegVia: via andWithNumDisks: disks]; 	}	return 0;}

## OCaml

let rec hanoi n a b c =  if n <> 0 then begin    hanoi (pred n) a c b;    Printf.printf "Move disk from pole %d to pole %d\n" a b;    hanoi (pred n) c b a  end let () =  hanoi 4 1 2 3

## Octave

function hanoimove(ndisks, from, to, via)  if ( ndisks == 1 )    printf("Move disk from pole %d to pole %d\n", from, to);  else    hanoimove(ndisks-1, from, via, to);    hanoimove(1, from, to, via);    hanoimove(ndisks-1, via, to, from);  endifendfunction hanoimove(4, 1, 2, 3);

## Rascal

Translation of: Python
public void hanoi(ndisks, startPeg, endPeg){	if(ndisks>0){		hanoi(ndisks-1, startPeg, 6 - startPeg - endPeg);		println("Move disk <ndisks> from peg <startPeg> to peg <endPeg>");		hanoi(ndisks-1, 6 - startPeg - endPeg, endPeg);	}}
Output:
rascal>hanoi(4,1,3)Move disk 1 from peg 1 to peg 2Move disk 2 from peg 1 to peg 3Move disk 1 from peg 2 to peg 3Move disk 3 from peg 1 to peg 2Move disk 1 from peg 3 to peg 1Move disk 2 from peg 3 to peg 2Move disk 1 from peg 1 to peg 2Move disk 4 from peg 1 to peg 3Move disk 1 from peg 2 to peg 3Move disk 2 from peg 2 to peg 1Move disk 1 from peg 3 to peg 1Move disk 3 from peg 2 to peg 3Move disk 1 from peg 1 to peg 2Move disk 2 from peg 1 to peg 3Move disk 1 from peg 2 to peg 3ok

## Raven

Translation of: Python
define hanoi use ndisks, startpeg, endpeg   ndisks 0 > if      6 startpeg - endpeg - startpeg ndisks 1 - hanoi      endpeg startpeg ndisks "Move disk %d from peg %d to peg %d\n" print       endpeg 6 startpeg - endpeg - ndisks 1 - hanoi define dohanoi use ndisks   # startpeg=1, endpeg=3   3 1 ndisks hanoi # 4 disks4 dohanoi 
Output:
raven hanoi.rv
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


## REBOL

rebol [	Title: "Towers of Hanoi"	URL: http://rosettacode.org/wiki/Towers_of_Hanoi] hanoi: func [	{Begin moving the golden disks from one pole to the next.	 Note: when last disk moved, the world will end.}	disks [integer!] "Number of discs on starting pole."	/poles "Name poles."	from to via][    if disks = 0 [return]	if not poles [from: 'left  to: 'middle  via: 'right]     hanoi/poles disks - 1 from via to	print [from "->" to]    hanoi/poles disks - 1 via to from] hanoi 4
Output:
left -> right
left -> middle
right -> middle
left -> right
middle -> left
middle -> right
left -> right
left -> middle
right -> middle
right -> left
middle -> left
right -> middle
left -> right
left -> middle
right -> middle

## Retro

~~~{ 'Num 'From 'To 'Via } [ var ] a:for-each  :set     !Via !To !From !Num ; :display @To @From 'Move_a_ring_from_%n_to_%n\n s:format s:put ;  :hanoi (num,from,to,via-)   set @Num n:-zero?   [ @Num @From @To @Via     @Num n:dec @From @Via @To hanoi set display     @Num n:dec @Via @To @From hanoi ] if ;  #3 #1 #3 #2 hanoi nl ~~~

## REXX

### simple text moves

/*REXX program  displays  the  moves  to solve  the  Tower of Hanoi  (with  N  disks).  */parse arg N .                                    /*get optional number of disks from CL.*/if N=='' | N==","  then N=3                      /*Not specified?  Then use the default.*/#= 0                                             /*#:  the number of disk moves (so far)*/z= 2**N  -  1                                    /*Z:   "     "    " minimum # of moves.*/call mov  1, 3, N                                /*move the top disk,  then recurse ··· */say                                              /* [↓]  Display the minimum # of moves.*/say 'The minimum number of moves to solve a '      N"─disk  Tower of Hanoi is "     zexit                                             /*stick a fork in it,  we're all done. *//*──────────────────────────────────────────────────────────────────────────────────────*/mov: procedure expose # z; parse arg  @1,@2,@3;          L= length(z)     if @3==1  then do;  #= # + 1                /*bump the (disk) move counter by one. */                         say 'step'   right(#, L)":  move disk on tower"   @1  '───►'   @2                    end               else do;  call mov @1,               6 [email protected] [email protected],         @3 -1                         call mov @1,               @2,                  1                         call mov 6 - @1 - @2,      @2,                @3 -1                    end     return                                      /* [↑]  this subroutine uses recursion.*/
output   when using the default input:
step 1:  move disk on tower 1 ───► 3
step 2:  move disk on tower 1 ───► 2
step 3:  move disk on tower 3 ───► 2
step 4:  move disk on tower 1 ───► 3
step 5:  move disk on tower 2 ───► 1
step 6:  move disk on tower 2 ───► 3
step 7:  move disk on tower 1 ───► 3

The minimum number of moves to solve a  3-disk  Tower of Hanoi is  7


output   when the following was entered (to solve with four disks):   4

step  1:  move disk on tower 1 ───► 2
step  2:  move disk on tower 1 ───► 3
step  3:  move disk on tower 2 ───► 3
step  4:  move disk on tower 1 ───► 2
step  5:  move disk on tower 3 ───► 1
step  6:  move disk on tower 3 ───► 2
step  7:  move disk on tower 1 ───► 2
step  8:  move disk on tower 1 ───► 3
step  9:  move disk on tower 2 ───► 3
step 10:  move disk on tower 2 ───► 1
step 11:  move disk on tower 3 ───► 1
step 12:  move disk on tower 2 ───► 3
step 13:  move disk on tower 1 ───► 2
step 14:  move disk on tower 1 ───► 3
step 15:  move disk on tower 2 ───► 3

The minimum number of moves to solve a  4-disk  Tower of Hanoi is  15


### pictorial moves

This REXX version pictorially shows   (via ASCII art)   the moves for solving the Town of Hanoi.

Quite a bit of code has been dedicated to showing a "picture" of the towers with the disks, and the movement of the disk (for each move).   "Coloring" of the disks is attempted with dithering.

In addition, it shows each move in a countdown manner (the last move is marked as #1).

It may not be obvious from the pictorial display of the moves, but whenever a disk is moved from one tower to another, it is always the top disk that is moved   (to the target tower).

Also, since the pictorial showing of the moves may be voluminous (especially for a larger number of disks), the move counter is started with the maximum and is the count shown is decremented so the viewer can see how many moves are left to display.

/*REXX program  displays  the  moves  to solve  the  Tower of Hanoi  (with  N  disks).  */parse arg N .                                    /*get optional number of disks from CL.*/if N=='' | N==","  then N=3                      /*Not specified?  Then use the default.*/sw= 80;    wp= sw%3 - 1;   blanks= left('', wp)  /*define some default REXX variables.  */c.1= sw % 3 % 2                                  /* [↑]  SW: assume default Screen Width*/c.2= sw % 2 - 1                                  /* ◄───  C.1 C.2 C.2  are the positions*/c.3= sw - 2 - c.1                                /*                    of the 3 columns.*/#= 0;        z= 2**N - 1;           moveK= z     /*#moves; min# of moves; where to move.*/@abc= 'abcdefghijklmnopqrstuvwxyN'               /*dithering chars when many disks used.*/ebcdic= ('f2'x==2)                               /*determine if EBCDIC or ASCII machine.*/ if ebcdic then do;   bar= 'bf'x;    ar= "df"x;    dither= 'db9f9caf'x;         down= "9a"x                      tr= 'bc'x;    bl= "ab"x;    br= 'bb'x;   vert= "fa"x;      tl= 'ac'x               end          else do;   bar= 'c4'x;    ar= "10"x;    dither= 'b0b1b2db'x;         down= "19"x                      tr= 'bf'x;    bl= "c0"x;    br= 'd9'x;   vert= "b3"x;      tl= 'da'x               end verts= vert || vert;           Tcorners= tl || tr;              box     = left(dither, 1)downs= down || down;           Bcorners= bl || br;              boxChars= dither || @abc$.= 0;$.1= N;         k= N;                            kk= k + k   do j=1  for N;   @.3.j= blanks;    @.2.j= blanks;    @.1.j= center( copies(box, kk), wp)  if N<=length(boxChars)  then @.1.j= translate( @.1.j, , substr( boxChars, kk%2, 1), box)  kk= kk - 2  end   /*j*/                                    /*populate the tower of Hanoi spindles.*/ call showTowers;   call mov 1,3,N;   saysay 'The minimum number of moves to solve a '        N"-disk  Tower of Hanoi is "      zexit                                             /*stick a fork in it,  we're all done. *//*──────────────────────────────────────────────────────────────────────────────────────*/dsk: parse arg from dest;   #= # + 1;       pp=     if from==1  then do;  pp= overlay(bl,  pp, c.1)                           pp= overlay(bar, pp, c.1+1, c.dest-c.1-1, bar) || tr                      end     if from==2  then do                      if dest==1  then do;  pp= overlay(tl,  pp, c.1)                                            pp= overlay(bar, pp, c.1+1, c.2-c.1-1,bar)||br                                       end                      if dest==3  then do;  pp= overlay(bl,  pp, c.2)                                            pp= overlay(bar, pp, c.2+1, c.3-c.2-1,bar)||tr                                       end                      end     if from==3  then do;  pp= overlay(br,  pp, c.3)                           pp= overlay(bar, pp, c.dest+1, c.3-c.dest-1, bar)                           pp= overlay(tl,  pp, c.dest)                      end     say translate(pp, downs, Bcorners || Tcorners || bar);     say overlay(moveK, pp, 1)     say translate(pp, verts, Tcorners || Bcorners || bar)     say translate(pp, downs, Tcorners || Bcorners || bar);     moveK= moveK - 1     $.from=$.from - 1;      $.dest=$.dest + 1;     _f= $.from + 1; _t=$.dest     @.dest._t= @.from._f;    @.from._f= blanks;      call showTowers     return/*──────────────────────────────────────────────────────────────────────────────────────*/mov: if arg(3)==1  then      call dsk arg(1) arg(2)                   else do;  call mov arg(1),              6 -arg(1) -arg(2),    arg(3) -1                             call mov arg(1),              arg(2),               1                             call mov 6 -arg(1) -arg(2),   arg(2),               arg(3) -1                        end                 /* [↑]  The  MOV  subroutine is recursive,  */     return                                 /*it uses no variables, is uses BIFs instead*//*──────────────────────────────────────────────────────────────────────────────────────*/showTowers: do j=N  by -1  for N; [email protected].1.j @.2.j @.3.j;  if _\=''  then say _; end;  return
output   when using the default input:
           ░░
▒▒▒▒
▓▓▓▓▓▓
↓
7           └───────────────────────────────────────────────────┐
│
↓
▒▒▒▒
▓▓▓▓▓▓                                                ░░
↓
6           └─────────────────────────┐
│
↓
▓▓▓▓▓▓                     ▒▒▒▒                       ░░
↓
5                                     ┌─────────────────────────┘
│
↓
░░
▓▓▓▓▓▓                     ▒▒▒▒
↓
4           └───────────────────────────────────────────────────┐
│
↓
░░
▒▒▒▒                     ▓▓▓▓▓▓
↓
3           ┌─────────────────────────┘
│
↓
░░                       ▒▒▒▒                     ▓▓▓▓▓▓
↓
2                                     └─────────────────────────┐
│
↓
▒▒▒▒
░░                                                ▓▓▓▓▓▓
↓
1           └───────────────────────────────────────────────────┐
│
↓
░░
▒▒▒▒
▓▓▓▓▓▓

The minimum number of moves to solve a  3-disk  Tower of Hanoi is  7


## Ring

 move(4, 1, 2, 3) func move n, src, dst, via     if n > 0 move(n - 1, src, via, dst)        see "" + src + " to " + dst + nl        move(n - 1, via, dst, src) ok 

## Ruby

### version 1

def move(num_disks, start=0, target=1, using=2)  if num_disks == 1   @towers[target] << @towers[start].pop    puts "Move disk from #{start} to #{target} : #{@towers}"  else    move(num_disks-1, start, using, target)    move(1,           start, target, using)    move(num_disks-1, using, target, start)  end end n = 5@towers = [[*1..n].reverse, [], []]move(n)
Output:
Move disk from 0 to 1 : [[5, 4, 3, 2], , []]
Move disk from 0 to 2 : [[5, 4, 3], , ]
Move disk from 1 to 2 : [[5, 4, 3], [], [2, 1]]
Move disk from 0 to 1 : [[5, 4], , [2, 1]]
Move disk from 2 to 0 : [[5, 4, 1], , ]
Move disk from 2 to 1 : [[5, 4, 1], [3, 2], []]
Move disk from 0 to 1 : [[5, 4], [3, 2, 1], []]
Move disk from 0 to 2 : [, [3, 2, 1], ]
Move disk from 1 to 2 : [, [3, 2], [4, 1]]
Move disk from 1 to 0 : [[5, 2], , [4, 1]]
Move disk from 2 to 0 : [[5, 2, 1], , ]
Move disk from 1 to 2 : [[5, 2, 1], [], [4, 3]]
Move disk from 0 to 1 : [[5, 2], , [4, 3]]
Move disk from 0 to 2 : [, , [4, 3, 2]]
Move disk from 1 to 2 : [, [], [4, 3, 2, 1]]
Move disk from 0 to 1 : [[], , [4, 3, 2, 1]]
Move disk from 2 to 0 : [, , [4, 3, 2]]
Move disk from 2 to 1 : [, [5, 2], [4, 3]]
Move disk from 0 to 1 : [[], [5, 2, 1], [4, 3]]
Move disk from 2 to 0 : [, [5, 2, 1], ]
Move disk from 1 to 2 : [, [5, 2], [4, 1]]
Move disk from 1 to 0 : [[3, 2], , [4, 1]]
Move disk from 2 to 0 : [[3, 2, 1], , ]
Move disk from 2 to 1 : [[3, 2, 1], [5, 4], []]
Move disk from 0 to 1 : [[3, 2], [5, 4, 1], []]
Move disk from 0 to 2 : [, [5, 4, 1], ]
Move disk from 1 to 2 : [, [5, 4], [2, 1]]
Move disk from 0 to 1 : [[], [5, 4, 3], [2, 1]]
Move disk from 2 to 0 : [, [5, 4, 3], ]
Move disk from 2 to 1 : [, [5, 4, 3, 2], []]
Move disk from 0 to 1 : [[], [5, 4, 3, 2, 1], []]


### version 2

# solve(source, via, target)# Example:# solve([5, 4, 3, 2, 1], [], [])# Note this will also solve randomly placed disks,# "place all disk in target with legal moves only".def solve(*towers)  # total number of disks  disks = towers.inject(0){|sum, tower| sum+tower.length}  x=0 # sequence number  p towers # initial trace  # have we solved the puzzle yet?  while towers.last.length < disks do    x+=1 # assume the next step    from = (x&x-1)%3    to = ((x|(x-1))+1)%3    # can we actually take from tower?    if top = towers[from].last      bottom = towers[to].last      # is the move legal?      if !bottom || bottom > top        # ok, do it!        towers[to].push(towers[from].pop)        p towers # trace      end    end  endend solve([5, 4, 3, 2, 1], [], [])
Output:
[[5, 4, 3, 2, 1], [], []]
[[5, 4, 3, 2], [], ]
[[5, 4, 3], , ]
[[5, 4, 3], [2, 1], []]
[[5, 4], [2, 1], ]
[[5, 4, 1], , ]
[[5, 4, 1], [], [3, 2]]
[[5, 4], [], [3, 2, 1]]
[, , [3, 2, 1]]
[, [4, 1], [3, 2]]
[[5, 2], [4, 1], ]
[[5, 2, 1], , ]
[[5, 2, 1], [4, 3], []]
[[5, 2], [4, 3], ]
[, [4, 3, 2], ]
[, [4, 3, 2, 1], []]
[[], [4, 3, 2, 1], ]
[, [4, 3, 2], ]
[, [4, 3], [5, 2]]
[[], [4, 3], [5, 2, 1]]
[, , [5, 2, 1]]
[, [4, 1], [5, 2]]
[[3, 2], [4, 1], ]
[[3, 2, 1], , ]
[[3, 2, 1], [], [5, 4]]
[[3, 2], [], [5, 4, 1]]
[, , [5, 4, 1]]
[, [2, 1], [5, 4]]
[[], [2, 1], [5, 4, 3]]
[, , [5, 4, 3]]
[, [], [5, 4, 3, 2]]
[[], [], [5, 4, 3, 2, 1]]


## Scheme

Recursive Process

(define (towers-of-hanoi n from to spare)  (define (print-move from to)    (display "Move[")    (display from)    (display ", ")    (display to)    (display "]")    (newline))  (cond ((= n 0) "done")        (else         (towers-of-hanoi (- n 1) from spare to)         (print-move from to)         (towers-of-hanoi (- n 1) spare to from)))) (towers-of-hanoi 3 "A" "B" "C")
Output:
Move[A, B]
Move[A, C]
Move[B, C]
Move[A, B]
Move[C, A]
Move[C, B]
Move[A, B]
"done"

## Seed7

const proc: hanoi (in integer: disk, in string: source, in string: dest, in string: via) is func  begin    if disk > 0 then      hanoi(pred(disk), source, via, dest);      writeln("Move disk " <& disk <& " from " <& source <& " to " <& dest);      hanoi(pred(disk), via, dest, source);    end if;  end func;

## Sidef

Translation of: Perl
func hanoi(n, from=1, to=2, via=3) {    if (n == 1) {        say "Move disk from pole #{from} to pole #{to}.";    } else {        hanoi(n-1, from, via,   to);        hanoi(  1, from,  to,  via);        hanoi(n-1,  via,  to, from);    }} hanoi(4);

## SNOBOL4

*       # Note: count is global         define('hanoi(n,src,trg,tmp)') :(hanoi_end)hanoi   hanoi = eq(n,0) 1 :s(return)        hanoi(n - 1, src, tmp, trg)        count  = count + 1        output = count ': Move disc from ' src ' to ' trg        hanoi(n - 1, tmp, trg, src) :(return)hanoi_end *       # Test with 4 discs        hanoi(4,'A','C','B')end
Output:
1: Move disc from A to B
2: Move disc from A to C
3: Move disc from B to C
4: Move disc from A to B
5: Move disc from C to A
6: Move disc from C to B
7: Move disc from A to B
8: Move disc from A to C
9: Move disc from B to C
10: Move disc from B to A
11: Move disc from C to A
12: Move disc from B to C
13: Move disc from A to B
14: Move disc from A to C
15: Move disc from B to C

## Standard ML

   fun hanoi(0, a, b, c) = [] |
hanoi(n, a, b, c) = hanoi(n-1, a, c, b) @ [(a,b)] @ hanoi(n-1, c, b, a);


## Stata

function hanoi(n, a, b, c) {	if (n>0) {		hanoi(n-1, a, c, b)		printf("Move from %f to %f\n", a, b)		hanoi(n-1, c, b, a)	}} hanoi(3, 1, 2, 3) Move from 1 to 2Move from 1 to 3Move from 2 to 3Move from 1 to 2Move from 3 to 1Move from 3 to 2Move from 1 to 2

## Swift

Translation of: JavaScript
func hanoi(n:Int, 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)    }} hanoi(4, "A", "B", "C")

Swift 2.1

func hanoi(n:Int, a:String, b:String, c:String) {  if (n > 0) {    hanoi(n - 1, a: a, b: c, c: b)    print("Move disk from \(a) to \(c)")    hanoi(n - 1, a: b, b: a, c: c)  }} hanoi(4, a:"A", b:"B", c:"C")

## Tcl

The use of interp alias shown is a sort of closure: keep track of the number of moves required

interp alias {} hanoi {} do_hanoi 0 proc do_hanoi {count n {from A} {to C} {via B}} {    if {$n == 1} { interp alias {} hanoi {} do_hanoi [incr count] puts "$count: move from $from to$to"    } else {        incr n -1        hanoi $n$from $via$to        hanoi 1  $from$to $via hanoi$n $via$to $from }} hanoi 4 Output: 1: move from A to B 2: move from A to C 3: move from B to C 4: move from A to B 5: move from C to A 6: move from C to B 7: move from A to B 8: move from A to C 9: move from B to C 10: move from B to A 11: move from C to A 12: move from B to C 13: move from A to B 14: move from A to C 15: move from B to C ## TI-83 BASIC TI-83 BASIC lacks recursion, so technically this task is impossible, however here is a version that uses an iterative method. PROGRAM:TOHSOLVE0→A1→B0→C0→D0→M1→RWhile A<1 or A>7Input "No. of rings=?",AEndrandM(A+1,3)→[C][[1,2][1,3][2,3]]→[E] Fill(0,[C])For(I,1,A,1)I?[C](I,1)EndClrHomeWhile [C](1,3)≠1 and [C](1,2)≠1 For(J,1,3)For(I,1,A)If [C](I,J)≠0:ThenOutput(I+1,3J,[C](I,J))EndEndEndWhile C=0Output(1,3B," ")1→I[E](R,2)→JWhile [C](I,J)=0 and I≤AI+1→IEnd[C](I,J)→D1→I[E](R,1)→JWhile [C](I,J)=0 and I≤AI+1→IEndIf (D<[C](I,J) and D≠0) or [C](I,J)=0:Then[E](R,2)→BElse[E](R,1)→BEnd 1→IWhile [C](I,B)=0 and I≤AI+1→IEndIf I≤A:Then[C](I,B)→C0→[C](I,B)Output(I+1,3B," ")EndOutput(1,3B,"V")End While C≠0Output(1,3B," ")If B=[E](R,2):Then[E](R,1)→BElse[E](R,2)→BEnd 1→IWhile [C](I,B)=0 and I≤AI+1→IEndIf [C](I,B)=0 or [C](I,B)>C:ThenC→[C](I-1,B)0→CM+1→MEndEndOutput(1,3B,"V")R+1→RIf R=4:Then:1→R:End End  ## Tiny BASIC Tiny BASIC does not have recursion, so only an iterative solution is possible... and it has no arrays, so actually keeping track of individual discs is not feasible. But as if by magic, it turns out that the source and destination pegs on iteration number n are given by (n&n-1) mod 3 and ((n|n-1) + 1) mod 3 respectively, where & and | are the bitwise and and or operators. Line 40 onward is dedicated to implementing those bitwise operations, since Tiny BASIC hasn't got them natively.  5 PRINT "How many disks?" INPUT D IF D < 1 THEN GOTO 5 IF D > 10 THEN GOTO 5 LET N = 110 IF D = 0 THEN GOTO 20 LET D = D - 1 LET N = 2*N GOTO 1020 LET X = 030 LET X = X + 1 IF X = N THEN END GOSUB 40 LET S = S - 3*(S/3) GOSUB 50 LET T = T + 1 LET T = T - 3*(T/3) PRINT "Move disc on peg ",S+1," to peg ",T+1 GOTO 3040 LET B = X - 1 LET A = X LET S = 0 LET Z = 204845 LET C = 0 IF B >= Z THEN LET C = 1 IF A >= Z THEN LET C = C + 1 IF C = 2 THEN LET S = S + Z IF A >= Z THEN LET A = A - Z IF B >= Z THEN LET B = B - Z LET Z = Z / 2 IF Z = 0 THEN RETURN GOTO 4550 LET B = X - 1 LET A = X LET T = 0 LET Z = 204855 LET C = 0 IF B >= Z THEN LET C = 1 IF A >= Z THEN LET C = C + 1 IF C > 0 THEN LET T = T + Z IF A >= Z THEN LET A = A - Z IF B >= Z THEN LET B = B - Z LET Z = Z / 2 IF Z = 0 THEN RETURN GOTO 55 Output:  How many discs? 4 Move disc on peg 1 to peg 3 Move disc on peg 1 to peg 2 Move disc on peg 3 to peg 2 Move disc on peg 1 to peg 3 Move disc on peg 2 to peg 1 Move disc on peg 2 to peg 3 Move disc on peg 1 to peg 3 Move disc on peg 1 to peg 2 Move disc on peg 3 to peg 2 Move disc on peg 3 to peg 1 Move disc on peg 2 to peg 1 Move disc on peg 3 to peg 2 Move disc on peg 1 to peg 3 Move disc on peg 1 to peg 2 Move disc on peg 3 to peg 2  ## Toka value| sa sb sc n |[ to sc to sb to sa to n ] is vars![ ( num from to via -- ) vars! n 0 <> [ n sa sb sc n 1- sa sc sb recurse vars! ." Move a ring from " sa . ." to " sb . cr n 1- sc sb sa recurse ] ifTrue] is hanoi ## True BASIC Translation of: FreeBASIC  DECLARE SUB hanoi SUB hanoi(n, desde , hasta, via) IF n > 0 THEN CALL hanoi(n - 1, desde, via, hasta) PRINT "Mover disco"; n; "desde posición"; desde; "hasta posición"; hasta CALL hanoi(n - 1, via, hasta, desde) END IFEND SUB PRINT "Tres discos"PRINTCALL hanoi(3, 1, 2, 3)PRINTPRINT "Cuatro discos"PRINTCALL hanoi(4, 1, 2, 3)PRINTPRINT "Pulsa un tecla para salir"END  ## TSE SAL // library: program: run: towersofhanoi: recursive: sub <description></description> <version>1.0.0.0.0</version> <version control></version control> (filenamemacro=runprrsu.s) [kn, ri, tu, 07-02-2012 19:54:23]PROC PROCProgramRunTowersofhanoiRecursiveSub( INTEGER totalDiskI, STRING fromS, STRING toS, STRING viaS, INTEGER bufferI ) IF ( totalDiskI == 0 ) RETURN() ENDIF PROCProgramRunTowersofhanoiRecursiveSub( totalDiskI - 1, fromS, viaS, toS, bufferI ) AddLine( Format( "Move disk", " ", totalDiskI, " ", "from peg", " ", "'", fromS, "'", " ", "to peg", " ", "'", toS, "'" ), bufferI ) PROCProgramRunTowersofhanoiRecursiveSub( totalDiskI - 1, viaS, toS, fromS, bufferI )END // library: program: run: towersofhanoi: recursive <description></description> <version>1.0.0.0.6</version> <version control></version control> (filenamemacro=runprtre.s) [kn, ri, tu, 07-02-2012 19:40:45]PROC PROCProgramRunTowersofhanoiRecursive( INTEGER totalDiskI, STRING fromS, STRING toS, STRING viaS ) INTEGER bufferI = 0 PushPosition() bufferI = CreateTempBuffer() PopPosition() PROCProgramRunTowersofhanoiRecursiveSub( totalDiskI, fromS, toS, viaS, bufferI ) GotoBufferId( bufferI )END PROC Main()STRING s1 = "4"IF ( NOT ( Ask( "program: run: towersofhanoi: recursive: totalDiskI = ", s1, _EDIT_HISTORY_ ) ) AND ( Length( s1 ) > 0 ) ) RETURN() ENDIF PROCProgramRunTowersofhanoiRecursive( Val( s1 ), "source", "target", "via" )END ## uBasic/4tH Translation of: C Proc _Move(4, 1,2,3) ' 4 disks, 3 polesEnd _Move Param(4) If ([email protected] > 0) Then Proc _Move ([email protected] - 1, [email protected], [email protected], [email protected]) Print "Move disk from pole ";b@;" to pole ";[email protected] Proc _Move ([email protected] - 1, [email protected], [email protected], [email protected]) EndIfReturn ## UNIX Shell Works with: bash #!/bin/bash move(){ local n="$1"  local from="$2" local to="$3"  local via="$4" if [[ "$n" == "1" ]]  then    echo "Move disk from pole $from to pole$to"  else    move $(($n - 1)) $from$via $to move 1$from $to$via    move $(($n - 1)) $via$to $from fi} move$1 $2$3 $4 ## Ursala #import nat move = ~&al^& ^rlPlrrPCT/~&arhthPX ^|W/~& ^|G/predecessor ^/~&htxPC ~&zyxPC #show+ main = ^|T(~&,' -> '--)* move/4 <'start','end','middle'> Output: start -> middle start -> end middle -> end start -> middle end -> start end -> middle start -> middle start -> end middle -> end middle -> start end -> start middle -> end start -> middle start -> end middle -> end ## VBScript Derived from the BASIC256 version. Sub Move(n,fromPeg,toPeg,viaPeg) If n > 0 Then Move n-1, fromPeg, viaPeg, toPeg WScript.StdOut.Write "Move disk from " & fromPeg & " to " & toPeg WScript.StdOut.WriteBlankLines(1) Move n-1, viaPeg, toPeg, fromPeg End IfEnd Sub Move 4,1,2,3WScript.StdOut.Write("Towers of Hanoi puzzle completed!") Output: 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! ## Vedit macro language This implementation outputs the results in current edit buffer. #1=1; #2=2; #3=3; #4=4 // move 4 disks from 1 to 2Call("MOVE_DISKS")Return // Move disks// #1 = from, #2 = to, #3 = via, #4 = number of disks//:MOVE_DISKS:if (#4 > 0) { Num_Push(1,4) #9=#2; #2=#3; #3=#9; #4-- // #1 to #3 via #2 Call("MOVE_DISKS") Num_Pop(1,4) Ins_Text("Move a disk from ") // move one disk Num_Ins(#1, LEFT+NOCR) Ins_Text(" to ") Num_Ins(#2, LEFT) Num_Push(1,4) #9=#1; #1=#3; #3 = #9; #4-- // #3 to #2 via #1 Call("MOVE_DISKS") Num_Pop(1,4)}Return ## Vim Script function TowersOfHanoi(n, from, to, via) if (a:n > 1) call TowersOfHanoi(a:n-1, a:from, a:via, a:to) endif echom("Move a disc from " . a:from . " to " . a:to) if (a:n > 1) call TowersOfHanoi(a:n-1, a:via, a:to, a:from) endifendfunction call TowersOfHanoi(4, 1, 3, 2) Output: Move a disc from 1 to 2 Move a disc from 1 to 3 Move a disc from 2 to 3 Move a disc from 1 to 2 Move a disc from 3 to 1 Move a disc from 3 to 2 Move a disc from 1 to 2 Move a disc from 1 to 3 Move a disc from 2 to 3 Move a disc from 2 to 1 Move a disc from 3 to 1 Move a disc from 2 to 3 Move a disc from 1 to 2 Move a disc from 1 to 3 Move a disc from 2 to 3 ## Visual Basic .NET Module TowersOfHanoi Sub MoveTowerDisks(ByVal disks As Integer, ByVal fromTower As Integer, ByVal toTower As Integer, ByVal viaTower As Integer) If disks > 0 Then MoveTowerDisks(disks - 1, fromTower, viaTower, toTower) System.Console.WriteLine("Move disk {0} from {1} to {2}", disks, fromTower, toTower) MoveTowerDisks(disks - 1, viaTower, toTower, fromTower) End If End Sub Sub Main() MoveTowerDisks(4, 1, 2, 3) End SubEnd Module ## Wren Translation of: Kotlin class Hanoi { construct new(disks) { _moves = 0 System.print("Towers of Hanoi with %(disks) disks:\n") move(disks, "L", "C", "R") System.print("\nCompleted in %(_moves) moves\n") } move(n, from, to, via) { if (n > 0) { move(n - 1, from, via, to) _moves = _moves + 1 System.print("Move disk %(n) from %(from) to %(to)") move(n - 1, via, to, from) } }} Hanoi.new(3)Hanoi.new(4) Output: Towers of Hanoi with 3 disks: Move disk 1 from L to C Move disk 2 from L to R Move disk 1 from C to R Move disk 3 from L to C Move disk 1 from R to L Move disk 2 from R to C Move disk 1 from L to C Completed in 7 moves Towers of Hanoi with 4 disks: Move disk 1 from L to R Move disk 2 from L to C Move disk 1 from R to C Move disk 3 from L to R Move disk 1 from C to L Move disk 2 from C to R Move disk 1 from L to R Move disk 4 from L to C Move disk 1 from R to C Move disk 2 from R to L Move disk 1 from C to L Move disk 3 from R to C Move disk 1 from L to R Move disk 2 from L to C Move disk 1 from R to C Completed in 15 moves  ## XPL0 code Text=12; proc MoveTower(Discs, From, To, Using);int Discs, From, To, Using;[if Discs > 0 then [MoveTower(Discs-1, From, Using, To); Text(0, "Move from "); Text(0, From); Text(0, " peg to "); Text(0, To); Text(0, " peg.^M^J"); MoveTower(Discs-1, Using, To, From); ];]; MoveTower(3, "left", "right", "center") Output: Move from left peg to right peg. Move from left peg to center peg. Move from right peg to center peg. Move from left peg to right peg. Move from center peg to left peg. Move from center peg to right peg. Move from left peg to right peg.  ## XQuery declare function local:hanoi($disk as xs:integer, $from as xs:integer,$to as xs:integer, $via as xs:integer) as element()* { if($disk > 0)  then (    local:hanoi($disk - 1,$from, $via,$to),    <move disk='{$disk}'><from>{$from}</from><to>{$to}</to></move>, local:hanoi($disk - 1, $via,$to, $from) ) else ()}; <hanoi>{ local:hanoi(4, 1, 2, 3)}</hanoi> Output: <?xml version="1.0" encoding="UTF-8"?><hanoi> <move disk="1"> <from>1</from> <to>3</to> </move> <move disk="2"> <from>1</from> <to>2</to> </move> <move disk="1"> <from>3</from> <to>2</to> </move> <move disk="3"> <from>1</from> <to>3</to> </move> <move disk="1"> <from>2</from> <to>1</to> </move> <move disk="2"> <from>2</from> <to>3</to> </move> <move disk="1"> <from>1</from> <to>3</to> </move> <move disk="4"> <from>1</from> <to>2</to> </move> <move disk="1"> <from>3</from> <to>2</to> </move> <move disk="2"> <from>3</from> <to>1</to> </move> <move disk="1"> <from>2</from> <to>1</to> </move> <move disk="3"> <from>3</from> <to>2</to> </move> <move disk="1"> <from>1</from> <to>3</to> </move> <move disk="2"> <from>1</from> <to>2</to> </move> <move disk="1"> <from>3</from> <to>2</to> </move></hanoi> ## XSLT <xsl:template name="hanoi"><xsl:param name="n"/><xsl:param name="from">left</xsl:param><xsl:param name="to">middle</xsl:param><xsl:param name="via">right</xsl:param> <xsl:if test="$n &gt; 0">    <xsl:call-template name="hanoi">      <xsl:with-param name="n"    select="$n - 1"/> <xsl:with-param name="from" select="$from"/>      <xsl:with-param name="to"   select="$via"/> <xsl:with-param name="via" select="$to"/>    </xsl:call-template>    <fo:block>      <xsl:text>Move disk from </xsl:text>      <xsl:value-of select="$from"/> <xsl:text> to </xsl:text> <xsl:value-of select="$to"/>    </fo:block>    <xsl:call-template name="hanoi">      <xsl:with-param name="n"    select="$n - 1"/> <xsl:with-param name="from" select="$via"/>      <xsl:with-param name="to"   select="$to"/> <xsl:with-param name="via" select="$from"/>    </xsl:call-template>  </xsl:if></xsl:template>
<xsl:call-template name="hanoi"><xsl:with-param name="n" select="4"/></xsl:call-template>


## Yabasic

sub hanoi(ndisks, startPeg, endPeg)    if ndisks then        hanoi(ndisks-1, startPeg, 6-startPeg-endPeg)        //print "Move disk ", ndisks, " from ", startPeg, " to ", endPeg        hanoi(ndisks-1, 6-startPeg-endPeg, endPeg)    end ifend sub print "Be patient, please.\n\n"print "Hanoi 1 ellapsed ... "; t1 = peek("millisrunning")hanoi(22, 1, 3)t2 = peek("millisrunning")print t2-t1, " ms"  sub hanoi2(n, from, to_, via)    if n = 1 then	//print "Move from ", from, " to ", to_    else	hanoi2(n - 1, from, via , to_ )    	hanoi2(1    , from, to_ , via )    	hanoi2(n - 1, via , to_ , from)    end ifend sub print "Hanoi 2 ellapsed ... ";hanoi2(22, 1, 3, 2)print peek("millisrunning") - t2, " ms"

## zkl

Translation of: C
fcn move(n, from,to,via){   if (n>0){      move(n-1, from,via,to);      println("Move disk from pole %d to pole %d".fmt(from, to));      move(n-1, via,to,from);   }}move(3, 1,2,3);
Output:
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
`