Towers of Hanoi: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(revert spam)
Line 1: Line 1:
{{task}}
[http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30278 cheap nexium] [http://wc1.worldcrossing.com/WebX/.1de60a20 cheap nexium] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30317 cheap zanaflex] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30333 free funny ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1377 free samsung ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a2b but ultracet] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=99 cheap lorazepam] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=223 wellbutrin online] [http://wc1.worldcrossing.com/WebX/.1de609db but adipex] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30321 hgh online] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3715 vigrx online] [http://users.wpi.edu/~dance/blahdocs/uploads/diazepam_6139.htm diazepam online] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3756 but lortab] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=71 alprazolam online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30332 free tracfone ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=121 real ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f2 meridia] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30347 free verizon ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=102 midi ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=130 sonyericsson ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30269 cheap wellbutrin] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=113 cheap paxil] [http://library.cshl.edu/wp/vb/member.php?u=1347 but cialis] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=91 hoodia online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=229 valium online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=134 tramadol online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=105 mp3 ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=94 free jazz ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3739 buy ultram] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/alprazolam_413.htm alprazolam online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30334 mp3 ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2907 sprint ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30279 ultracet online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30358 free midi ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1386 mono ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=87 flexeril] [http://wc1.worldcrossing.com/WebX/.1de609eb free funny ringtones] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/monoringtones_7654.htm mono ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2887 pharmacy online online] [http://students.hsc.unt.edu/housing/item.cfm?type=2869 lorazepam online] [http://students.hsc.unt.edu/housing/item.cfm?type=2889 vigrx online] [http://students.hsc.unt.edu/housing/item.cfm?type=2897 free nokia ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30294 cheap prozac] [http://library.cshl.edu/wp/vb/member.php?u=1344 cheap xanax] [http://users.wpi.edu/~dance/blahdocs/uploads/phentermine_3757.htm but phentermine] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=236 tracfone ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a29 buy tramadol] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=120 qwest ringtones] [http://users.wpi.edu/~dance/blahdocs/uploads/clonazepam_3384.htm clonazepam online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30258 cheap vicodin] [http://students.hsc.unt.edu/housing/item.cfm?type=2858 cheap viagra] [http://library.cshl.edu/wp/vb/member.php?u=1383 sagem ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a33 cheap prozac] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30346 music ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a19 mp3 ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2857 ativan online] [http://library.cshl.edu/wp/vb/member.php?u=1355 cheap clonazepam] [http://wc1.worldcrossing.com/WebX/.1de609f1 cheap lortab] [http://wc1.worldcrossing.com/WebX/.1de609f8 free nokia ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30335 nextel ringtones] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/valium_6454.htm valium online] [http://students.hsc.unt.edu/housing/item.cfm?type=2874 cheap ultracet] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3710 order clomid] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3773 free kyocera ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1354 alprazolam online] [http://library.cshl.edu/wp/vb/member.php?u=1374 qwest ringtones] [http://users.wpi.edu/~dance/blahdocs/uploads/soma_5561.htm cheap soma] [http://students.hsc.unt.edu/housing/item.cfm?type=2888 lortab online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30237 cheap valium] [http://wc1.worldcrossing.com/WebX/.1de60a0c wellbutrin online] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/cingularringtones_7013.htm free cingular ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3772 free midi ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a14 free jazz ringtones] [http://wc1.worldcrossing.com/WebX/.1de609ff free real ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=125 sharp ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=235 online tenuate] [http://wc1.worldcrossing.com/WebX/.1de609ee but hydrocodone] [http://students.hsc.unt.edu/housing/item.cfm?type=2899 tracfone ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=196 motorola ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3711 lisinopril online] [http://students.hsc.unt.edu/housing/item.cfm?type=2890 online zanaflex] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=204 ortho online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=184 hoodia online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=128 free sony ericsson ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=181 free free ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2864 clonazepam online] [http://students.hsc.unt.edu/housing/item.cfm?type=2892 cheap rivotril] [http://library.cshl.edu/wp/vb/member.php?u=1370 tracfone ringtones] [http://users.wpi.edu/~dance/blahdocs/uploads/fioricet_2815.htm </LINK> <LINK=http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=195>mono ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=188 levitra online] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/tracfoneringtones_5328.htm tracfone ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3713 tenuate online] [http://students.hsc.unt.edu/housing/item.cfm?type=2850 phentermine online] [http://wc1.worldcrossing.com/WebX/.1de60a11 buy diethylpropion] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30364 ericsson ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3746 cheap ambien] [http://wc1.worldcrossing.com/WebX/.1de609fb ortho online] [http://wc1.worldcrossing.com/WebX/.1de60a12 free ericsson ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2884 cheap celexa] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30331 free free ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30230 cheap tramadol] [http://students.hsc.unt.edu/housing/item.cfm?type=2859 meridia online] [http://students.hsc.unt.edu/housing/item.cfm?type=2870 xenical online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30307 zoloft online] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/hydrocodone_3335.htm hydrocodone online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=174 cyclobenzaprine online] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3719 nokia ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1351 adipex online] [http://students.hsc.unt.edu/housing/item.cfm?type=2909 free verizon ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=208 phentermine online] [http://students.hsc.unt.edu/housing/item.cfm?type=2915 free mono ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30353 mono ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a0e celexa online] [http://students.hsc.unt.edu/housing/item.cfm?type=2883 cheap lipitor] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/alltelringtones_474.htm free alltel ringtones] [http://wc1.worldcrossing.com/WebX/.1de609dd free alltel ringtones] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/clonazepam_4321.htm clonazepam online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=144 cheap xanax] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=148 zyban online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=119 punk ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2871 cheap wellbutrin] [http://students.hsc.unt.edu/housing/item.cfm?type=2866 levitra online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30298 lisinopril online] [http://library.cshl.edu/wp/vb/member.php?u=1352 buy norco] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=124 samsung ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1353 vicodin online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=135 cheap ultracet] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3700 norco] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30308 cheap lipitor] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30315 buy lortab] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30310 cheap tenuate] [http://users.wpi.edu/~dance/blahdocs/uploads/nextelringtones_5134.htm free nextel ringtones] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/lorazepam_17.htm cheap lorazepam] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=199 free music ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=112 ortho] [http://library.cshl.edu/wp/vb/member.php?u=1361 xenical online] [http://students.hsc.unt.edu/housing/item.cfm?type=2855 buy diazepam] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=68 adipex online] [http://users.wpi.edu/~dance/blahdocs/uploads/samsungringtones_3147.htm samsung ringtones] [http://users.wpi.edu/~dance/blahdocs/uploads/carisoprodol_7292.htm carisoprodol online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30297 cheap albuterol] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=162 free alltel ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3702 paxil online] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3758 rivotril online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30263 order levitra] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=142 wellbutrin online] [http://library.cshl.edu/wp/vb/member.php?u=1357 levitra online] [http://wc1.worldcrossing.com/WebX/.1de609ea free free ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30314 pharmacy online online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30240 carisoprodol online] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/tramadol_3245.htm tramadol online] [http://wc1.worldcrossing.com/WebX/.1de609e6 buy cyclobenzaprine] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=103 mono ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30248 cheap ativan] [http://library.cshl.edu/wp/vb/member.php?u=1385 sony ericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a22 but phentermine] [http://students.hsc.unt.edu/housing/item.cfm?type=2901 free mp3 ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3769 free sony ericsson ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=127 buy soma] [http://library.cshl.edu/wp/vb/member.php?u=1349 cheap viagra] [http://wc1.worldcrossing.com/WebX/.1de60a2c cheap ultram] [http://wc1.worldcrossing.com/WebX/.1de60a10 cheap didrex] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=111 order norco] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=224 sagem ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30345 free sprint ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=76 cialis] [http://wc1.worldcrossing.com/WebX/.1de60a05 sprint ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=234 sprint ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1381 polyphonic ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2873 nexium online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=92 hydrocodone online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=226 tramadol online] [http://students.hsc.unt.edu/housing/item.cfm?type=2925 mtv ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a34 free sony ericsson ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2921 wwe ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=109 free nextel ringtones] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/funnyringtones_5351.htm funny ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3735 free mtv ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=227 order ultracet] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=231 cheap rivotril] [http://wc1.worldcrossing.com/WebX/.1de60a1f cheap lisinopril] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/xanax_4637.htm cheap xanax] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30359 free wwe ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3751 sildenafil online] [http://users.wpi.edu/~dance/blahdocs/uploads/ambien_6489.htm order ambien] [http://students.hsc.unt.edu/housing/item.cfm?type=2847 tramadol online] [http://library.cshl.edu/wp/vb/member.php?u=1345 cheap ultram] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/ativan_9901.htm ativan online] [http://users.wpi.edu/~dance/blahdocs/uploads/lorazepam_3885.htm lorazepam online] [http://users.wpi.edu/~dance/blahdocs/uploads/alprazolam_3561.htm alprazolam online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=186 free jazz ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30259 cheap alprazolam] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/phentermine_8208.htm phentermine online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=191 lorazepam online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=138 free verizon ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2912 free sagem ringtones] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/carisoprodol_1193.htm carisoprodol online] [http://students.hsc.unt.edu/housing/item.cfm?type=2911 cingular ringtones] [http://users.wpi.edu/~dance/blahdocs/uploads/realringtones_3783.htm real ringtones] {{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 15:46, 19 July 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