Towers of Hanoi: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|PureBasic}}: Added PureBasic) |
||
Line 785: | Line 785: | ||
hanoi(4, "left", "middle", "right");</lang> |
hanoi(4, "left", "middle", "right");</lang> |
||
=={{header|PureBasic}}== |
|||
Algorithm according to http://en.wikipedia.org/wiki/Towers_of_Hanoi |
|||
<lang PureBasic>Procedure Hanoi(n, A.s, C.s, B.s) |
|||
If n |
|||
Hanoi(n-1, A, B, C) |
|||
PrintN("Move the plate from "+A+" to "+C) |
|||
Hanoi(n-1, B, C, A) |
|||
EndIf |
|||
EndProcedure</lang> |
|||
Full program |
|||
<lang PureBasic>Procedure Hanoi(n, A.s, C.s, B.s) |
|||
If n |
|||
Hanoi(n-1, A, B, C) |
|||
PrintN("Move the plate from "+A+" to "+C) |
|||
Hanoi(n-1, B, C, A) |
|||
EndIf |
|||
EndProcedure |
|||
If OpenConsole() |
|||
Define n=3 |
|||
PrintN("Moving "+Str(n)+" pegs."+#CRLF$) |
|||
Hanoi(n,"Left Peg","Middle Peg","Right Peg") |
|||
PrintN(#CRLF$+"Press ENTER to exit."): Input() |
|||
EndIf</lang> |
|||
Outputs |
|||
<tt> |
|||
Moving 3 pegs. |
|||
Move the plate from Left Peg to Middle Peg |
|||
Move the plate from Left Peg to Right Peg |
|||
Move the plate from Middle Peg to Right Peg |
|||
Move the plate from Left Peg to Middle Peg |
|||
Move the plate from Right Peg to Left Peg |
|||
Move the plate from Right Peg to Middle Peg |
|||
Move the plate from Left Peg to Middle Peg |
|||
Press ENTER to exit.</tt> |
|||
=={{header|Python}}== |
=={{header|Python}}== |