kernelthread.com

GNU Calc

The Towers of Hanoi as a GNU Calc program.

Calc is an arbitrary precision arithmetic system that uses a C-like language. It's useful as a calculator, an algorithm prototype, and as a mathematical research tool. More importantly, calc provides a machine-independent means of computation.

More information is available at the GNU Calc page.

/* * The Towers Of Hanoi * GNU Calc - C-style arbitrary precision arithmetic system * Copyright (C) 2002 Amit Singh. All Rights Reserved. * http://hanoi.kernelthread.com */ define move(f, t) { printf("%s --> %s\n", f, t); } define dohanoi(n, f, t, u) { if (n == 1) { move(f, t); } else { dohanoi(n - 1, f, u, t); move(f, t); dohanoi(n - 1, u, t, f); } } define hanoi(n) { if (!isint(n) || n <= 0) { quit "n must be an integer > 0"; } /* move n disks from tower 1 to 3 using 2 */ dohanoi(n, "1", "3", "2"); }

Download

hanoi.cal