Merge pull request #1363 from xexyl/vanschnitz1995
This commit is contained in:
1 file changed
+11
-10
+11
-10
@@ -60,7 +60,7 @@ never be placed on top of a smaller disk.
|
||||
|
||||
Our program solves the Towers of Hanoi problem. Well, that's
|
||||
not exactly true; actually, it's the compiler that solves the
|
||||
problem. The resulting program just prints out the correct\
|
||||
problem. The resulting program just prints out the correct
|
||||
solution.
|
||||
|
||||
How do you trick a compiler into actually solving the problem?
|
||||
@@ -76,25 +76,24 @@ gcc hanoi.c -o hanoi -Dn=4
|
||||
|
||||
A default value of `5` will be used for `n` if you do not define
|
||||
it on the command line. The value of `n` cannot be greater than
|
||||
fifteen (the compiler we used to test has a limit on the #include
|
||||
depth). The compiler then solves the problem using binary\
|
||||
fifteen (the compiler we used to test has a limit on the `#include`
|
||||
depth). The compiler then solves the problem using binary
|
||||
arithmetic based on whether particular symbols are defined or not.
|
||||
|
||||
To loop, the program `#include`s itself. This is, of course,
|
||||
expensive; one compile we did with `n=14` took about fifty
|
||||
minutes to compile on our system (compiling with `n=15` caused our system to
|
||||
crash).
|
||||
To loop, the program `#include`s itself. This is, of course, expensive; one
|
||||
compile we did with `n=14` took about fifty minutes to compile on our system
|
||||
(compiling with `n=15` caused our system to crash).
|
||||
|
||||
The resulting program that the compiler generates simply
|
||||
prints out the answer. Did I say "simply"? Actually, the
|
||||
whole resulting program consists of a single printf statement,
|
||||
whole resulting program consists of a single `printf(3)` statement,
|
||||
consisting of a massive string constant of length `35*(2^n-1)`,
|
||||
followed by `3*(2^n-1)` integers which get formatted into the
|
||||
string. For our `n=14` run, this adds up to a string constant
|
||||
of length 573405, followed by 49149 integers delimited by
|
||||
commas. (Generating the string constant depends on the
|
||||
ANSI C feature in which adjacent character strings are
|
||||
catenated; a version that does not use this feature has been
|
||||
concatenated; a version that does not use this feature has been
|
||||
included for people who can only run K&R). A good way to see
|
||||
the resulting program (on a Unix system) is to do the command
|
||||
|
||||
@@ -104,13 +103,14 @@ gcc hanoi.c -E -Dn=5 | grep -v \# | grep -v ^\$
|
||||
|
||||
For an odd number of disks, the program will provide a
|
||||
solution wherein the disks end up on peg 2; for an even
|
||||
number of disks, they will end on peg 3. This should\
|
||||
number of disks, they will end on peg 3. This should
|
||||
provide some hint as to what sort of algorithm is used.
|
||||
|
||||
We have included a [spoiler](spoiler.c) version of the program, with
|
||||
meaningful symbol names and comments, but we encourage you
|
||||
to try to decipher the program without it...
|
||||
|
||||
```
|
||||
begin 444 spoiler.c
|
||||
M(VEF;F1E9B`@24Y)5$E!3$E:140*"B\J"B`J("!)9B!N(&ES;B=T(&1E9FEN
|
||||
M960L(&UA:V4@:70@-0H@*B\*(VEF;F1E9B`@;@HC9&5F:6YE("!N(#`U"B-E
|
||||
@@ -189,6 +189,7 @@ to try to decipher the program without it...
|
||||
`
|
||||
end
|
||||
|
||||
```
|
||||
|
||||
## Copyright and CC BY-SA 4.0 License:
|
||||
|
||||
|
||||
Reference in new issue
Block a user