Towers of Hanoi: Difference between revisions
Content deleted Content added
Undo revision 8085 by Special:Contributions/On5M1p (User talk:On5M1p) |
mNo edit summary |
||
Line 1: | Line 1: | ||
[http://mania555.ifrance.com/img/styles/bon-jovi.htm bon jovi its my life] [http://itsmeladuh.ifrance.com/content/view/siemens-/ siemens gigaset s100] [http://reikofanel.ifrance.com/web/bifid.htm bifid] [http://sarah531.ifrance.com/lib/cruscotto-audi.htm cruscotto audi a3] [http://reikofanel.ifrance.com/web/il-terrore.htm il terrore di chicago] [http://blackblackblack.ifrance.com/lib/richard-strauss.htm richard strauss arabella] [http://asing-argirl.ifrance.com/images/small/sborra-sulla/ sborra sulla faccia] [http://itsmeladuh.ifrance.com/content/view/bebe-confort/ bebe confort elios] [http://dgdeeper.ifrance.com/img/styles/abit-is.htm abit is10] [http://toupheefee.ifrance.com/text/directory/harasymowicz-jerzy/ harasymowicz jerzy] [http://soundwave106.ifrance.com/resources/articles/doninelli/ doninelli] [http://csmith06.ifrance.com/lib/sable-diva.htm sable diva del wrestling] [http://soundwave106.ifrance.com/resources/articles/gays-kids/ gays kids] [http://soundwave106.ifrance.com/resources/articles/asus-ati/ asus ati 9550] [http://sarah531.ifrance.com/lib/divx-.htm divx 40 gb] [http://itsmeladuh.ifrance.com/content/view/campeggio-lago/ campeggio lago di garda] [http://dgdeeper.ifrance.com/img/styles/siemens-gigaset.htm siemens gigaset dect] [http://twobit77.ifrance.com/description/lib/una-data/ una data per ollie king] [http://bang-sigh.ifrance.com/images/small/un-posto/ un posto nel mondo] [http://dgdeeper.ifrance.com/img/styles/elkann.htm elkann] [http://reikofanel.ifrance.com/web/belinda-peregrin.htm belinda peregrin schull] [http://hethrfethr77.ifrance.com/content/view/concessionaria-auto/ concessionaria auto localita formia] [http://bang-sigh.ifrance.com/images/small/becas/ becas] [http://kirizz.ifrance.com/html/pianta-rampicante.htm pianta rampicante] [http://itsmeladuh.ifrance.com/content/view/so-yesterday/ so yesterday di hilary duff] [http://kirizz.ifrance.com/html/ati-xse.htm ati x800se] [http://soundwave106.ifrance.com/resources/articles/nardi-piani/ nardi piani cottura 90] [http://kirizz.ifrance.com/html/zoophycos.htm zoophycos] [http://kiravina.ifrance.com/html/trova-hotel.htm trova hotel] [http://twobit77.ifrance.com/description/lib/dajla/ dajla] [http://twobit77.ifrance.com/description/lib/tv-monitor/ tv monitor 26] [http://blackblackblack.ifrance.com/lib/when-i.htm when i get] [http://twobit77.ifrance.com/description/lib/noleggiobarche/ noleggiobarche] [http://blackblackblack.ifrance.com/lib/spiaggia.htm spiaggia] [http://sarah531.ifrance.com/lib/volkswagen-passat.htm volkswagen passat 1900 tdi 130 cv] [http://sarah531.ifrance.com/lib/gassi.htm gassi] [http://bladiblada.ifrance.com/resources/articles/scarica-suonerie.htm scarica suonerie] [http://soundwave106.ifrance.com/resources/articles/gogle-con/ gogle con] [http://itsmeladuh.ifrance.com/content/view/ojos-asi/ ojos asi shakira] [http://kirizz.ifrance.com/html/omicidio-americano.htm omicidio americano] [http://blackblackblack.ifrance.com/lib/titolo-cd.htm titolo cd tiziano ferro] [http://soundwave106.ifrance.com/resources/articles/design-della/ design della comunicazione] [http://soundwave106.ifrance.com/resources/articles/mezzo-giorno/ mezzo giorno di fuoco] [http://itsmeladuh.ifrance.com/content/view/maglierie-frosinone/ maglierie frosinone] [http://bang-sigh.ifrance.com/images/small/logitech-cordless/ logitech cordless desktop oem] [http://twobit77.ifrance.com/description/lib/povera-sora/ povera sora] [http://csmith06.ifrance.com/lib/supersonic.htm supersonic] [http://kiravina.ifrance.com/html/i-pooh.htm i pooh ascolta] [http://csmith06.ifrance.com/lib/wet-t.htm wet t shirt] [http://sarah531.ifrance.com/lib/costantino-vitigliano.htm costantino vitigliano il calendario] [http://nettikyzarc.ifrance.com/content/view/christmast-card/ christmast card vodafone] [http://hethrfethr77.ifrance.com/content/view/capitello/ capitello] [http://soundwave106.ifrance.com/resources/articles/el/ el 8] [http://toupheefee.ifrance.com/text/directory/monitor-ultrasharp/ monitor ultrasharp] [http://itsmeladuh.ifrance.com/content/view/ngocha/ ngocha] [http://csmith06.ifrance.com/lib/pannelli-isolanti.htm pannelli isolanti] [http://reikofanel.ifrance.com/web/pilot-flight.htm pilot flight] [http://soundwave106.ifrance.com/resources/articles/nooit/ nooit] [http://bladiblada.ifrance.com/resources/articles/europa-contro.htm europa contro italia] [http://dgdeeper.ifrance.com/img/styles/toshiba-satellite.htm toshiba satellite m40 232] [http://faithib.ifrance.com/description/lib/fetival-bar.htm fetival bar 2004] [http://nettikyzarc.ifrance.com/content/view/super-mop/ super mop] [http://hethrfethr77.ifrance.com/content/view/dimagrire-velocemente/ dimagrire velocemente] [http://mania555.ifrance.com/img/styles/basura.htm basura] [http://asing-argirl.ifrance.com/images/small/daewoo-fso/ daewoo fso] [http://itsmeladuh.ifrance.com/content/view/orologiaio/ orologiaio] [http://nettikyzarc.ifrance.com/content/view/incontro-donna/ incontro donna dell est] [http://twobit77.ifrance.com/description/lib/grand-theft/ grand theft auto vice city] [http://blackblackblack.ifrance.com/lib/shane-owen.htm shane owen feel] [http://itsmeladuh.ifrance.com/content/view/franceville/ franceville] [http://bladiblada.ifrance.com/resources/articles/high-power.htm high power] [http://twobit77.ifrance.com/description/lib/lionel/ lionel] [http://bladiblada.ifrance.com/resources/articles/feri-da.htm feri da stiro a vapore imetec] [http://sarah531.ifrance.com/lib/toner-hp.htm toner hp color laserjet 2550l] [http://sarah531.ifrance.com/lib/oxford-university.htm oxford university] [http://sarah531.ifrance.com/lib/philips.htm philips 825] [http://itsmeladuh.ifrance.com/content/view/jennifer-lopez/ jennifer lopez all i have] [http://alvonina.ifrance.com/img/styles/frasi-auguri/ frasi auguri di lavoro] [http://reikofanel.ifrance.com/web/alfa-romeo.htm alfa romeo 146 17 16v l] [http://dgdeeper.ifrance.com/img/styles/e-.htm e 1000 batteria motorola] [http://kiravina.ifrance.com/html/ottico-ricaricabile.htm ottico ricaricabile mouse e tastiere] [http://kiravina.ifrance.com/html/windows-xp.htm windows xp sistema operativo] [http://toupheefee.ifrance.com/text/directory/ups/ ups] [http://kiravina.ifrance.com/html/salentino.htm salentino] [http://alvonina.ifrance.com/img/styles/vanno-lindo/ vanno lindo] [http://nettikyzarc.ifrance.com/content/view/lac/ lac] [http://faithib.ifrance.com/description/lib/punzonatrice.htm punzonatrice] [http://nettikyzarc.ifrance.com/content/view/monitor-dv/ monitor dv] [http://csmith06.ifrance.com/lib/vivavoce-motorola.htm vivavoce motorola c350] [http://alvonina.ifrance.com/img/styles/nilo-azzurro/ nilo azzurro] [http://faithib.ifrance.com/description/lib/caffe-macchina.htm caffe macchina] [http://asing-argirl.ifrance.com/images/small/dizionario-italiano/ dizionario italiano olandese] [http://asing-argirl.ifrance.com/images/small/ironside/ ironside] [http://dgdeeper.ifrance.com/img/styles/dianium.htm dianium] [http://toupheefee.ifrance.com/text/directory/diesel-autocarro/ diesel autocarro] [http://kiravina.ifrance.com/html/taj-mahal.htm taj mahal salon de bahia] [http://faithib.ifrance.com/description/lib/daddy-yanke.htm daddy yanke] [http://faithib.ifrance.com/description/lib/azione-comprare.htm azione comprare] [http://mania555.ifrance.com/img/styles/samsung-sgh.htm samsung sgh z140] [http://faithib.ifrance.com/description/lib/una-demo.htm una demo per airport tycoon 2] [http://csmith06.ifrance.com/lib/laois.htm laois] [http://twobit77.ifrance.com/description/lib/rio-negro/ rio negro] [http://blackblackblack.ifrance.com/lib/fes-marocco.htm fes marocco] [http://crazybird027.ifrance.com/view/new/canon-ef/ canon ef is usm] [http://alvonina.ifrance.com/img/styles/batterie-/ batterie 2300 mah] [http://kiravina.ifrance.com/html/alberto-rossi.htm alberto rossi srl] [http://reikofanel.ifrance.com/web/hammamet-yasmine.htm hammamet yasmine alberghi] [http://itsmeladuh.ifrance.com/content/view/prestito-cesenatico/ prestito cesenatico] [http://bang-sigh.ifrance.com/images/small/rom-track/ rom track] [http://itsmeladuh.ifrance.com/content/view/whit-or/ whit or whithout yu] [http://nettikyzarc.ifrance.com/content/view/yaga-y/ yaga y mackie] [http://alvonina.ifrance.com/img/styles/emanuela-folliero/ emanuela folliero 2005] [http://dgdeeper.ifrance.com/img/styles/video-porno.htm video porno grtis] [http://twobit77.ifrance.com/description/lib/maciulla/ maciulla] [http://faithib.ifrance.com/description/lib/blackang.htm blackang] [http://nettikyzarc.ifrance.com/content/view/dvx/ dvx 2600] [http://crazybird027.ifrance.com/view/new/i-sogni/ i sogni nel cassetto] [http://crazybird027.ifrance.com/view/new/soubresaut/ soubresaut] [http://asing-argirl.ifrance.com/images/small/o-zone/ o zone romanian dragostea din] [http://reikofanel.ifrance.com/web/kyle-maclahan.htm kyle maclahan] [http://kirizz.ifrance.com/html/hard-disc.htm hard disc interno] [http://sarah531.ifrance.com/lib/noura.htm noura] [http://nettikyzarc.ifrance.com/content/view/jaguar-s/ jaguar s type 27 d] [http://twobit77.ifrance.com/description/lib/conexant-softk/ conexant softk56 data fax pci modem] [http://twobit77.ifrance.com/description/lib/sudi-noi/ sudi noi pupo] [http://bang-sigh.ifrance.com/images/small/spartiti-pinocchio/ spartiti pinocchio] [http://itsmeladuh.ifrance.com/content/view/smeg-/ smeg 53 ct] [http://dgdeeper.ifrance.com/img/styles/vamos-a.htm vamos a bailar esta vida nueva] [http://kiravina.ifrance.com/html/escape-from.htm escape from house] [http://kiravina.ifrance.com/html/omoiga.htm omoiga] [http://twobit77.ifrance.com/description/lib/accessori-compaq/ accessori compaq] [http://crazybird027.ifrance.com/view/new/www-mundo/ www mundo nick con] [http://dgdeeper.ifrance.com/img/styles/canon-nb.htm canon nb 1lh batteria] [http://asing-argirl.ifrance.com/images/small/volare-vola/ volare vola re volare] [http://sarah531.ifrance.com/lib/sparkle-geforce.htm sparkle geforce fx5200] [http://nettikyzarc.ifrance.com/content/view/residence-mare/ residence mare italia] [http://sarah531.ifrance.com/lib/milano-ibiza.htm milano ibiza biglietti aerei] [http://kirizz.ifrance.com/html/auto-usata.htm auto usata sardegna] [http://nettikyzarc.ifrance.com/content/view/salma-hayek/ salma hayek nuda] [http://nettikyzarc.ifrance.com/content/view/cross-ktm/ cross ktm 125] [http://faithib.ifrance.com/description/lib/matt.htm matt] [http://dgdeeper.ifrance.com/img/styles/bed-breakfast.htm bed breakfast germania] [http://dgdeeper.ifrance.com/img/styles/shuttle-accessori.htm shuttle accessori] [http://itsmeladuh.ifrance.com/content/view/fishermansam/ fishermansam] [http://asing-argirl.ifrance.com/images/small/thiess-frank/ thiess frank] [http://bladiblada.ifrance.com/resources/articles/alda-merini.htm alda merini diario] [http://toupheefee.ifrance.com/text/directory/dragonballxxx-gratis/ dragonballxxx gratis] [http://itsmeladuh.ifrance.com/content/view/parcheggi/ parcheggi] [http://csmith06.ifrance.com/lib/ladra-del.htm ladra del vento] [http://asing-argirl.ifrance.com/images/small/msi-megabook/ msi megabook s250] [http://dgdeeper.ifrance.com/img/styles/donne-senza.htm donne senza mutande] [http://soundwave106.ifrance.com/resources/articles/assorbente-savona/ assorbente savona] [http://asing-argirl.ifrance.com/images/small/breaking/ breaking] [http://nettikyzarc.ifrance.com/content/view/iwo-jima/ iwo jima film dvd] [http://faithib.ifrance.com/description/lib/silent-hill.htm silent hill] [http://csmith06.ifrance.com/lib/freds.htm freds] [http://twobit77.ifrance.com/description/lib/stand-by/ stand by me lennon] [http://csmith06.ifrance.com/lib/girl-sex.htm girl sex] [http://twobit77.ifrance.com/description/lib/trasporti-monza/ trasporti monza] [http://sarah531.ifrance.com/lib/phangnga-provincia.htm phangnga (provincia)] [http://mania555.ifrance.com/img/styles/flasc-game.htm flasc game] [http://reikofanel.ifrance.com/web/tartarughe-terrestri.htm tartarughe terrestri italiane] [http://dgdeeper.ifrance.com/img/styles/nn.htm nn] [http://mania555.ifrance.com/img/styles/jakki-degg.htm jakki degg] [http://crazybird027.ifrance.com/view/new/kiki-pereira/ kiki pereira] [http://twobit77.ifrance.com/description/lib/solo-moto/ solo moto] [http://twobit77.ifrance.com/description/lib/bruce-lee/ bruce lee contro i supermen] [http://kiravina.ifrance.com/html/wallpaper-cycler.htm wallpaper cycler 21] [http://sarah531.ifrance.com/lib/nike-brasil.htm nike brasil] [http://dgdeeper.ifrance.com/img/styles/ram-kit.htm ram kit] [http://reikofanel.ifrance.com/web/sylwester-.htm sylwester 2004 2005 propozycje] [http://alvonina.ifrance.com/img/styles/driver-mpu/ driver mpu 401] [http://crazybird027.ifrance.com/view/new/due-per/ due per la strada audrey hepburn film] [http://faithib.ifrance.com/description/lib/fifa.htm fifa 96] [http://sarah531.ifrance.com/lib/musiani-enrico.htm musiani enrico] [http://dgdeeper.ifrance.com/img/styles/vigile-del.htm vigile del fuoco pompieri] [http://sarah531.ifrance.com/lib/personaggio-televisivi.htm personaggio televisivi] [http://faithib.ifrance.com/description/lib/credit.htm credit] [http://alvonina.ifrance.com/img/styles/maharaja/ maharaja] [http://alvonina.ifrance.com/img/styles/salumi/ salumi] [http://blackblackblack.ifrance.com/lib/camila-kiss.htm camila kiss nua] [http://kiravina.ifrance.com/html/camera-ragusa.htm camera ragusa] [http://soundwave106.ifrance.com/resources/articles/monna-lisa/ monna lisa] [http://twobit77.ifrance.com/description/lib/corsair-cmx/ corsair cmx 3200xl] [http://alvonina.ifrance.com/img/styles/combo-vhs/ combo vhs dvd] [http://kiravina.ifrance.com/html/plasma-nec.htm plasma nec 50] [http://kiravina.ifrance.com/html/agriturismo-granada.htm agriturismo granada] [http://hethrfethr77.ifrance.com/content/view/lisa-gerard/ lisa gerard peter burke] [http://hethrfethr77.ifrance.com/content/view/sony/ sony 5 1] [http://asing-argirl.ifrance.com/images/small/corso-java/ corso java] [http://crazybird027.ifrance.com/view/new/disegno-da/ disegno da colorare] [http://twobit77.ifrance.com/description/lib/pro-form/ pro form 560] [http://asing-argirl.ifrance.com/images/small/epson-cinghia/ epson cinghia di trasferimento] [http://hethrfethr77.ifrance.com/content/view/lifegate/ lifegate] [http://kiravina.ifrance.com/html/soddr-mb.htm soddr 512mb] [http://twobit77.ifrance.com/description/lib/nuovi-filmati/ nuovi filmati per nba live 2004] [http://hethrfethr77.ifrance.com/content/view/complanno-auguri/ complanno auguri] [http://csmith06.ifrance.com/lib/zelig-salsa.htm zelig salsa] [http://nettikyzarc.ifrance.com/content/view/m-v/ m v s n] [http://itsmeladuh.ifrance.com/content/view/lettore-cd/ lettore cd mp3 portatile thomson] {{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 |