Towers of Hanoi: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(revert spam)
Line 1: Line 1:
{{task}}
[http://fikimber.125mb.com/lib/le-olimpiadi.htm le olimpiadi della giungla] [http://kocarawf.125mb.com/blog/el-chiringuito.htm el chiringuito] [http://bedrew.125mb.com/bisca.htm bisca] [http://dleslis.125mb.com/resources/dr-lg.htm dr4800 lg] [http://bedrew.125mb.com/master-falcon.htm master falcon] [http://gitedy.125mb.com/blog/etilometro/ etilometro] [http://ruwier.125mb.com/ebony-pussy/ ebony pussy] [http://dleslis.125mb.com/resources/cavit-karabey.htm cavit karabey] [http://fikimber.125mb.com/lib/espectaculo.htm espectaculo] [http://sibeatri.angelfire.com/html/lavoro-lanciano/ lavoro lanciano] [http://mamans.125mb.com/topic/video-rock.htm video rock] [http://mamans.125mb.com/topic/master-strong.htm master strong] [http://nescogin.125mb.com/content/la-guerra.htm la guerra di pero] [http://fikimber.125mb.com/lib/certificazioni-gas.htm certificazioni gas metano] [http://yonosoyyo.angelfire.com/directory/tommy-srl.htm tommy srl] [http://gitedy.125mb.com/blog/my-sacrifice/ my sacrifice] [http://hahosmer.angelfire.com/html/vendita-personal.htm vendita personal computer] [http://stjollay.125mb.com/topic/chimaira/ chimaira] [http://dacornel.125mb.com/data/carene.htm carene] [http://elernst.angelfire.com/articles/neonato-set.htm neonato set avent] [http://slavfondmeister.angelfire.com/styles/stadio-.htm stadio 2 sprint] [http://fikimber.125mb.com/lib/tariffa-aere.htm tariffa aere] [http://yarobiso.angelfire.com/text/power-color.htm power color 9600pro] [http://tibrown.125mb.com/description/televisore-philips/ televisore philips 25] [http://paryker.125mb.com/styles/agguato-nei/ agguato nei caraibi] [http://dleslis.125mb.com/resources/ozone-mia.htm ozone mia ai ha] [http://mamans.125mb.com/topic/gioco-demo.htm gioco demo simulatore] [http://awihimmeru.angelfire.com/commercialista.htm commercialista] [http://sewehner.125mb.com/articles/nike-dry/ nike dry fit] [http://spdinnie.angelfire.com/html/vasco-rossi/ vasco rossi net] [http://fikimber.125mb.com/lib/foto-hard.htm foto hard lilli carati gratis] [http://elernst.angelfire.com/articles/vacuna-contra.htm vacuna contra el cancer] [http://nescogin.125mb.com/content/single-calabresi.htm single calabresi] [http://vegrigsb.angelfire.com/resources/danzig.htm danzig] [http://sawilson.125mb.com/data/km-fiat.htm km0 fiat punto d diesel auto km 0] [http://anethel.angelfire.com/lettini-viaggio.htm lettini viaggio graco] [http://fikimber.125mb.com/lib/codag.htm codag] [http://fikimber.125mb.com/lib/dvd-vhs.htm dvd vhs combinato] [http://blhestrie.125mb.com/web/annuncio-cartomanzia/ annuncio cartomanzia] [http://hahosmer.angelfire.com/html/parnaso-raffaello.htm parnaso raffaello] [http://paryker.125mb.com/styles/decoder-satellite/ decoder satellite humax] [http://dleslis.125mb.com/resources/selen-super.htm selen super troia] [http://requidim.angelfire.com/web/ragazzo-civitanova.htm ragazzo civitanova marche] [http://blhestrie.125mb.com/web/apple-motion/ apple motion] [http://bedrew.125mb.com/ufficio-somma.htm ufficio somma lombardo] [http://woharwoo.angelfire.com/data/motorhead-the/ motorhead the best of motorhead] [http://fikimber.125mb.com/lib/il-paese.htm il paese senza dio] [http://awihimmeru.angelfire.com/ceraunavolta.htm ceraunavolta] [http://wiellico.125mb.com/html/tapis-roulant.htm tapis roulant olympic 2000] [http://hoadmoni.125mb.com/styles/grow-old/ grow old with you] [http://sibeatri.angelfire.com/html/silvia-salemi/ silvia salemi] [http://nescogin.125mb.com/content/genio-e.htm genio e follia] [http://ruwier.125mb.com/micheal-buble/ micheal buble the way you look tonight] [http://fikimber.125mb.com/lib/butinar.htm butinar] [http://slavfondmeister.angelfire.com/styles/www-gazzzetta.htm www gazzzetta] [http://gistrate.angelfire.com/html/depucellage.htm depucellage] [http://dleslis.125mb.com/resources/russia-cartina.htm russia cartina] [http://fikimber.125mb.com/lib/sinto-tv.htm sinto tv videoproiettori] [http://dleslis.125mb.com/resources/colonne-sonore.htm colonne sonore highlander] [http://degaylor.125mb.com/fg/ fg 42] [http://sawilson.125mb.com/data/jeep.htm jeep] [http://paryker.125mb.com/styles/minnesota-cose/ minnesota cose fare] [http://hoadmoni.125mb.com/styles/surgical-gloves/ surgical gloves] [http://spdinnie.angelfire.com/html/www-totoscommesse/ www totoscommesse it] [http://nescogin.125mb.com/content/perfekte.htm perfekte] [http://hahosmer.angelfire.com/html/ufficio-lyon.htm ufficio lyon] [http://paryker.125mb.com/styles/baba-o/ baba o riley] [http://fikimber.125mb.com/lib/hellgate-il.htm hellgate il grande inferno] [http://yarobiso.angelfire.com/text/memoria-aspire.htm memoria aspire 1200] [http://bedrew.125mb.com/accessori-canon.htm accessori canon xl1] [http://dacornel.125mb.com/data/dvp-dvd.htm dvp3010 dvd dvx philips] [http://anethel.angelfire.com/meteo-bologna.htm meteo bologna] [http://vegrigsb.angelfire.com/resources/multipla-bipower.htm multipla bipower torino] [http://sawilson.125mb.com/data/globe-theatre.htm globe theatre roma] [http://mamans.125mb.com/topic/sodimm-.htm sodimm 512 mb 333] [http://degaylor.125mb.com/fiera-inquieta/ fiera inquieta] [http://degaylor.125mb.com/biancheria-intima/ biancheria intima donna] [http://dacornel.125mb.com/data/schiavo-terry.htm schiavo terry] [http://woharwoo.angelfire.com/data/palmare-palmone/ palmare palmone treo 600] [http://sewehner.125mb.com/articles/uno-straniero/ uno straniero a sacramento] [http://dacornel.125mb.com/data/cerco-corriere.htm cerco corriere per trasporto cuccioli] [http://fikimber.125mb.com/lib/la-cavalletta.htm la cavalletta] [http://dacornel.125mb.com/data/www-lanciay.htm www lanciay it] [http://bedrew.125mb.com/horsex.htm horsex] [http://bedrew.125mb.com/fotos-de.htm fotos de los pijos 87] [http://kocarawf.125mb.com/blog/tribute.htm tribute] [http://wiellico.125mb.com/html/philips-pw.htm philips 32pw9309] [http://kocarawf.125mb.com/blog/gate-jump.htm gate jump] [http://requidim.angelfire.com/web/coracle.htm coracle] [http://tibrown.125mb.com/description/frontera-com/ frontera com] [http://blhestrie.125mb.com/web/f-world/ f1 world grand prix] [http://degaylor.125mb.com/thomas-friedman/ thomas friedman] [http://anethel.angelfire.com/biglietto-di.htm biglietto di augurio da stampare] [http://tibrown.125mb.com/description/electrolux-trilobite/ electrolux trilobite 2] [http://anethel.angelfire.com/versace-woman.htm versace woman] [http://yarobiso.angelfire.com/text/regali-nascita.htm regali nascita] [http://morudd.angelfire.com/web/gigaset-c/ gigaset c35] [http://sewehner.125mb.com/articles/monopoli/ monopoli] [http://yonosoyyo.angelfire.com/directory/hp-cxi.htm hp 970cxi] [http://mcclaise.angelfire.com/description/sboccare-motorola/ sboccare motorola a835] [http://hoadmoni.125mb.com/styles/www-lupin/ www lupin com] [http://nescogin.125mb.com/content/cardiofrequenzimetro-ciclismo.htm cardiofrequenzimetro ciclismo sport e] [http://fikimber.125mb.com/lib/jvc-mce.htm jvc mc500e] [http://amarymuk.angelfire.com/html/codici-sblocco.htm codici sblocco lg u8110] [http://sibeatri.angelfire.com/html/la-voce/ la voce della calunnia] [http://kocarawf.125mb.com/blog/best-time.htm best time viaggi] [http://ruwier.125mb.com/televisori-/ televisori 10 pollici] [http://slavfondmeister.angelfire.com/styles/memoria-ram.htm memoria ram 512 ddr] [http://nescogin.125mb.com/content/bella-riccardo.htm bella riccardo cocciante] [http://degaylor.125mb.com/tutti-i/ tutti i miei sbagli] [http://slavfondmeister.angelfire.com/styles/not-without.htm not without us] [http://champ-alnite.angelfire.com/rotten-in.htm rotten in culo] [http://mamans.125mb.com/topic/hp-nx.htm hp nx 6125] [http://stjollay.125mb.com/topic/storia-della/ storia della pubblicita] [http://spdinnie.angelfire.com/html/www-the/ www the oc it] [http://fikimber.125mb.com/lib/videofonino-lg.htm videofonino lg] [http://stjollay.125mb.com/topic/universite-reims/ universite reims] [http://tibrown.125mb.com/description/vanni/ vanni] [http://blhestrie.125mb.com/web/basi-nusicali/ basi nusicali] [http://awihimmeru.angelfire.com/www-vitadidonna.htm www vitadidonna it] [http://stjollay.125mb.com/topic/fiat-punto/ fiat punto speed 1 3] [http://kocarawf.125mb.com/blog/canon-ef.htm canon ef 50mm] [http://fikimber.125mb.com/lib/dream-desk.htm dream desk pc] [http://elernst.angelfire.com/articles/gemini-astronomia.htm gemini (astronomia)] [http://morudd.angelfire.com/web/video-titmuss/ video titmuss] [http://paryker.125mb.com/styles/inviato-da/ inviato da roberta] [http://champ-alnite.angelfire.com/abit-fatalty.htm abit fatal1ty 939] [http://sibeatri.angelfire.com/html/fiat-marea/ fiat marea 105 jtd elx] [http://blhestrie.125mb.com/web/mesoterapia-corso/ mesoterapia corso] [http://woharwoo.angelfire.com/data/i-m/ i m so exciting] [http://mamans.125mb.com/topic/mercy-drive.htm mercy drive] [http://mamans.125mb.com/topic/server-di.htm server di memorizzazione di rete] [http://kocarawf.125mb.com/blog/i-duetti.htm i duetti di teatro 10] [http://stjollay.125mb.com/topic/w-w/ w w w meteotrentino it] [http://grdugans.angelfire.com/text/la-ragazza.htm la ragazza di tony] [http://mcclaise.angelfire.com/description/usbstor/ usbstor] [http://dacornel.125mb.com/data/file-maker.htm file maker pro 7] [http://gitedy.125mb.com/blog/bluetoth/ bluetoth] [http://paryker.125mb.com/styles/kenwood-bm/ kenwood bm300] [http://sewehner.125mb.com/articles/di-saracu/ di saracu] [http://sawilson.125mb.com/data/empire-ehart.htm empire ehart] [http://grdugans.angelfire.com/text/fly-on.htm fly on drugs] [http://spdinnie.angelfire.com/html/key-tps/ key tps france] [http://ruwier.125mb.com/entony-club/ entony club] [http://gitedy.125mb.com/blog/lightin-break/ lightin break] [http://ruwier.125mb.com/incontro-pinerolo/ incontro pinerolo] [http://spdinnie.angelfire.com/html/le-soldat/ le soldat inconnu sauvegarde] [http://gitedy.125mb.com/blog/big-tettone/ big tettone] [http://spdinnie.angelfire.com/html/quan-nha/ quan nha la vn com] [http://fikimber.125mb.com/lib/sm.htm sm 17] [http://morudd.angelfire.com/web/amstrad-lettore/ amstrad lettore dx] [http://mcclaise.angelfire.com/description/itaia/ itaia 1] [http://awihimmeru.angelfire.com/fiera-di.htm fiera di milano] [http://dleslis.125mb.com/resources/epson-monitor.htm epson monitor] [http://morudd.angelfire.com/web/lavoro-a/ lavoro a verbania] [http://grdugans.angelfire.com/text/mostert.htm mostert] [http://sawilson.125mb.com/data/new-mediaguard.htm new mediaguard keys] [http://nescogin.125mb.com/content/photo-porno.htm photo porno] [http://dleslis.125mb.com/resources/sonho-lindo.htm sonho lindo] [http://anethel.angelfire.com/ona.htm ona] [http://dleslis.125mb.com/resources/notebook-pentium.htm notebook pentium 4 3 ghz] [http://dleslis.125mb.com/resources/giorgia-on.htm giorgia on my mind] [http://dacornel.125mb.com/data/nero-wolfe.htm nero wolfe circuito chiuso] [http://yonosoyyo.angelfire.com/directory/sistema-operativo.htm sistema operativo pocket pc] [http://sewehner.125mb.com/articles/televisori-lcd/ televisori lcd 30 sony] [http://wiellico.125mb.com/html/vicentico.htm vicentico] [http://stjollay.125mb.com/topic/re-scorpione/ re scorpione] [http://gistrate.angelfire.com/html/world-championship.htm world championship 2004 yu gi oh] [http://mamans.125mb.com/topic/filmografia-carmen.htm filmografia carmen elettra] [http://kocarawf.125mb.com/blog/toshiba.htm toshiba 60] [http://bedrew.125mb.com/seggiolino-auto.htm seggiolino auto brevi] [http://gitedy.125mb.com/blog/billy-honey/ billy honey] [http://spdinnie.angelfire.com/html/fischia-il/ fischia il vento] [http://gistrate.angelfire.com/html/cortona-turismo.htm cortona turismo] [http://tibrown.125mb.com/description/nikon-mc/ nikon mc20] [http://nescogin.125mb.com/content/calathea.htm calathea] [http://morudd.angelfire.com/web/euronova-italia/ euronova italia it] [http://blhestrie.125mb.com/web/spaccio-cesare/ spaccio cesare paciotti civitanova] [http://woharwoo.angelfire.com/data/nude-in/ nude in video] [http://degaylor.125mb.com/we-are/ we are the worlde] [http://anethel.angelfire.com/predator-pulse.htm predator pulse calcio] [http://gitedy.125mb.com/blog/cit-viaggi/ cit viaggi] [http://yarobiso.angelfire.com/text/programma-per.htm programma per mixare] [http://stjollay.125mb.com/topic/hot-meil/ hot meil] [http://awihimmeru.angelfire.com/the-aristocrats.htm the aristocrats] [http://sawilson.125mb.com/data/memoria-portatile.htm memoria portatile 1gb] [http://gistrate.angelfire.com/html/frodi.htm frodi] [http://spdinnie.angelfire.com/html/cheguevara/ cheguevara] [http://sibeatri.angelfire.com/html/quetzal/ quetzal] [http://sewehner.125mb.com/articles/reinhold-messner/ reinhold messner] [http://hoadmoni.125mb.com/styles/novecento-bertolucci/ novecento bertolucci] [http://yarobiso.angelfire.com/text/nuova-fiat.htm nuova fiat furgone auto nuove] [http://sewehner.125mb.com/articles/la-roccia/ la roccia di fuoco] [http://paryker.125mb.com/styles/kingston-/ kingston 512mb ddr sodimm (915)] [http://wiellico.125mb.com/html/katie-cassidy.htm katie cassidy] [http://elernst.angelfire.com/articles/navigatore-gps.htm navigatore gps satellitare garmin] [http://wiellico.125mb.com/html/pallavolo-a.htm pallavolo a firenze] [http://dacornel.125mb.com/data/hymn-polski.htm hymn polski] [http://sewehner.125mb.com/articles/cavo-dvi/ cavo dvi hdmi] [http://degaylor.125mb.com/rasoi-elettrici/ rasoi elettrici ricaricabili philips] {{task}}


In this task, the goal is to solve the Towers of Hanoi problem with recursivity.
In this task, the goal is to solve the Towers of Hanoi problem with recursivity.
Line 13: Line 13:
if Ndisks > 0 then
if Ndisks > 0 then
Hanoi(Ndisks - 1, Start_Peg, Via_Peg, End_Peg);
Hanoi(Ndisks - 1, Start_Peg, Via_Peg, End_Peg);
Put_Line("Move disk"
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;


==[[AppleScript]]==
[[Category:AppleScript]]
global moves --this is so the handler 'hanoi' can see the 'moves' variable
set moves to ""
hanoi(4, "peg A", "peg C", "peg B")
on hanoi(ndisks, fromPeg, toPeg, withPeg)
if ndisks is greater than 0 then
hanoi(ndisks - 1, fromPeg, withPeg, toPeg)
set moves to moves & "Move disk " & ndisks & " from " & fromPeg & " to " & toPeg & return
hanoi(ndisks - 1, withPeg, toPeg, fromPeg)
end if
return moves
end hanoi


==[[C plus plus|C++]]==
[[Category:C plus plus]]
'''Compiler:''' [[GCC]]

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);
}
}



