Towers of Hanoi: Difference between revisions
Content deleted Content added
Added C++ |
No edit summary |
||
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 {}) |
|||
==[[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); |
|||
} |
|||
} |
|||
==[[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; |
Revision as of 02:51, 11 April 2007
Towers of Hanoi
You are encouraged to solve this task according to the task description, using any language you may know.
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"