Towers of Hanoi: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
Line 1:
{{task}}
[http://ultra-creator.110mb.com/articles/rudy-.htm rudy il successo di un sogno] [http://lazythoughts.110mb.com/lib/ehime/ ehime] [http://forever-your.110mb.com/data/raab-fiume.htm raab (fiume)] [http://yeeden.110mb.com/data/galileamontijo/ galileamontijo] [http://trouble166.110mb.com/stilling-johann.htm stilling johann heinrich jung] [http://olikka.110mb.com/library/jacobina.htm jacobina] [http://dialed.110mb.com/small/chiuhuahua/ chiuhuahua] [http://ultra-creator.110mb.com/articles/nylons.htm nylons] [http://olikka.110mb.com/library/telecomando-ps.htm telecomando ps2 sony] [http://mouse-zae.110mb.com/batterie-piombo.htm batterie piombo 12v] [http://nationavce.110mb.com/view/lingerie-foto.htm lingerie foto] [http://trouble166.110mb.com/done-incinte.htm done incinte] [http://forever-your.110mb.com/data/pompini-con.htm pompini con animali] [http://forever-your.110mb.com/data/televisore-panasonic.htm televisore panasonic 42] [http://yeeden.110mb.com/data/anni-/ anni 40 midi] [http://trouble166.110mb.com/mara-carfagna.htm mara carfagna] [http://blue-olusha.110mb.com/library/vasi-di/ vasi di vetro] [http://trene-von-anty.110mb.com/description/accessori-dx.htm accessori dx 7630] [http://funk-mastah-ion.110mb.com/data/librto-it.htm librto it] [http://book-wench.110mb.com/text/addon/ addon 5 1] [http://sister-sunshine.110mb.com/html/dottor-dolittle/ dottor dolittle] [http://stitchesnglue.110mb.com/marlene-kuntz.htm marlene kuntz foto] [http://some-omeelse.110mb.com/data/ora-vieni.htm ora vieni con me] [http://slavkamolot.110mb.com/library/ganga-bang/ ganga bang] [http://some-omeelse.110mb.com/data/motivation.htm motivation] [http://gekijuls.110mb.com/unie/ unie] [http://slavkamolot.110mb.com/library/reptile-s/ reptile s house] [http://blue-olusha.110mb.com/library/volkswagen-km/ volkswagen km zero] [http://mouse-zae.110mb.com/puntatori-laser.htm puntatori laser] [http://mouse-zae.110mb.com/yo-te.htm yo te avise] [http://trouble166.110mb.com/mr-driller.htm mr driller] [http://funk-mastah-ion.110mb.com/data/di-stephen.htm di stephen king] [http://funk-mastah-ion.110mb.com/data/iriver.htm iriver 895] [http://gekijuls.110mb.com/que-es/ que es linguistica] [http://book-wench.110mb.com/text/el-baile/ el baile del pescado] [http://blue-olusha.110mb.com/library/final-fantasy/ final fantasy xii] [http://book-wench.110mb.com/text/scorn-not/ scorn not his simplicity] [http://nationavce.110mb.com/view/photogallery-filippo.htm photogallery filippo inzaghi] [http://mouse-zae.110mb.com/espanish.htm espanish] [http://dialed.110mb.com/small/aspirapolveri-bosch/ aspirapolveri bosch] [http://some-omeelse.110mb.com/data/canon-reflex.htm canon reflex] [http://dialed.110mb.com/small/calendario-rutas/ calendario rutas] [http://nationavce.110mb.com/view/ders.htm ders] [http://blue-olusha.110mb.com/library/sony-hc/ sony hc1 e] [http://gekijuls.110mb.com/nfl-fever/ nfl fever 2002] [http://dialed.110mb.com/small/kodak-ls/ kodak ls753] [http://dialed.110mb.com/small/primo-chip/ primo chip] [http://unremig.110mb.com/tosciba-tecra/ tosciba tecra a3] [http://nationavce.110mb.com/view/sotto-massima.htm sotto massima sicurezza] [http://lazythoughts.110mb.com/lib/siti-di/ siti di gossip] [http://sneezingpops.110mb.com/left-outside/ left outside alone anastasia] [http://ultra-creator.110mb.com/articles/sonata-per.htm sonata per clarinetto e pianoforte] [http://trouble166.110mb.com/panasonic-.htm panasonic 300 x] [http://funk-mastah-ion.110mb.com/data/a-a.htm a a a offresi sesso] [http://blue-olusha.110mb.com/library/free-me/ free me] [http://funk-mastah-ion.110mb.com/data/cura.htm cura] [http://unremig.110mb.com/sinonimo-e/ sinonimo e contrario] [http://sister-sunshine.110mb.com/html/vagina-pelosa/ vagina pelosa] [http://dialed.110mb.com/small/pinguino/ pinguino 9000] [http://slavkamolot.110mb.com/library/jbl-e/ jbl e 20] [http://sister-sunshine.110mb.com/html/nikita-elton/ nikita elton john] [http://funk-mastah-ion.110mb.com/data/bearpaw-cu.htm bearpaw 2448cu pro usb2 0 19200 dpi] [http://sneezingpops.110mb.com/scandalo-premeditato/ scandalo premeditato] [http://unremig.110mb.com/mp-portatile/ mp3 portatile cd dvd] [http://book-wench.110mb.com/text/video-x/ video x gratuit] [http://some-omeelse.110mb.com/data/striscioni-calcio.htm striscioni calcio] [http://gekijuls.110mb.com/via-technologies/ via technologies] [http://trene-von-anty.110mb.com/description/vendita-dvd.htm vendita dvd vergini on line] [http://mouse-zae.110mb.com/derosa.htm derosa] [http://funk-mastah-ion.110mb.com/data/fiat-seicento.htm fiat seicento anno 2002] [http://dialed.110mb.com/small/yamaha-chitarra/ yamaha chitarra acustica] [http://trouble166.110mb.com/www-agriturismo.htm www agriturismo a magione] [http://funk-mastah-ion.110mb.com/data/ex-rumbera.htm ex rumbera] [http://trouble166.110mb.com/www-mailworld.htm www mail2world com] [http://ultra-creator.110mb.com/articles/trust-.htm trust 600 ups 600va] [http://trouble166.110mb.com/www-cheb.htm www cheb bilal] [http://lazythoughts.110mb.com/lib/wanhsien/ wanhsien] [http://trene-von-anty.110mb.com/description/manutentore-meccanico.htm manutentore meccanico (regione piemonte alessandria provincia)] [http://funk-mastah-ion.110mb.com/data/serigrafica.htm serigrafica] [http://some-omeelse.110mb.com/data/ciber-mice.htm ciber mice party] [http://stitchesnglue.110mb.com/boogi-pimps.htm boogi pimps] [http://sneezingpops.110mb.com/fad/ fad] [http://lazythoughts.110mb.com/lib/rafaella-cicarelli/ rafaella cicarelli] [http://dialed.110mb.com/small/hotel-arles/ hotel arles] [http://trouble166.110mb.com/arcidiocesi-di.htm arcidiocesi di cracovia] [http://stitchesnglue.110mb.com/video-site.htm video site de cul xxx] [http://slavkamolot.110mb.com/library/sd/ sd] [http://blue-olusha.110mb.com/library/vrs-n/ vrs n8100] [http://trouble166.110mb.com/drago-dacciaio.htm drago dacciaio] [http://ultra-creator.110mb.com/articles/climatizzatore-a.htm climatizzatore a pompa di calore lg] [http://ultra-creator.110mb.com/articles/yanou-et.htm yanou et dj sammy] [http://sister-sunshine.110mb.com/html/becaud/ becaud] [http://unremig.110mb.com/formato-europeo/ formato europeo curriculum vit] [http://nationavce.110mb.com/view/epson-emp.htm epson emp835] [http://mouse-zae.110mb.com/joypad-wireless.htm joypad wireless xbox accessori console] [http://sister-sunshine.110mb.com/html/vendita-piscine/ vendita piscine in cemento armato citta catania] [http://sister-sunshine.110mb.com/html/cover-nokia/ cover nokia 6600] [http://gekijuls.110mb.com/mondiale-calcio/ mondiale calcio 2006] [http://stitchesnglue.110mb.com/thermaltake-orb.htm thermaltake orb ii] [http://forever-your.110mb.com/data/frammenti-di.htm frammenti di novecento] [http://trene-von-anty.110mb.com/description/v-creative.htm v200 creative] [http://lazythoughts.110mb.com/lib/nocturne-with/ nocturne with no moon] [http://nationavce.110mb.com/view/sucera.htm sucera] [http://some-omeelse.110mb.com/data/eskimo-joe.htm eskimo joe] [http://book-wench.110mb.com/text/la-vita/ la vita la vita] [http://slavkamolot.110mb.com/library/viva-san/ viva san calo] [http://slavkamolot.110mb.com/library/zombi/ zombi] [http://trouble166.110mb.com/tre-cuori.htm tre cuori in affitto] [http://book-wench.110mb.com/text/s-giorgio/ s giorgio] [http://mouse-zae.110mb.com/junior-jack.htm junior jack stupiddisco] [http://ultra-creator.110mb.com/articles/lara-wendel.htm lara wendel] [http://yeeden.110mb.com/data/www-erboristerie/ www erboristerie it] [http://stitchesnglue.110mb.com/ati-xse.htm ati x300se] [http://olikka.110mb.com/library/hex-file.htm hex file tps french] [http://unremig.110mb.com/carlo-martello/ carlo martello] [http://forever-your.110mb.com/data/un-cappello.htm un cappello pieno di pioggia] [http://dialed.110mb.com/small/boss-od/ boss od3] [http://slavkamolot.110mb.com/library/hans-georg/ hans georg gadamer] [http://some-omeelse.110mb.com/data/estupido.htm estupido] [http://nationavce.110mb.com/view/cd-audio.htm cd audio mp3] [http://some-omeelse.110mb.com/data/lopez-live.htm lopez live puerto rico] [http://stitchesnglue.110mb.com/giochi-cel.htm giochi cel] [http://book-wench.110mb.com/text/harumi/ harumi] [http://gekijuls.110mb.com/diaspora-degli/ diaspora degli ebrei] [http://trouble166.110mb.com/perfection.htm perfection 4490] [http://blue-olusha.110mb.com/library/nokia-/ nokia 6680 1 gb] [http://dialed.110mb.com/small/verbatim-dvd/ verbatim dvd printable] [http://sister-sunshine.110mb.com/html/togher/ togher] [http://olikka.110mb.com/library/veterinari-pv.htm veterinari pv] [http://stitchesnglue.110mb.com/nokia-auricolare.htm nokia auricolare bluetooth hs 36w] [http://olikka.110mb.com/library/re-collage.htm re collage] [http://yeeden.110mb.com/data/un-fiore/ un fiore nel deserto] [http://ultra-creator.110mb.com/articles/pantaloni-vita.htm pantaloni vita bassa] [http://stitchesnglue.110mb.com/spartito-lo.htm spartito lo strano percorso] [http://some-omeelse.110mb.com/data/centrifuga-asciugabiancheria.htm centrifuga asciugabiancheria] [http://slavkamolot.110mb.com/library/phanton/ phanton] [http://stitchesnglue.110mb.com/uomini-nudisti.htm uomini nudisti] [http://unremig.110mb.com/computer-assemblato/ computer assemblato] [http://book-wench.110mb.com/text/bud-e/ bud e terence] [http://stitchesnglue.110mb.com/foto-sesso.htm foto sesso in gruppo] [http://lazythoughts.110mb.com/lib/durezza/ durezza] [http://stitchesnglue.110mb.com/goldminer-online.htm goldminer online] [http://olikka.110mb.com/library/giochi-di.htm giochi di coppia] [http://yeeden.110mb.com/data/le-infermiere/ le infermiere] [http://dialed.110mb.com/small/gioco-scaricare/ gioco scaricare gratis pc] [http://sneezingpops.110mb.com/klubbheroes/ klubbheroes] [http://trouble166.110mb.com/opel-tigra.htm opel tigra 1999] [http://book-wench.110mb.com/text/eros-ramazzotti/ eros ramazzotti] [http://gekijuls.110mb.com/monchy-alexandra/ monchy alexandra] [http://forever-your.110mb.com/data/accessori-cuffie.htm accessori cuffie] [http://funk-mastah-ion.110mb.com/data/jet-blue.htm jet blue] [http://mouse-zae.110mb.com/eurelle.htm eurelle] [http://stitchesnglue.110mb.com/linguaggio-degli.htm linguaggio degli animali] [http://mouse-zae.110mb.com/dissipatore-alluminio.htm dissipatore alluminio] [http://funk-mastah-ion.110mb.com/data/cassic-puzzle.htm cassic puzzle] [http://mouse-zae.110mb.com/que-es.htm que es la flor] [http://slavkamolot.110mb.com/library/auricolare-bluetooth/ auricolare bluetooth treo] [http://blue-olusha.110mb.com/library/aa/ aa 12000] [http://dialed.110mb.com/small/irrigazione-kit/ irrigazione kit] [http://sister-sunshine.110mb.com/html/mercedes-classe/ mercedes classe a elegance km 0] [http://ultra-creator.110mb.com/articles/carta-astrale.htm carta astrale] [http://some-omeelse.110mb.com/data/villaggio-turistici.htm villaggio turistici grecia] [http://some-omeelse.110mb.com/data/mms-tim.htm mms tim] [http://ultra-creator.110mb.com/articles/uno-scapolo.htm uno scapolo in paradiso] [http://olikka.110mb.com/library/age-of.htm age of mythology gold] [http://yeeden.110mb.com/data/router-u/ router u s] [http://ultra-creator.110mb.com/articles/www-escortgirls.htm www escortgirls com] [http://trouble166.110mb.com/reflex-digitale.htm reflex digitale pentax] [http://gekijuls.110mb.com/renault-clio/ renault clio 1 8] [http://yeeden.110mb.com/data/jump-sascha/ jump sascha] [http://mouse-zae.110mb.com/masterizzatore-dvdrw.htm masterizzatore dvdrw liteon] [http://slavkamolot.110mb.com/library/luxury-gifts/ luxury gifts] [http://nationavce.110mb.com/view/pallone-rugby.htm pallone rugby] [http://dialed.110mb.com/small/dj-francesco/ dj francesco e aida yespica] [http://gekijuls.110mb.com/sugar-ray/ sugar ray no cerveza no trabaja] [http://yeeden.110mb.com/data/bikes/ bikes] [http://some-omeelse.110mb.com/data/sedere-miss.htm sedere miss] [http://funk-mastah-ion.110mb.com/data/lavoro-faenza.htm lavoro faenza] [http://book-wench.110mb.com/text/xbox-pack/ xbox pack] [http://forever-your.110mb.com/data/pizzi-nilla.htm pizzi nilla] [http://blue-olusha.110mb.com/library/www-paginegialle/ www paginegialle com] [http://gekijuls.110mb.com/hotel-sardegna/ hotel sardegna tre stelle] [http://lazythoughts.110mb.com/lib/telefilm-di/ telefilm di settimo cielo] [http://ultra-creator.110mb.com/articles/www-mifuturo.htm www mifuturo com] [http://blue-olusha.110mb.com/library/drukyul/ drukyul] [http://gekijuls.110mb.com/tv-alta/ tv alta definizione da 32] [http://slavkamolot.110mb.com/library/per-favore/ per favore non toccate le palline] [http://some-omeelse.110mb.com/data/donne-inculate.htm donne inculate da scimmie] [http://gekijuls.110mb.com/codename-panzers/ codename panzers] [http://dialed.110mb.com/small/gay-taranto/ gay taranto] [http://unremig.110mb.com/don-abbondio/ don abbondio] [http://trene-von-anty.110mb.com/description/lettore-dvd.htm lettore dvd per portatile e tv] [http://sneezingpops.110mb.com/philips-sa/ philips sa175] [http://mouse-zae.110mb.com/ericsson-t.htm ericsson t18] [http://lazythoughts.110mb.com/lib/isuzu-pick/ isuzu pick up] [http://some-omeelse.110mb.com/data/nudisti-foto.htm nudisti foto] [http://olikka.110mb.com/library/pda-accessorio.htm pda accessorio mio 168 supporto per auto] [http://unremig.110mb.com/gimme-some/ gimme some love] [http://dialed.110mb.com/small/bullitt/ bullitt] [http://sneezingpops.110mb.com/foto-sonia/ foto sonia gray] {{task}}
 
In this task, the goal is to solve the Towers of Hanoi problem with recursivity.
Line 13:
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]]==
[[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 18:16, 25 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