==[[E]]==
[[Category:E]]

def move(out, n, fromPeg, toPeg, viaPeg) {
if (n.aboveZero()) {
move(out, n.previous(), fromPeg, viaPeg, toPeg)
out.println(`Move disk $n from $fromPeg to $toPeg.`)
move(out, n.previous(), viaPeg, toPeg, fromPeg)
}
}
move(stdout, 4, def left {}, def right {}, def middle {})

==[[Forth]]==
[[Category:Forth]]
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" ;
: print ( t f -- )
CR ." Move disk from " execute ." to " execute ;
: move-disk ( v t f n -- v t f )
dup 1 = if drop 2dup print exit then
1- >R
rot swap R@ ( t v f n-1 ) recurse
rot swap 2dup print
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 ;

==[[Java]]==
[[Category: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);
}
}

==[[Perl]]==
[[Category:Perl]]
sub move {
my $n = shift;
my $from = shift;
my $to = shift;
my $via = shift;
if ($n == 1) {
print "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);
};
};

==[[Pop11]]==
[[Category:Pop11]]

define hanoi(n, src, dst, via);
if n > 0 then
hanoi(n - 1, src, via, dst);
printf('Move disk ' >< n >< ' from ' >< src >< ' to ' >< dst >< '.\n');
hanoi(n - 1, via, dst, src);
endif;
enddefine;

