Towers of Hanoi: Difference between revisions
Content deleted Content added
m Undo revision 8057 by Special:Contributions/On5M1p (User talk:On5M1p) |
mNo edit summary |
||
Line 1: | Line 1: | ||
[http://nibko.info/data/discoteche-lombarde.htm discoteche lombarde] [http://italnet.net.cn/img/diritto-penale/ diritto penale fiandaca musco] [http://italnet.net.cn/img/happy-birthay/ happy birthay] [http://puli.org.cn/web/last-tango/ last tango in paris] [http://zodip.info/description/primi-dettagli.htm primi dettagli su crackdown per xbox 360] [http://hangzhau.cn/html/rowenta-rh/ rowenta rh 511] [http://puli.org.cn/web/cosmetico-prodotti/ cosmetico prodotti bellezza] [http://zodip.info/description/amd-.htm amd 64 3200 socket754] [http://hangzhau.cn/html/altalex-it/ altalex it] [http://hangzhau.cn/html/nikon-/ nikon 80 200 d] [http://puli.org.cn/web/www-bellsouth/ www bellsouth com au] [http://nibko.info/data/secret-top.htm secret top] [http://italnet.net.cn/img/mary-poppins/ mary poppins film dvd] [http://italnet.net.cn/img/sidney-bechet/ sidney bechet] [http://nibko.info/data/motorini-maggiorenni.htm motorini maggiorenni] [http://nibko.info/data/emporio-armani.htm emporio armani abiti] [http://zodip.info/description/aneni.htm aneni] [http://hangzhau.cn/html/sungsuong-net/ sungsuong net] [http://italnet.net.cn/img/architettura-dei/ architettura dei calcolatori con] [http://puli.org.cn/web/www-il/ www il ritmo del sucesso com] [http://zodip.info/description/puzzle-bambino.htm puzzle bambino] [http://nibko.info/data/fiat-uno.htm fiat uno] [http://zodip.info/description/anastasia-outside.htm anastasia outside alone] [http://hangzhau.cn/html/tato-pe/ tato pe canta] [http://italnet.net.cn/img/the-nutcracker/ the nutcracker] [http://hangzhau.cn/html/blauwe-ruis/ blauwe ruis] [http://zodip.info/description/rharb.htm rharb] [http://hangzhau.cn/html/pista-de/ pista de reggaeton] [http://italnet.net.cn/img/parafrasi-pianto/ parafrasi pianto antico] [http://hangzhau.cn/html/halo-/ halo 2 xbox] [http://puli.org.cn/web/nipple-slip/ nipple slip] [http://nibko.info/data/www-buildabear.htm www buildabear com] [http://hangzhau.cn/html/giochi/ giochi 6630] [http://nibko.info/data/bond.htm bond] [http://italnet.net.cn/img/lettori-dvd/ lettori dvd casa] [http://italnet.net.cn/img/ilaria-d/ ilaria d amico] [http://hangzhau.cn/html/poltrona-relax/ poltrona relax massaggiante] [http://puli.org.cn/web/noleggio-auto/ noleggio auto morgan] [http://zodip.info/description/raffaello-creazioni.htm raffaello creazioni] [http://zodip.info/description/flat-internet.htm flat internet offerta] [http://puli.org.cn/web/camperisti/ camperisti] [http://italnet.net.cn/img/kenshiro/ kenshiro] [http://italnet.net.cn/img/to-much/ to much] [http://nibko.info/data/canzoni-francesi.htm canzoni francesi] [http://italnet.net.cn/img/www-girls/ www girls gone wild com] [http://italnet.net.cn/img/lago-di/ lago di iseo] [http://nibko.info/data/perche-sherlock.htm perche sherlock holmes fa uso di droghe] [http://hangzhau.cn/html/impianto/ impianto 5 1] [http://hangzhau.cn/html/hi-fi/ hi fi da muro] [http://nibko.info/data/camere-san.htm camere san gimignano] [http://italnet.net.cn/img/mizuno-atlantis/ mizuno atlantis] [http://nibko.info/data/imprese-farmaceutiche.htm imprese farmaceutiche ascoli piceno] [http://italnet.net.cn/img/morningstar/ morningstar] [http://zodip.info/description/bifid.htm bifid] [http://zodip.info/description/alcaline-torcia.htm alcaline torcia] [http://nibko.info/data/azienda-recupero.htm azienda recupero dato] [http://zodip.info/description/alkaid.htm alkaid] [http://zodip.info/description/tumeur-cerebrale.htm tumeur cerebrale] [http://zodip.info/description/gold-miner.htm gold miner.] [http://nibko.info/data/tritarifiuti.htm tritarifiuti] [http://nibko.info/data/caolinite.htm caolinite] [http://hangzhau.cn/html/rise-of/ rise of nations gold edition] [http://puli.org.cn/web/magnifico-hir/ magnifico hir aj kam hir a] [http://italnet.net.cn/img/motori-nautica/ motori nautica] [http://zodip.info/description/video-di.htm video di robbie williams] [http://zodip.info/description/www-blackbooty.htm www blackbooty com] [http://zodip.info/description/programmi-download.htm programmi download] [http://hangzhau.cn/html/flexell/ fle-x-ell] [http://nibko.info/data/cordless-rumblepad.htm cordless rumblepad da logitech] [http://puli.org.cn/web/comune-fagnano/ comune fagnano olona] [http://hangzhau.cn/html/the-reason/ the reason traduzione] [http://italnet.net.cn/img/parrini-distribuzione/ parrini distribuzione] [http://puli.org.cn/web/california-ristorante/ california ristorante] [http://zodip.info/description/nokia-.htm nokia 6600 vivavoce] [http://puli.org.cn/web/mass-media/ mass media] [http://zodip.info/description/best-of.htm best of van morrison] [http://puli.org.cn/web/piastra-di/ piastra di registrazione doppia] [http://nibko.info/data/gps-per.htm gps per symbian] [http://zodip.info/description/mostra-mercato.htm mostra mercato del disco treviso] [http://zodip.info/description/fax-copiatrice.htm fax copiatrice telefono] [http://puli.org.cn/web/e-pur/ e pur mi son scordato di te] [http://nibko.info/data/califoria-dreaming.htm califoria dreaming] [http://nibko.info/data/trincee.htm trincee] [http://nibko.info/data/subline-srl.htm subline srl] [http://hangzhau.cn/html/fick-august/ fick, august] [http://italnet.net.cn/img/great/ great] [http://hangzhau.cn/html/webcam-animali/ webcam animali] [http://hangzhau.cn/html/d-alessio/ d alessio giggi] [http://hangzhau.cn/html/sesso-tv/ sesso tv] [http://nibko.info/data/piatto-porcellana.htm piatto porcellana] [http://italnet.net.cn/img/sv-pioneer/ s-v50 pioneer] [http://nibko.info/data/time-splitters.htm time splitters future perfect] [http://hangzhau.cn/html/nu-i/ nu i alta femeie in lume] [http://zodip.info/description/giovan.htm giovan] [http://nibko.info/data/atz.htm atz] [http://nibko.info/data/cellulare-spia.htm cellulare spia] [http://zodip.info/description/toshiba-satellite.htm toshiba satellite p30 - 121] [http://italnet.net.cn/img/offerta-hotel/ offerta hotel sardegna] [http://hangzhau.cn/html/provincia-brescia/ provincia brescia it csa] [http://italnet.net.cn/img/aranea/ aranea] [http://hangzhau.cn/html/bruno-magli/ bruno magli] [http://nibko.info/data/boundage.htm boundage] [http://zodip.info/description/pasto-o.htm pasto o san juan de pasto] [http://puli.org.cn/web/valentina-uomini/ valentina uomini donne] [http://italnet.net.cn/img/paola-giovetti/ paola giovetti] [http://puli.org.cn/web/ibn-hawqal/ ibn hawqal] [http://zodip.info/description/suzuki-samurai.htm suzuki samurai usati] [http://zodip.info/description/tormento-d.htm tormento d amor] [http://italnet.net.cn/img/la-comunicazione/ la comunicazione il linguaggio comunica] [http://italnet.net.cn/img/musica-matrimonio/ musica matrimonio lombardia] [http://nibko.info/data/michelle-vieth.htm michelle vieth motel] [http://zodip.info/description/auricolari-bluetooth.htm auricolari bluetooth] [http://puli.org.cn/web/opel-meriva/ opel meriva 1 4] [http://italnet.net.cn/img/delonghi-magnifica/ delonghi magnifica] [http://hangzhau.cn/html/burn-this/ burn this house] [http://puli.org.cn/web/gay-incontri/ gay incontri] [http://puli.org.cn/web/samsung-monitor/ samsung monitor 19] [http://nibko.info/data/calendario-di.htm calendario di panorama] [http://hangzhau.cn/html/giorgio-armani/ giorgio armani calzature uomo] [http://zodip.info/description/dfence.htm d-fence 2] [http://puli.org.cn/web/salso/ salso] [http://zodip.info/description/rexton-auto.htm rexton auto] [http://nibko.info/data/hp.htm hp 817] [http://nibko.info/data/bella-e.htm bella e accessibile] [http://zodip.info/description/grazie-signore.htm grazie signore grazie] [http://zodip.info/description/matura-video.htm matura video] [http://italnet.net.cn/img/portale-aste/ portale aste] [http://italnet.net.cn/img/ngoc-ha/ ngoc ha] [http://puli.org.cn/web/jean-patou/ jean patou] [http://nibko.info/data/m-frame.htm m frame] [http://zodip.info/description/savan.htm savan] [http://puli.org.cn/web/a-tdi/ a3 tdi] [http://nibko.info/data/video-ritmo.htm video ritmo no pare] [http://italnet.net.cn/img/beffroy-de/ beffroy de reigny, louis-abel] [http://zodip.info/description/prostituzione.htm prostituzione] [http://italnet.net.cn/img/bernie-boulder/ bernie boulder] [http://nibko.info/data/testi-tommasini.htm testi tommasini] [http://puli.org.cn/web/jhon-cena/ jhon cena midi] [http://hangzhau.cn/html/v-motorola/ v525 motorola cellulari] [http://hangzhau.cn/html/sesso-donne/ sesso donne con cavalli] [http://puli.org.cn/web/nvidia-geforce/ nvidia geforce 6600 256mb pci-express tv] [http://hangzhau.cn/html/ricariche-gratis/ ricariche gratis] [http://puli.org.cn/web/pono-film/ pono film] [http://puli.org.cn/web/iriver/ iriver 256] [http://zodip.info/description/tamron-af.htm tamron af 17-35] [http://italnet.net.cn/img/calice-di/ calice di fuoco] [http://italnet.net.cn/img/liquido-campeggio/ liquido campeggio] [http://nibko.info/data/giorgia-ricciardi.htm giorgia ricciardi] [http://hangzhau.cn/html/ballerine-di/ ballerine di buona domenica] [http://hangzhau.cn/html/sesso-fra/ sesso fra animali] [http://puli.org.cn/web/mba-degree/ mba degree] [http://hangzhau.cn/html/prenotazione-albergo/ prenotazione albergo malaga] [http://zodip.info/description/o-zone.htm o zone printre nori] [http://italnet.net.cn/img/kjarkas/ kjarkas] [http://puli.org.cn/web/chris/ chris] [http://nibko.info/data/un-altro.htm un altro modem per la playstation 2] [http://puli.org.cn/web/gidget-a/ gidget a roma] [http://italnet.net.cn/img/subliminal-verses/ subliminal verses] [http://hangzhau.cn/html/copertina-per/ copertina per cd festival bar 2004] [http://nibko.info/data/idea-fiat.htm idea fiat] [http://italnet.net.cn/img/lg-dr/ lg dr7400] [http://nibko.info/data/jeep-cherokee.htm jeep cherokee limited 2 5] [http://puli.org.cn/web/p-pio/ p pio] [http://puli.org.cn/web/pannolini-per/ pannolini per bambini] [http://hangzhau.cn/html/portatile-hp/ portatile hp cel] [http://zodip.info/description/pollo-al.htm pollo al curry] [http://italnet.net.cn/img/lo-strano/ lo strano triangolo] [http://zodip.info/description/r-inkjet.htm r300 inkjet epson stampanti] [http://hangzhau.cn/html/costi-cellulari/ costi cellulari samsung] [http://hangzhau.cn/html/ferro-da/ ferro da stiro amstrad] [http://puli.org.cn/web/softwer/ softwer] [http://zodip.info/description/psp-batteria.htm psp batteria] [http://nibko.info/data/rilegatrici-a.htm rilegatrici a3] [http://nibko.info/data/appartamento-san.htm appartamento san teodoro] [http://nibko.info/data/pe.htm pe 8700] [http://puli.org.cn/web/pasion-de/ pasion de sabado] [http://nibko.info/data/vital-dent.htm vital dent] [http://zodip.info/description/monitor-.htm monitor 15 tft] [http://hangzhau.cn/html/via-gambirasio/ via gambirasio] [http://italnet.net.cn/img/il-prigioniero/ il prigioniero di me stesso] [http://nibko.info/data/snoop-doggy.htm snoop doggy dog] [http://hangzhau.cn/html/granada-nicaragua/ granada (nicaragua)] [http://nibko.info/data/sony-walkman.htm sony walkman 20gb] [http://italnet.net.cn/img/stampanti-hp/ stampanti hp designjet 100] [http://puli.org.cn/web/usa-ultimo/ usa ultimo decapitato filmato] [http://hangzhau.cn/html/dragostea-din/ dragostea din tel] [http://italnet.net.cn/img/ragazza-san/ ragazza san gimignano] [http://italnet.net.cn/img/smu-bugil/ smu bugil] [http://puli.org.cn/web/football-com/ football com] [http://nibko.info/data/musica-e.htm musica e spettacolo da ace combat 5] [http://zodip.info/description/roch.htm roch] [http://zodip.info/description/grn-turismo.htm grn turismo 2] [http://italnet.net.cn/img/ingranaggio-orologio/ ingranaggio orologio] [http://italnet.net.cn/img/veritas-backup/ veritas backup exec 10 0] [http://puli.org.cn/web/www-/ www 191 biz] [http://hangzhau.cn/html/name-is/ name is tonino film dvd] [http://nibko.info/data/telefono-cordless.htm telefono cordless siemens segreteria] [http://zodip.info/description/memorie-ram.htm memorie ram vitesta] [http://nibko.info/data/d-body.htm d50 body silver sd 256] [http://zodip.info/description/donne-inculate.htm donne inculate da cani] {{task}} |
|||
{{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" |
||
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 |