Article View: a.bsu.programming
Article #86745Re: Towers of Hanoi (nonrecursive)
From: Ahto Truu
Date: Wed, 08 Oct 1997 00:00
Date: Wed, 08 Oct 1997 00:00
28 lines
1807 bytes
1807 bytes
Deniz Tortop wrote:
> Desperately looking for the iterative version of the Towers of Hanoi
> algorithm. Pseudo code or any programming language is more than enough.
Courtesy of John Tromp (Pascal translation by me, original was in C):
procedure p (n : word);
var i : word;
begin
for i := 1 to 1 shl n - 1 do
writeln (i and (i - 1) mod 3, ' -> ', (i or (i - 1) + 1) mod 3);
end;
And this is my slightly improved version:
procedure p (n : word);
var i : word;
begin
for i := 1 to 1 shl n - 1 do
writeln (i and (i - 1) shl (n and 1) mod 3, ' -> ',
(i or (i - 1) + 1) shl (n and 1) mod 3);
end;
--
Ahto Truu (ahto@apt.ee)
Technical Details
Message-ID:
<343B9B41.A40@this.place>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<61etj1$4eu@dfw-ixnews3.ix.netcom.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fe306,5b0c24fabb02a1d7 X-Google-Attributes: gidfe306,public X-Google-Thread: f5380,5b0c24fabb02a1d7 X-Google-Attributes: gidf5380,public X-Google-Thread: 125116,5b0c24fabb02a1d7 X-Google-Attributes: gid125116,public X-Google-Thread: ffead,5b0c24fabb02a1d7 X-Google-Attributes: gidffead,public X-Google-Thread: 107d8b,5b0c24fabb02a1d7 X-Google-Attributes: gid107d8b,public X-Google-Thread: 108717,5b0c24fabb02a1d7 X-Google-Attributes: gid108717,public X-Google-Thread: ff2b1,5b0c24fabb02a1d7 X-Google-Attributes: gidff2b1,public X-Google-Thread: 105753,5b0c24fabb02a1d7 X-Google-Attributes: gid105753,public From: Ahto Truu <no.spam.wanted@this.place> Subject: Re: Towers of Hanoi (nonrecursive) Date: 1997/10/08 Message-ID: <343B9B41.A40@this.place> X-Deja-AN: 278709955 References: <61etj1$4eu@dfw-ixnews3.ix.netcom.com> To: Deniz Tortop <tortop@wpi.edu> Organization: AS APT Newsgroups: a.bsu.programming,adass.iraf.programming,comp.programming,comp.sys.be.help,comp.sys.be.programmer,comp.sys.cbm,netcom.programmers,rec.games.programmer