hanoi(4, "left", "middle", "right");

==[[Python]]==
[[Category:Python]]

<pre>
def hanoi(ndisks, startPeg=1, endPeg=3):
if ndisks:
hanoi(ndisks-1, startPeg, 6-startPeg-endPeg)
print "Move disk %d from peg %d to peg %d" % (ndisks, startPeg, endPeg)
hanoi(ndisks-1, 6-startPeg-endPeg, endPeg)

hanoi(ndisks=4)
</pre>

==[[Seed7]]==
[[Category: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;

==[[Toka]]==
[[Category: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

Revision as of 19:46, 29 August 2007

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

In this task, the goal is to solve the Towers of Hanoi problem with recursivity.

Ada

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; Via_Peg : Pegs := Center; End_Peg : Pegs := Right) 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;


AppleScript

global moves --this is so the handler 'hanoi' can see the 'moves' variable
set moves to ""
hanoi(4, "peg A", "peg C", "peg B")

on hanoi(ndisks, fromPeg, toPeg, withPeg)
    if ndisks is greater than 0 then
        hanoi(ndisks - 1, fromPeg, withPeg, toPeg)
        set moves to moves & "Move disk " & ndisks & " from " & fromPeg & " to " & toPeg & return
        hanoi(ndisks - 1, withPeg, toPeg, fromPeg)
    end if
    return moves
end hanoi


C++

Compiler: GCC

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);
  }
}


