HANOI builds the puzzle as real geometry — a base, three posts and a stack of discs — and HANOISOLVE solves it in front of you, one move at a time.
What TowerHanoi does
The rules, unchanged since Lucas set the puzzle in 1883:
1. Only one disc moves at a time.
2. No disc may rest on a smaller one.
The legend has monks moving sixty-four discs, the world ending when they finish. The arithmetic is the interesting part: n discs take 2n − 1 moves, so ten discs is a thousand moves and twenty is a million. Sixty-four, at one move a second, is about 585 billion years. The legend is safe.
How to use it
- Load the routine with
APPLOAD, or put it on your support file search path. - Type
HANOIand give the number of discs. - Type
HANOISOLVEand watch.
How it works
To move n discs from A to C: move the top n−1 to B, move the bottom one to C, then move the n−1 from B to C. Three lines, and it is optimal — no shorter solution exists. Recursion rarely looks better than this.
Discs are created with entmake, held by entity name, and moved. Removing a disc with (command "erase" <point> "") is an erase by picking a computed screen coordinate — anything else near that point goes instead. Moving the object you already have a handle on is both correct and far faster.
The puzzle knows its own objects. Tidying up between runs by erasing the last N objects drawn, N being remembered from the previous run, erases whatever you happened to draw in between.
AutoCAD’s option word is Color, not Colour. Every (command "layer" ... "colour" ...) in the original failed on that, and the discs came out monochrome at best.
Notes and limits
- Ten discs is 1,023 moves. Start small.
- It is real 3D geometry, so you can orbit round it.
- Nothing else in the drawing is touched.
- One undo group covers the build and the solve.
Download and details
| Name | TowerHanoi — build the Tower of Hanoi in 3D and watch it solve itself |
| Download | TowerHanoi.lsp — 12.5 KB, plain AutoLISP source. Downloads directly; no zip to unpack. |
| Type | AutoCAD AutoLISP routine |
| Commands | HANOI, HANOISOLVE |
| Requires | nothing beyond a CAD platform with AutoLISP. |
| Source size | 12.5 KB, 307 lines |
| Error handling | Yes — a *error* handler restores every system variable it changed, on cancel as well as on error. |
| Undo | The whole operation is wrapped in a single undo group, so one U reverses all of it. |
| Compatibility | AutoCAD (any release with AutoLISP), BricsCAD, ZWCAD, GstarCAD, ProgeCAD and other IntelliCAD-based platforms. AutoCAD LT needs a LISP enabler. No .NET, no ObjectARX, no installer. |
| Author | YZ, August 2026 |
| Licence | Free to use, supplied “as is” with no warranty. |
References
- HILBERT LISP — the other recursion in this set
- FRACTREE LISP — recursion that looks like a tree
- 5 crazy things to do with AutoLISP — more of the same spirit
- More free AutoLISP routines on this site
- Wisey’s Steel Shapes — free structural steel section drawing program