Towers of Hanoi: Difference between revisions
Content added Content deleted
(+Icon+Unicon) |
|||
Line 445: | Line 445: | ||
procedure hanoi(n, needle1, needle2) #: solve towers of hanoi by moving n disks from needle 1 to needle2 via other |
procedure hanoi(n, needle1, needle2) #: solve towers of hanoi by moving n disks from needle 1 to needle2 via other |
||
local other |
local other |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if n = 1 then |
if n = 1 then |
||
write("Move disk from ", needle1, " to ", needle2) |
write("Move disk from ", needle1, " to ", needle2) |
||
else { |
else { |
||
other := 6 - needle1 - needle2 # clever but somewhat un-iconish way to find other |
other := 6 - needle1 - needle2 # clever but somewhat un-iconish way to find other |
||
hanoi(n-1, needle1, other) |
hanoi(n-1, needle1, other) |
||
write("Move disk from ", needle1, " to ", needle2) |
write("Move disk from ", needle1, " to ", needle2) |
||
hanoi(n-1, other, needle2) |
hanoi(n-1, other, needle2) |
||
} |
} |
||
return |
return |
||
end</lang> |
end</lang> |
||
==={{header|Unicon}}=== |
==={{header|Unicon}}=== |
||
This Icon solution works in Unicon. |
This Icon solution works in Unicon. |