E

def move(out, n, fromPeg, toPeg, viaPeg) {
    if (n.aboveZero()) {
        move(out, n.previous(), fromPeg, viaPeg, toPeg)
        out.println(`Move disk $n from $fromPeg to $toPeg.`)
        move(out, n.previous(), viaPeg, toPeg, fromPeg)
    }
}

move(stdout, 4, def left {}, def right {}, def middle {})

Forth

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" ;

: print ( t f -- )
  CR ." Move disk from " execute ."  to " execute  ;
: move-disk ( v t f n -- v t f )
  dup 1 = if drop 2dup print exit then
  1-       >R
  rot swap R@ ( t v f n-1 ) recurse
  rot swap        2dup print
  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 ;

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);
   }
 }

Perl

sub move {
    my $n    = shift;
    my $from = shift;
    my $to   = shift;
    my $via  = shift;

    if ($n == 1) {
        print "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);
    };
};

Pop11

define hanoi(n, src, dst, via);
if n > 0 then
    hanoi(n - 1, src, via, dst);
    printf('Move disk ' >< n >< ' from ' >< src >< ' to ' >< dst >< '.\n');
    hanoi(n - 1, via, dst, src);
endif;
enddefine;
hanoi(4, "left", "middle", "right");

Python

def hanoi(ndisks, startPeg=1, endPeg=3):
    if ndisks:
        hanoi(ndisks-1, startPeg, 6-startPeg-endPeg)
        print "Move disk %d from peg %d to peg %d" % (ndisks, startPeg, endPeg)
        hanoi(ndisks-1, 6-startPeg-endPeg, endPeg)

hanoi(ndisks=4)

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;

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