home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!caen!uwm.edu!csd4.csd.uwm.edu!markh
- From: markh@csd4.csd.uwm.edu (Mark)
- Subject: Re: Teaching the basics
- Message-ID: <1992Aug26.175253.29133@uwm.edu>
- Sender: news@uwm.edu (USENET News System)
- Organization: Computing Services Division, University of Wisconsin - Milwaukee
- References: <1992Aug17.123916.14815@husc13.harvard.edu> <7116@charon.cwi.nl> <14194@goanna.cs.rmit.oz.au>
- Date: Wed, 26 Aug 1992 17:52:53 GMT
- Lines: 31
-
- In article <14194@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
- >Please get your attribution straight. That _wasn't_ me. I'm the one who
- >said that if you don't understand recursion, you don't understand programming.
- >
- >> void hanoi(n)
- >> {
- >> for (x = 1; x < (1<<n); x++)
- >> printf("move a disc from %d to %d\n", (x&x-1)%3, ((x|x-1)+1)%3);
- >> }
- >>
- >Besides, the simple recursive version is not as limited by the size of
- >integers. The machine I'm using as a _terminal_emulator_ can go through
- >hanoi(32) in under an hour, so don't think that 32-bit integers are enough...
-
- It's easy enough to extend it to arbitrary precision integers since the
- operations involved (decrement, logical shifts, logical or) are easily
- extended, especially if you're doing it in assembly.
-
- You could even bring these operations out to the source level in a language
- like C++, using the same operator symbols (+, <<, >>, etc).
-
- Generally a recursion with N essential entry points has its entry-point stack
- coded as follows:
-
- long_word X;
- CLEAR() { X = 0; }
- PUSH(int N) { X = N*X + 1; }
- EMPTY() { return X == 0; }
- POP() { int Top; X--, Top = X%N, X /= N; return Top; }
-
- A recursion with a single entry point therefore uses a counter.
-