Implementing the Hanota Algorithm Using Golang | by Dwen | Feb, 2022

Tower of Hanoi — Golang version

Photo by Caspar Camille Rubin on Unsplash
The number of moves of a gold piece is 2 to the power of 1 minus 1
The number of moves of 2 gold pieces is 2 to the power of 2 minus 1
The number of moves of 3 gold pieces is 2 to the power of 3 minus 1

The number of moves of n pieces of gold is 2 to the nth power minus 1
  1. The small beads must be on top of the large beads
  1. Empty your brain and think about the simplest and most rude solution logic first. Think of the beads as a whole.
  2. To meet the basic conditions for the large bead to be down, it is definitely necessary to empty the largest bead on Aand then put the largest bead on the Ccolumn. Suppose the largest bead number is N.
  3. If you want to move to the Ccolumn, the first step to be realized must be to move all N-1beads to the Bcolumn, only in this way the Nth bead (that is, the largest bead) can be moved to the Ccolumn.
  4. Move the N-1beads to the Bcolumn, because the big ones are down and the small ones are up, so the N-1beads are also ordered on the Bcolumn.
  5. Finally, move the N-1 beads from the B column to the C column to complete the final goal.
The third step is actually equivalent to changing the requirements. Suppose K = N - 1.There are K beads on column B, column A is empty, column C has the largest bead so for column B with K beads it is equivalent to empty.The first step is to move K-1 beads on B to A.The second step moves the Kth bead on B to C.The third step moves K-1 beads on A to C.
To achieve moving from A to B, then C is the auxiliary column.To move from A to C, then B is the auxiliary pillar.To achieve the move from B to C, then A is the auxiliary column.
  1. Find the exit conditions
  • The second step is to move the [Nth] plate from A to C
  • The third step is to move the [remaining N-1] plates from B to C

Leave a Comment