Merge pull request #1363 from xexyl/vanschnitz1995

This commit is contained in:
Landon Curt Noll authored and GitHub committed 2023-11-10 14:54:07 -07:00
commit cd56c20949
1 file changed
+88 -87
+88 -87
View File
@@ -60,7 +60,7 @@ never be placed on top of a smaller disk.
Our program solves the Towers of Hanoi problem. Well, that's Our program solves the Towers of Hanoi problem. Well, that's
not exactly true; actually, it's the compiler that solves the 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. solution.
How do you trick a compiler into actually solving the problem? 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 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 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 fifteen (the compiler we used to test has a limit on the `#include`
depth). The compiler then solves the problem using binary\ depth). The compiler then solves the problem using binary
arithmetic based on whether particular symbols are defined or not. arithmetic based on whether particular symbols are defined or not.
To loop, the program `#include`s itself. This is, of course, To loop, the program `#include`s itself. This is, of course, expensive; one
expensive; one compile we did with `n=14` took about fifty compile we did with `n=14` took about fifty minutes to compile on our system
minutes to compile on our system (compiling with `n=15` caused our system to (compiling with `n=15` caused our system to crash).
crash).
The resulting program that the compiler generates simply The resulting program that the compiler generates simply
prints out the answer. Did I say "simply"? Actually, the 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)`, consisting of a massive string constant of length `35*(2^n-1)`,
followed by `3*(2^n-1)` integers which get formatted into the 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 string. For our `n=14` run, this adds up to a string constant
of length 573405, followed by 49149 integers delimited by of length 573405, followed by 49149 integers delimited by
commas. (Generating the string constant depends on the commas. (Generating the string constant depends on the
ANSI C feature in which adjacent character strings are 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 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 the resulting program (on a Unix system) is to do the command
@@ -104,91 +103,93 @@ gcc hanoi.c -E -Dn=5 | grep -v \# | grep -v ^\$
For an odd number of disks, the program will provide a For an odd number of disks, the program will provide a
solution wherein the disks end up on peg 2; for an even 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. provide some hint as to what sort of algorithm is used.
We have included a [spoiler](spoiler.c) version of the program, with We have included a [spoiler](spoiler.c) version of the program, with
meaningful symbol names and comments, but we encourage you meaningful symbol names and comments, but we encourage you
to try to decipher the program without it... 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 begin 444 spoiler.c
M960L(&UA:V4@:70@-0H@*B\*(VEF;F1E9B`@;@HC9&5F:6YE("!N(#`U"B-E M(VEF;F1E9B`@24Y)5$E!3$E:140*"B\J"B`J("!)9B!N(&ES;B=T(&1E9FEN
M;F1I9@H*+RH*("H@4V]M92!U<V5F=6P@=F%R:6%B;&5S"B`J+PHC9&5F:6YE M960L(&UA:V4@:70@-0H@*B\*(VEF;F1E9B`@;@HC9&5F:6YE("!N(#`U"B-E
M("!42$E37T9)3$4@7U]&24Q%7U\@"B-D969I;F4@($9/4DU!5%]35%))3D<@ M;F1I9@H*+RH*("H@4V]M92!U<V5F=6P@=F%R:6%B;&5S"B`J+PHC9&5F:6YE
M(DUO=F4@9&ES:R`E9"!F<F]M('!E9R`E9"!T;R!P96<@)61<;B(*(V1E9FEN M("!42$E37T9)3$4@7U]&24Q%7U\@"B-D969I;F4@($9/4DU!5%]35%))3D<@
M92`@24Y)5$E!3$E:140*"B\J"B`J(%1R86YS;&%T92!N(&EN=&\@8FET<PH@ M(DUO=F4@9&ES:R`E9"!F<F]M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968@("!B:70P"B-D969I;F4@($Y?
M4U].3U1?6D523PHC96YD:68*(VEF9&5F("`@8FET,@HC9&5F:6YE("!.7TE3 M25-?3D]47UI%4D\*(V5N9&EF"B-I9F1E9B`@(&)I=#$*(V1E9FEN92`@3E])
M7TY/5%]:15)/"B-E;F1I9@HC:69D968@("!B:70S"B-D969I;F4@($Y?25-? M4U].3U1?6D523PHC96YD:68*(VEF9&5F("`@8FET,@HC9&5F:6YE("!.7TE3
M3D]47UI%4D\*(V5N9&EF"@HC:69D968@("!.7TE37TY/5%]:15)/"B-U;F1E M7TY/5%]:15)/"B-E;F1I9@HC:69D968@("!B:70S"B-D969I;F4@($Y?25-?
M9B`@($Y?25-?3D]47UI%4D\*"B\J"B`J($1E8W)E;65N="!N"B`J+PHC:69N M3D]47UI%4D\*(V5N9&EF"@HC:69D968@("!.7TE37TY/5%]:15)/"B-U;F1E
M9&5F("!B:70P"B-D969I;F4@(&)I=#`*(VEF;F1E9B`@8FET,0HC9&5F:6YE M9B`@($Y?25-?3D]47UI%4D\*"B\J"B`J($1E8W)E;65N="!N"B`J+PHC:69N
M("!B:70Q"B-I9FYD968@(&)I=#(*(V1E9FEN92`@8FET,@HC:69N9&5F("!B M9&5F("!B:70P"B-D969I;F4@(&)I=#`*(VEF;F1E9B`@8FET,0HC9&5F:6YE
M:70S"B-D969I;F4@(&)I=#,*(V5L<V4*(W5N9&5F("`@8FET,PHC96YD:68* M("!B:70Q"B-I9FYD968@(&)I=#(*(V1E9FEN92`@8FET,@HC:69N9&5F("!B
M(V5L<V4*(W5N9&5F("`@8FET,@HC96YD:68*(V5L<V4*(W5N9&5F("`@8FET M:70S"B-D969I;F4@(&)I=#,*(V5L<V4*(W5N9&5F("`@8FET,PHC96YD:68*
M,0HC96YD:68*(V5L<V4*(W5N9&5F("`@8FET,`HC96YD:68*"B\J"B`J("!( M(V5L<V4*(W5N9&5F("`@8FET,@HC96YD:68*(V5L<V4*(W5N9&5F("`@8FET
M97)E)W,@<F5C=7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B`@(&)I=#(*(VEF9&5F("`@8FET,PHC=6YD968@("!B:70S"B-E;'-E"B-D M9F1E9B`@(&)I=#$*(W5N9&5F("`@8FET,0HC:69D968@("!B:70R"B-U;F1E
M969I;F4@(&)I=#,*(V5N9&EF"B-E;'-E"B-D969I;F4@(&)I=#(*(V5N9&EF M9B`@(&)I=#(*(VEF9&5F("`@8FET,PHC=6YD968@("!B:70S"B-E;'-E"B-D
M"B-E;'-E"B-D969I;F4@(&)I=#$*(V5N9&EF"B-E;'-E"B-D969I;F4@(&)I M969I;F4@(&)I=#,*(V5N9&EF"B-E;'-E"B-D969I;F4@(&)I=#(*(V5N9&EF
M=#`*(V5N9&EF"@HC96YD:68@("\J("-I9F1E9B!.7TE37TY/5%]:15)/("HO M"B-E;'-E"B-D969I;F4@(&)I=#$*(V5N9&EF"B-E;'-E"B-D969I;F4@(&)I
M"@HC96YD:68@("\J("-E;'-E(&]F("-I9FYD968@24Y)5$E!3$E:140@*B\* M=#`*(V5N9&EF"@HC96YD:68@("\J("-I9F1E9B!.7TE37TY/5%]:15)/("HO
` M"@HC96YD:68@("\J("-E;'-E(&]F("-I9FYD968@24Y)5$E!3$E:140@*B\*
end `
end
```
## Copyright and CC BY-SA 4.0 License: ## Copyright and CC BY-SA 4.0 License: