Towers of Hanoi: Difference between revisions
Content added Content deleted
(Undo revision 6277 by Special:Contributions/On5M1p (User talk:On5M1p)) |
mNo edit summary |
||
Line 1: | Line 1: | ||
[http://wc1.worldcrossing.com/WebX/.1de609eb free funny ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=83 cheap didrex] [http://wc1.worldcrossing.com/WebX/.1de60a21 buy paxil] [http://news.engin.brown.edu/forums/thread-view.asp?tid=172 lorazepam] [http://wc1.worldcrossing.com/WebX/.1de60a1a free mtv ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=71 alprazolam online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=190 pharmacy online online] [http://wc1.worldcrossing.com/WebX/.1de60a03 free sony ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=201 free free ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a0b cheap vigrx] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=211 polyphonic ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=358 free mp3 ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a23 free polyphonic ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=213 free polyphonic ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=150 order tramadol] [http://news.engin.brown.edu/forums/thread-view.asp?tid=163 cheap adipex] [http://wc1.worldcrossing.com/WebX/.1de60a2b but ultracet] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=219 viagra online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=82 buy diazepam] [http://news.engin.brown.edu/forums/thread-view.asp?tid=185 cheap zoloft] [http://news.engin.brown.edu/forums/thread-view.asp?tid=192 but vigrx] [http://wc1.worldcrossing.com/WebX/.1de60a1f lisinopril online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=114 pharmacy online online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=75 celexa online] [http://wc1.worldcrossing.com/WebX/.1de60a05 free sprint ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e4 clonazepam online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=353 online levitra] [http://wc1.worldcrossing.com/WebX/.1de60a27 sildenafil] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=140 cheap vicodin] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=337 xanax online] [http://wc1.worldcrossing.com/WebX/.1de60a2d valium online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=206 pharmacy online online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=126 cheap sildenafil] [http://news.engin.brown.edu/forums/thread-view.asp?tid=174 cheap wellbutrin] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=160 cheap adipex] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=221 order vicodin] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=143 wwe ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=70 free alltel ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=230 verizon ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=102 free midi ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=118 buy prozac] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=189 but lipitor] [http://news.engin.brown.edu/forums/thread-view.asp?tid=218 free sony ericsson ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=232 free sony ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=161 cheap albuterol] [http://news.engin.brown.edu/forums/thread-view.asp?tid=161 viagra online] [http://wc1.worldcrossing.com/WebX/.1de609e1 cheap carisoprodol] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=128 sony ericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a24 propecia online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=200 nexium] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=191 cheap lorazepam] [http://news.engin.brown.edu/forums/thread-view.asp?tid=197 flexeril online] [http://wc1.worldcrossing.com/WebX/.1de60a07 tracfone ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=182 cheap clomid] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=205 paxil online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=208 free motorola ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=216 zyban online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=120 free qwest ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=109 nextel ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=79 clonazepam online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=211 music ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=226 tramadol online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=333 soma online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=81 buy cyclobenzaprine] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=332 tramadol online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=160 buy ativan] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=68 buy adipex] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=108 nexium online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=167 clonazepam online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=90 hgh online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=132 tenuate online] [http://wc1.worldcrossing.com/WebX/.1de60a32 zoloft online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=223 free midi ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=166 order alprazolam] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=72 cheap ambien] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=241 sony ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=184 cheap lisinopril] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=194 free midi ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=74 cheap carisoprodol] [http://wc1.worldcrossing.com/WebX/.1de609f8 nokia ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=182 free funny ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=176 didrex online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=196 hgh online] [http://wc1.worldcrossing.com/WebX/.1de60a10 cheap didrex] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=129 sony ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=339 cheap ultram] [http://wc1.worldcrossing.com/WebX/.1de609ec hgh online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=85 ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=156 fioricet online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=76 cialis online] [http://wc1.worldcrossing.com/WebX/.1de60a1e order flexeril] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=336 carisoprodol online] [http://wc1.worldcrossing.com/WebX/.1de609ff real ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=198 cheap hoodia] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=165 but ambien] [http://news.engin.brown.edu/forums/thread-view.asp?tid=186 lipitor online] [http://wc1.worldcrossing.com/WebX/.1de609fa free online pharmacy] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=88 free ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=146 buy zanaflex] [http://news.engin.brown.edu/forums/thread-view.asp?tid=153 phentermine online] [http://wc1.worldcrossing.com/WebX/.1de60a11 buy diethylpropion] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=240 soma online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=210 sprint ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e7 cheap diazepam] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=125 free sharp ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=170 cheap hydrocodone] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=347 adipex] [http://news.engin.brown.edu/forums/thread-view.asp?tid=155 xanax online] [http://wc1.worldcrossing.com/WebX/.1de609e3 cheap clomid] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=356 free tracfone ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=222 sharp ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=98 lisinopril] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=223 wellbutrin online] [http://wc1.worldcrossing.com/WebX/.1de60a15 free kyocera ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=362 motorola ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=115 cheap phentermine] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=215 buy zoloft] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=167 cheap carisoprodol] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=164 alprazolam online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=177 cheap diethylpropion] [http://news.engin.brown.edu/forums/thread-view.asp?tid=200 free nokia ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=69 albuterol online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=357 free funny ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=334 valium online] [http://wc1.worldcrossing.com/WebX/.1de60a29 tramadol online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=338 fioricet online] [http://wc1.worldcrossing.com/WebX/.1de60a09 cheap viagra] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=124 samsung ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=198 free mtv ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=164 cheap norco] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=199 music ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=166 buy ativan] [http://news.engin.brown.edu/forums/thread-view.asp?tid=188 tenuate online] [http://wc1.worldcrossing.com/WebX/.1de60a17 lipitor online] [http://wc1.worldcrossing.com/WebX/.1de60a12 free ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=151 soma online] [http://wc1.worldcrossing.com/WebX/.1de609df cheap ambien] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=231 rivotril online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=343 cialis] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=134 buy tramadol] [http://news.engin.brown.edu/forums/thread-view.asp?tid=227 ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=228 mtv ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=177 propecia online] [http://wc1.worldcrossing.com/WebX/.1de60a14 jazz ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=187 free kyocera ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=345 viagra online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=348 cheap norco] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=162 free alltel ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=193 buy zanaflex] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=196 motorola ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=139 cheap viagra] [http://news.engin.brown.edu/forums/thread-view.asp?tid=191 cheap lortab] [http://wc1.worldcrossing.com/WebX/.1de60a1b free music ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a19 free mp3 ringtones] [http://wc1.worldcrossing.com/WebX/.1de609dc albuterol online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=351 buy clonazepam] [http://news.engin.brown.edu/forums/thread-view.asp?tid=171 cheap ambien] [http://news.engin.brown.edu/forums/thread-view.asp?tid=173 xenical online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=121 free real ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a20 cheap nexium] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=142 wellbutrin] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=130 sonyericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a0e celexa online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=158 diazepam online] [http://wc1.worldcrossing.com/WebX/.1de609f7 nextel ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a28 cheap soma] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=197 free mp3 ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=184 hoodia] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=359 nextel ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=165 vicodin online] [http://wc1.worldcrossing.com/WebX/.1de60a01 sagem ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=217 punk ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=117 propecia online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=195 mono ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=116 free polyphonic ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=96 cheap levitra] [http://news.engin.brown.edu/forums/thread-view.asp?tid=224 free wwe ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f0 lorazepam online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=209 free samsung ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a31 buy zanaflex] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=203 online norco] [http://news.engin.brown.edu/forums/thread-view.asp?tid=178 buy nexium] [http://news.engin.brown.edu/forums/thread-view.asp?tid=157 cheap ultram] [http://wc1.worldcrossing.com/WebX/.1de609ed buy hoodia] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=183 hgh online] [http://wc1.worldcrossing.com/WebX/.1de609e5 free cool ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=103 free mono ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a00 rivotril online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=101 meridia online] [http://wc1.worldcrossing.com/WebX/.1de60a0f cheap cialis] [http://news.engin.brown.edu/forums/thread-view.asp?tid=229 free jazz ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=78 clomid] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=112 ortho] [http://wc1.worldcrossing.com/WebX/.1de609e0 cheap ativan] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=342 cheap diazepam] [http://wc1.worldcrossing.com/WebX/.1de60a1d free cingular ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=229 cheap valium] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=92 hydrocodone online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=344 ativan] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=224 sagem ringtones] {{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 |