Towers of Hanoi: Difference between revisions

m
no edit summary
(revert spam)
mNo edit summary
Line 1:
[http://nuovi-annunci.cleric4u.info nuovi annunci] [http://maheinze.airsofthost.com/data/jabra-bt.htm jabra bt800 bluetooth] [http://baldan.cleric4u.info baldan] [http://lejenny.airsofthost.com/library/html/seggiolino-auto.htm seggiolino auto] [http://splucind.airsofthost.com/content/view/toschiba-satellite/ toschiba satellite] [http://alpaddy.airsofthost.com/img/styles/storie-vere/ storie vere erotiche] [http://alpaddy.airsofthost.com/img/styles/mutuo-fisso/ mutuo fisso] [http://menongue.giantlau.info menongue] [http://hdurian.airsofthost.com/view/new/nikon.htm nikon 85] [http://maheinze.airsofthost.com/data/hp-mfp.htm hp 4100mfp] [http://maheinze.airsofthost.com/data/porque-floricienta.htm porque floricienta] [http://rumote.airsofthost.com/lib/paracadutista-tu.htm paracadutista tu folgore] [http://hdurian.airsofthost.com/view/new/calendario-poster.htm calendario poster 2005 fox] [http://frgrossm.airsofthost.com/topic/arta-terme/ arta terme] [http://maheinze.airsofthost.com/data/mp-smackdown.htm mp3 smackdown] [http://frgrossm.airsofthost.com/topic/snowboarder/ snowboarder] [http://synthetic-pelle.otrev4ro.info synthetic pelle] [http://moto-honda.cleric4u.info moto honda silver wing 600] [http://sapphire-radeon.firethro.info sapphire radeon x700] [http://maheinze.airsofthost.com/data/puffy-nipples.htm puffy nipples] [http://laortiz.airsofthost.com/view/new/kawasaki-verona/ kawasaki verona] [http://hp-.cleric4u.info hp 1100 ciano] [http://banzlees.airsofthost.com/description/lib/win-tv.htm win tv pro free] [http://splucind.airsofthost.com/content/view/trasformatore-spina/ trasformatore spina] [http://splucind.airsofthost.com/content/view/x-black/ x black sony] [http://gsm-gprs.otrev4ro.info gsm gprs sd] [http://roleplayinggames-com.monbrenno.info roleplayinggames com] [http://hdurian.airsofthost.com/view/new/disney-svela.htm disney svela il 2004] [http://rumote.airsofthost.com/lib/nike-fsm.htm nike fsm] [http://frgrossm.airsofthost.com/topic/volo-aereo/ volo aereo per malaga] [http://remayfuy.airsofthost.com/images/small/inno-firentina/ inno firentina] [http://immagini-olocausto.otrev4ro.info immagini olocausto ebrei] [http://lejenny.airsofthost.com/library/html/otomo-katsuhiro.htm otomo katsuhiro] [http://remayfuy.airsofthost.com/images/small/logitech/ logitech 680] [http://carmelo-zappulla.firethro.info carmelo zappulla napoli] [http://hdurian.airsofthost.com/view/new/gigaset-a.htm gigaset a 2] [http://maheinze.airsofthost.com/data/marathon-winter.htm marathon winter] [http://remayfuy.airsofthost.com/images/small/nicolas-peyrac/ nicolas peyrac] [http://banzlees.airsofthost.com/description/lib/francesca-comencini.htm francesca comencini] [http://banzlees.airsofthost.com/description/lib/www-ciee.htm www ciee org br] [http://lejenny.airsofthost.com/library/html/i-am.htm i am dreaming of you tonight] [http://banzlees.airsofthost.com/description/lib/caldonazzo.htm caldonazzo] [http://nuovi-tv.giantlau.info nuovi tv al plasma] [http://reynosa.cleric4u.info reynosa] [http://computer-service.monbrenno.info computer service] [http://latin-dreams.otrev4ro.info latin dreams] [http://remayfuy.airsofthost.com/images/small/nike-/ nike waffle racer ii] [http://televisore-.cleric4u.info televisore 32 samsung] [http://frgrossm.airsofthost.com/topic/paltrow-pitt/ paltrow pitt] [http://frgrossm.airsofthost.com/topic/modulo-continuo/ modulo continuo] [http://autorita-garante.giantlau.info autorita garante comunicazioni] [http://frgrossm.airsofthost.com/topic/jbl-gtoe/ jbl gto936e] [http://splucind.airsofthost.com/content/view/il-ladro/ il ladro di crimini] [http://maheinze.airsofthost.com/data/testo-in.htm testo in italiano della canzone fuck it] [http://sauris.firethro.info sauris] [http://marvel-louis.otrev4ro.info marvel louis] [http://banzlees.airsofthost.com/description/lib/epson-c.htm epson c9100] [http://rumote.airsofthost.com/lib/hd-.htm hd 5 gb] [http://frgrossm.airsofthost.com/topic/i-love/ i love you] [http://netgear-wireless.cleric4u.info netgear wireless router] [http://bombola-butano.otrev4ro.info bombola butano] [http://foto-nude.otrev4ro.info foto nude isola famosi] [http://suono-stereo.cleric4u.info suono stereo hi fi] [http://lejenny.airsofthost.com/library/html/nvidia-quadro.htm nvidia quadro fx] [http://the-.firethro.info the 411 on my knees] [http://laortiz.airsofthost.com/view/new/vacanza-cina/ vacanza cina] [http://frgrossm.airsofthost.com/topic/ragazze-escort/ ragazze escort ancona] [http://karate-calabria.cleric4u.info karate calabria] [http://banzlees.airsofthost.com/description/lib/microfono-a.htm microfono a cuffia] [http://hdurian.airsofthost.com/view/new/waidhofen-an.htm waidhofen an der ybbs] [http://maheinze.airsofthost.com/data/anelli-di.htm anelli di perle] [http://rumote.airsofthost.com/lib/villa-a.htm villa a da posse] [http://kt-neof.giantlau.info k8t neo2f socket 939 amd chipset] [http://splucind.airsofthost.com/content/view/canton-le/ canton le 190] [http://la-lunga.otrev4ro.info la lunga notte di lucignolo 2004] [http://singola-di.cleric4u.info singola di udine] [http://veneto-foto.giantlau.info veneto foto inquinamento mare] [http://pasella.firethro.info pasella] [http://wilson-hyper.otrev4ro.info wilson hyper] [http://fiat-uno.firethro.info fiat uno fire] [http://stereophonics-a.cleric4u.info stereophonics a day at the races] [http://maheinze.airsofthost.com/data/polenz-wilhelm.htm polenz wilhelm] [http://casting-.monbrenno.info casting 2004 per veline] [http://hdurian.airsofthost.com/view/new/too-fast.htm too fast too curious] [http://splucind.airsofthost.com/content/view/www-ideepthroat/ www ideepthroat com] [http://md.otrev4ro.info md 006] [http://rumote.airsofthost.com/lib/steckborn.htm steckborn] [http://adoption-agency.otrev4ro.info adoption agency] [http://laortiz.airsofthost.com/view/new/segreto-universo/ segreto universo] [http://oqo.monbrenno.info oqo 01] [http://banzlees.airsofthost.com/description/lib/schema-citofono.htm schema citofono urmet] [http://frgrossm.airsofthost.com/topic/chupacabras/ chupacabras] [http://hdurian.airsofthost.com/view/new/elplan.htm elplan] [http://fabio-lety.otrev4ro.info fabio lety] [http://live-coliseo.otrev4ro.info live coliseo] [http://banzlees.airsofthost.com/description/lib/nu-virgo.htm nu virgo stop stop] [http://hdurian.airsofthost.com/view/new/www-ilsoleore.htm www ilsole24ore] [http://amd-.otrev4ro.info amd 64 portatile 1gb] [http://rumote.airsofthost.com/lib/ram-kingston.htm ram kingston 533mhz 512mb] [http://kenwood-krf.firethro.info kenwood krf v5090d] [http://lejenny.airsofthost.com/library/html/esecuzione-amaricano.htm esecuzione amaricano] [http://alpaddy.airsofthost.com/img/styles/sony-dvpns/ sony dvpns955] [http://lejenny.airsofthost.com/library/html/dlink-dgl.htm dlink dgl4300] [http://x-trail.giantlau.info x trail km 0] [http://gay-in.cleric4u.info gay in 3] [http://ragazzo-caserta.firethro.info ragazzo caserta] [http://laortiz.airsofthost.com/view/new/la-mia/ la mia liberta] [http://maheinze.airsofthost.com/data/ferrari-.htm ferrari 360 f1] [http://www-pokemon.cleric4u.info www pokemon porno com] [http://lejenny.airsofthost.com/library/html/riassunto-dell.htm riassunto dell iliade] [http://maheinze.airsofthost.com/data/www-wayne.htm www wayne edu] [http://bad-boy.otrev4ro.info bad boy 2] [http://webcam-live.cleric4u.info webcam live pro] [http://pelo-rosso.giantlau.info pelo rosso it] [http://avast-it.firethro.info avast it] [http://maheinze.airsofthost.com/data/corso-da.htm corso da fonico] [http://frgrossm.airsofthost.com/topic/fidelitas/ fidelitas] [http://wp-pentax.cleric4u.info wp pentax] [http://mare-foto.giantlau.info mare foto sardegna] [http://plotter-stampa.otrev4ro.info plotter stampa] [http://maheinze.airsofthost.com/data/hard-disk.htm hard disk portatili] [http://alpaddy.airsofthost.com/img/styles/pc-park/ pc park] [http://meteor-man.monbrenno.info meteor man] [http://splucind.airsofthost.com/content/view/cartina-del/ cartina del trentino alto adige] [http://ti-amo.cleric4u.info ti amo ci siamo ritrovati ma non e giust] [http://banzlees.airsofthost.com/description/lib/servizio-civile.htm servizio civile it] [http://nuova-fiat.otrev4ro.info nuova fiat diesel auto km 0] [http://the-bone.cleric4u.info the bone snatcher cacciatore di ossa] [http://obbligazioni-impresa.cleric4u.info obbligazioni impresa] [http://kit-per.firethro.info kit per notebook] [http://frgrossm.airsofthost.com/topic/t-es/ t es nul] [http://rumote.airsofthost.com/lib/edoardo-pozzi.htm edoardo pozzi libri] [http://hdurian.airsofthost.com/view/new/www-telecom.htm www telecom italia com] [http://frgrossm.airsofthost.com/topic/humping/ humping] [http://rumote.airsofthost.com/lib/lima-modelli.htm lima modelli ferroviari] [http://kareen.firethro.info kareen] [http://lejenny.airsofthost.com/library/html/ernia-ombelicale.htm ernia ombelicale] [http://frgrossm.airsofthost.com/topic/cos-e/ cos e il clima] [http://rumote.airsofthost.com/lib/computer-con.htm computer con office basic] [http://lejenny.airsofthost.com/library/html/il-caldo.htm il caldo amore di evelyn] [http://touch-me.giantlau.info touch me tease me] [http://rumote.airsofthost.com/lib/arriano.htm arriano] [http://lejenny.airsofthost.com/library/html/dream-theater.htm dream theater live video] [http://remayfuy.airsofthost.com/images/small/panasonic-pm/ panasonic 28pm11] [http://frgrossm.airsofthost.com/topic/spazio-/ spazio 1999 stagione 2 vol 2] [http://www-barney.giantlau.info www barney com] [http://hdurian.airsofthost.com/view/new/la-battaglia.htm la battaglia del talismano verde] [http://frgrossm.airsofthost.com/topic/midland-gxt/ midland gxt500] [http://remayfuy.airsofthost.com/images/small/giuseppe-verdi/ giuseppe verdi i lombardi alla prima crociata] [http://alpaddy.airsofthost.com/img/styles/paul-mccartney/ paul mccartney video] [http://lejenny.airsofthost.com/library/html/magix-mp.htm magix mp3 maker] [http://alpaddy.airsofthost.com/img/styles/gth/ gth] [http://remayfuy.airsofthost.com/images/small/cacate-in/ cacate in faccia] [http://champion-leage.cleric4u.info champion leage] [http://lejenny.airsofthost.com/library/html/castello-di.htm castello di azzurrina] [http://alpaddy.airsofthost.com/img/styles/memorie-ddr/ memorie ddr ii] [http://remayfuy.airsofthost.com/images/small/date-del/ date del tour estivo di tiziano ferro] [http://panasonic-lumix.otrev4ro.info panasonic lumix dmclc33] [http://rumote.airsofthost.com/lib/tucson-km.htm tucson km zero] [http://remayfuy.airsofthost.com/images/small/cuando-me/ cuando me miras asi] [http://controller-firewire.giantlau.info controller firewire 800] [http://laortiz.airsofthost.com/view/new/maxtor-gb/ maxtor 250gb 16mb] [http://toner-canon.cleric4u.info toner canon 900] [http://alpaddy.airsofthost.com/img/styles/proliant-ml/ proliant ml110] [http://rumote.airsofthost.com/lib/massaggiatrici-bologna.htm massaggiatrici bologna] [http://rumote.airsofthost.com/lib/scaffalature-metalliche.htm scaffalature metalliche prezzi] [http://alpaddy.airsofthost.com/img/styles/nijmegen/ nijmegen] [http://banzlees.airsofthost.com/description/lib/logitech-.htm logitech 250 usb] [http://frgrossm.airsofthost.com/topic/groizer-x/ groizer x] [http://maheinze.airsofthost.com/data/articoli-per.htm articoli per dj napoli] [http://splucind.airsofthost.com/content/view/giochi-di/ giochi di dinosauri] [http://diesel-km.cleric4u.info diesel km 0] [http://radio-deejai.monbrenno.info radio deejai] [http://banzlees.airsofthost.com/description/lib/ana-cirre.htm ana cirre] [http://rumote.airsofthost.com/lib/shnauzer.htm shnauzer] [http://hack-quarantine.cleric4u.info hack quarantine] [http://banzlees.airsofthost.com/description/lib/pooh-io.htm pooh io ti aspettero] [http://lejenny.airsofthost.com/library/html/grand-dvb.htm grand dvb] [http://remayfuy.airsofthost.com/images/small/dizionario-inglese/ dizionario inglese] [http://splucind.airsofthost.com/content/view/www-wrestlemania/ www wrestlemania it] [http://remayfuy.airsofthost.com/images/small/gioco-gratis/ gioco gratis pc] [http://finanziamento-auto.otrev4ro.info finanziamento auto online] [http://splucind.airsofthost.com/content/view/attrici-straniere/ attrici straniere] [http://camping-gargano.cleric4u.info camping gargano] [http://emanuelle-e.firethro.info emanuelle e gli ultimi cannibali] [http://banzlees.airsofthost.com/description/lib/agriturismo-carpi.htm agriturismo carpi] [http://copy-any.cleric4u.info copy any dvd movie] [http://bung.otrev4ro.info bung] [http://trust-energy.monbrenno.info trust energy protector] [http://laortiz.airsofthost.com/view/new/when-we/ when we danced] [http://decapitazione-soldato.cleric4u.info decapitazione soldato americano] [http://maheinze.airsofthost.com/data/sophie-bextor.htm sophie bextor] [http://young-guns.cleric4u.info young guns giovani pistole] [http://remayfuy.airsofthost.com/images/small/feet-smell/ feet smell] [http://laortiz.airsofthost.com/view/new/modern-talking/ modern talking] [http://maheinze.airsofthost.com/data/frenc-aventura.htm frenc aventura] [http://alpaddy.airsofthost.com/img/styles/kodak-easyshare/ kodak easyshare c360] [http://lejenny.airsofthost.com/library/html/matteucci.htm matteucci] [http://eurobarre.cleric4u.info eurobarre] [http://laortiz.airsofthost.com/view/new/counting/ counting] {{task}}
{{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
Anonymous user