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>// Code found and then improved by Glenn C. Rhoads,
<lang d>import std.stdio, std.conv, std.typetuple;
// then some more by M. Kolar (2000).
 
void main(in string[] args) {
<lang d> import stdcore.stdc.stdio, std.conv, std.typetuple;
// Code found and then improved by Glenn C. Rhoads,
// then some more by M. Kolar (2000).
 
immutable size_t n = (args.length > 1) ? to!size_t(args[1]).to!size_t : 3;
size_t[3] p = [(1 << n) - 1, 0, 0];
 
// Show the start configuration of the pegs.
printf("\n|");
'|'.putchar;
foreach_reverse (size_timmutable i; 1 .. n + 1)
printf(" %zd", i);
printf("\n|\n|\n %d", i);
"\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 (intsize_t w = x; !(w & 1); w >>= 1, j <<= 1) {}
 
// Now j is not the number of the disk to move,
Line 492:
}
 
// Show the current configuration of pegs.
foreach (immutable(size_t) k; TypeTuple!(0, 1, 2)) {
foreach (immutable size_t k; printfTypeTuple!("\n|"0, 1, 2);) {
"\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').putchar;
 
putchar('\n');
}
}</lang>
Line 536:
|
|
| 3 2 1</pre>
</pre>
 
=={{header|Dart}}==