How does this work? Weird Towers of Hanoi Solution
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Many "weird" Towers of Hanoi solutions look magical because they avoid the familiar recursive explanation and instead follow a short mechanical rule. The most common version is the iterative solution: move the smallest disk every other turn, and on the alternating turns make the only legal move that does not involve the smallest disk.
The Standard Recursive Idea First
The classic recursive solution says:
- move
n-1disks to the spare peg - move the largest disk
- move the
n-1disks onto it
That yields the recurrence T(n) = 2T(n-1) + 1, which gives the minimum move count 2^n - 1.
The iterative rule looks strange because it does not mention n-1 subproblems at all, yet it still produces the same optimal sequence.
The Iterative Rule
For the three-peg puzzle, an optimal iterative method is:
- move the smallest disk every other move
- between those moves, make the only legal move that does not involve the smallest disk
A simple implementation for printing the moves looks like this.
The code is less important than the logic behind it.
Why Moving the Smallest Disk Every Other Turn Works
The smallest disk is special because it can always move to either of the other pegs. In an optimal solution, it moves on every odd-numbered turn.
Why:
- no larger disk can move as frequently without blocking progress
- delaying the smallest disk would waste a legal move opportunity
- the puzzle alternates between moving the smallest disk and reorganizing the larger structure around it
The direction of the smallest disk's cycle depends on whether n is odd or even. That parity rule is what makes the iterative method seem mysterious at first.
Why the Other Move Is Forced
After moving the smallest disk, exactly two pegs remain for the next move. Among the top disks on those pegs, there is at most one legal move, because a larger disk cannot sit on a smaller one.
So the "weird" algorithm is not guessing. It is alternating between:
- a deterministic move of the smallest disk
- the only remaining legal move
Once you see that, the method stops looking magical and starts looking inevitable.
It Is Still the Same Recursive Structure
The iterative method feels different from recursion, but it is encoding the same structure implicitly. The repeated movement of the smallest disk and the forced legal moves between larger disks reconstruct the exact minimal sequence that the recursive proof describes.
So the weird solution is not a new puzzle. It is a different way to generate the same optimal path.
Common Pitfalls
The most common mistake is forgetting that the direction of the smallest disk's cycle depends on whether the disk count is odd or even.
Another mistake is thinking the non-smallest move requires search. In the three-peg puzzle, it is forced.
A third issue is assuming the iterative rule generalizes unchanged to every Hanoi variant. Extra pegs or changed movement rules produce different behavior.
Finally, the solution only looks mysterious if you skip the invariants. Once you track the smallest disk and the uniqueness of the other legal move, the pattern is straightforward.
Summary
- The classic minimum move count is still
2^n - 1. - The iterative "weird" solution moves the smallest disk every other turn.
- On alternating turns, the only legal move that avoids the smallest disk is forced.
- The smallest disk's movement direction depends on whether
nis odd or even. - The iterative rule is not different from the recursive solution in outcome, only in presentation.
- What looks magical is really a compact expression of the same underlying structure.
Related reading
- How does Top-K sort algorithm work in MongoDB
- How does vector clock work in leaderless (or peer-to-peer) architecture?
- How exactly do you compute the Fast Fourier Transform?
- How exactly does a XOR Linked list work?
- How hard is this graph problem?
- How is 2D bin packing achieved programmatically?
- How exactly does tail recursion work?
- How external merge sort algorithm works?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.