Implement tower of hanoi in Golang


In this article we are going to learn how to use recursion method and iteration to implement tower of hanoi in golang. The Tower of Hanoi is a puzzle in which we move a set of disks from one page to another by moving one disk at a time. There are certain set of rules that need to be obeyed in this puzzle.

Using Recursion Approach

In this method, we will write a go language program to implement tower of Hanoi by using the method of recursion.

If n is 1, we can move the disk from from to to directly. Otherwise, we first move n-1 disks from from to via, then move the nth disk from from to to, and finally move the n-1 disks from via to to.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Then, create a function named hanoi(). This function accepts three arguments the first one is the number of string variables passed while others are the string variables.

  • Step 3 − Then start the main() function. Inside the main() call the hanoi() function by passing the required arguments to it.

  • Step 4 − If n == 1 move the disk from from peg to to peg and stop the program. Otherwise call the hanoi() function recursively with n – 1, from peg, via peg, and to peg as arguments and print the result on the screen by using fmt.Println() function.

Example

In the following Example, we are going to understand how to develop a go language program to implement tower of hanoi by using recursion approach

package main

import "fmt"

func hanoi(n int, from string, to string, via string) {
   if n == 1 {
      fmt.Printf("Move disk 1 from %s to %s\n", from, to)
   } else {
      hanoi(n-1, from, via, to)
      fmt.Printf("Move disk %d from %s to %s\n", n, from, to)
      hanoi(n-1, via, to, from)
   }
}

func main() {
   hanoi(3, "A", "C", "B")
}

Output

Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C

Using The Concept Of Iteration

In this method, we will write a go language program to implement the tower of hanoi by using the concept of iteration.

Syntax

func len(v Type) int

The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Next create a function called hanoi(). This function accepts four arguments. The first one is the integer value depicting the number of strings while the others are the string variables.

  • Step 3 − Inside this function create an empty array called stack and store in it the variables obtained as arguments.

  • Step 4 − Then use the for loop to iterate over the stack array and use the for loop to print various peg positions on the screen by using the fmt.Println() function.

Example

In the following Example we will understand how to develop a go language program to implement tower of hanoi by using the concept of iteration

package main

import "fmt"

func hanoi(n int, from string, to string, via string) {
   stack := make([][3]string, 0)
   stack = append(stack, [3]string{from, to, via})

   for len(stack) > 0 {
      task := stack[len(stack)-1]
      stack = stack[:len(stack)-1]

      if n == 1 {
         fmt.Printf("Move disk 1 from %s to %s\n", task[0], task[1])
      } else {
         stack = append(stack, [3]string{task[2], task[1], task[0]})
         stack = append(stack, [3]string{task[0], task[1], task[2]})
         stack = append(stack, [3]string{task[1], task[2], task[0]})
         n--
      }
   }
}

func main() {
   hanoi(3, "A", "C", "B")
}

Output

Move disk 1 from B to A
Move disk 1 from C to B
Move disk 1 from A to B
Move disk 1 from A to C
Move disk 1 from B to C

Conclusion

We have successfully compiled and executed a go language program to implement the tower of Hanoi along with Examples. We will use two different methods to implement this. In the first method we are using the recursion approach while in the second one we are using the method of iteration.

Updated on: 05-Apr-2023

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements