Anonymous user
Towers of Hanoi: Difference between revisions
Updated the iterative D version
(→{{header|Euphoria}}: Euphoria example added) |
(Updated the iterative D version) |
||
Line 230:
===Iterative===
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;▼
<lang d>// Code found and then improved by Glenn C. Rhoads,▼
// then some more by M. Kolar (2000).▼
▲import std.stdio, std.conv, std.typetuple;
void main(string[] args) {
▲ // then some more by M. Kolar (2000).
size_t[3] p;▼
▲ size_t[3] p = [(1 << n) - 1, 0, 0];
printf("\n|");
printf(" %d", i);
printf("\n|\n|\n");
foreach (size_t x; 1 .. (1 << n)) {
immutable size_t to = (i2 + i2 / 3) & 3;
for (i = x, j = 1; ; i >>= 1, j <<= 1)▼
for (int w =
// Now j is not the number of the disk to move,
p[fr] &= ~j;▼
▲ p[fr] &= ~j;
}
foreach (size_t k; TypeTuple!(0, 1, 2)) {
printf("\n|");
size_t j = 1 << n;
j >>= 1;
if (j & p[k])
printf(" %
}
}
}
}</lang>
|