Skip to content

Instantly share code, notes, and snippets.

@R3DHULK
Created March 28, 2023 17:50
Show Gist options
  • Save R3DHULK/784552ef3c56f7787f3827a0a8a87cb2 to your computer and use it in GitHub Desktop.
Save R3DHULK/784552ef3c56f7787f3827a0a8a87cb2 to your computer and use it in GitHub Desktop.
Tower Of Hanoi Written In Go
package main
import (
"fmt"
)
func main() {
var numDisks int
fmt.Print("Enter the number of disks: ")
fmt.Scanln(&numDisks)
moves := towerOfHanoi(numDisks, "A", "B", "C")
fmt.Println("Moves:", moves)
}
func towerOfHanoi(numDisks int, from string, to string, aux string) int {
if numDisks == 1 {
fmt.Println("Move disk 1 from", from, "to", to)
return 1
}
moves := towerOfHanoi(numDisks-1, from, aux, to)
fmt.Println("Move disk", numDisks, "from", from, "to", to)
moves++
moves += towerOfHanoi(numDisks-1, aux, to, from)
return moves
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment