Anonymous user
Towers of Hanoi: Difference between revisions
Updated second D entry
(Added COBOL solution. Corrected GOSUB BASIC example, PL/I and R lang tags. Moved Emacs Lisp example to correct position. Re-formatted BASIC and BASIC256 sections.) |
(Updated second D entry) |
||
Line 462:
===Fast Iterative Version===
See: [http://hanoitower.mkolar.org/shortestTHalgo.html The shortest and "mysterious" TH algorithm]
<lang d>import std.stdio, std.conv, std.typetuple;▼
void main(in string[] args) {
▲ // Code found and then improved by Glenn C. Rhoads,
▲ // then some more by M. Kolar (2000).
immutable size_t n = (args.length > 1) ?
size_t[3] p = [(1 << n) - 1, 0, 0];
// Show the start configuration of the pegs.
'|'.putchar;
foreach_reverse (
printf("
"\n|\n|".puts;
foreach (immutable size_t x; 1 .. (1 << n)) {
{
immutable size_t i1 = x & (x - 1);
Line 484:
size_t j = 1;
for (
// Now j is not the number of the disk to move,
Line 492:
}
// Show the current configuration of pegs.
foreach (immutable size_t k;
"\n|".printf;
size_t j = 1 << n;
foreach_reverse (immutable size_t w; 1 .. n + 1) {
j >>= 1;
if (j & p[k])
Line 501 ⟶ 502:
}
}
▲ putchar('\n');
}
}</lang>
Line 536:
|
|
| 3 2 1
</pre>
=={{header|Dart}}==
|