How to resolve the algorithm Towers of Hanoi step by step in the Swift programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Towers of Hanoi step by step in the Swift programming language
Table of Contents
Problem Statement
Solve the Towers of Hanoi problem with recursion.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Towers of Hanoi step by step in the Swift programming language
Source code in the swift programming language
func hanoi(n:Int, a:String, b:String, c:String) {
if (n > 0) {
hanoi(n - 1, a, c, b)
println("Move disk from \(a) to \(c)")
hanoi(n - 1, b, a, c)
}
}
hanoi(4, "A", "B", "C")
func hanoi(n:Int, a:String, b:String, c:String) {
if (n > 0) {
hanoi(n - 1, a: a, b: c, c: b)
print("Move disk from \(a) to \(c)")
hanoi(n - 1, a: b, b: a, c: c)
}
}
hanoi(4, a:"A", b:"B", c:"C")
You may also check:How to resolve the algorithm Copy a string step by step in the Raven programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the MIPS Assembly programming language
You may also check:How to resolve the algorithm Blum integer step by step in the jq programming language
You may also check:How to resolve the algorithm Continued fraction step by step in the Erlang programming language
You may also check:How to resolve the algorithm Input loop step by step in the ALGOL W programming language