II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
128 c.A.R. HOARE<br />
The program can be written easily<br />
begin n, next:2..N; sieve:powerset 2..N;<br />
end primefinder.<br />
sieve: = range (2, N);<br />
primes: = { };<br />
while sieve ~ empty do<br />
begin next: = min (sieve);<br />
end<br />
primes: v (next };<br />
for n'= next step next until N do<br />
sieve" - {n}<br />
But if N is significantly large, say of the order of 10 000, this program<br />
cannot be directly executed with any acceptable degree of efficiency. The<br />
soluti<strong>on</strong> is to use this program as an abstract model of the algorithm, and<br />
rewrite it in a more efficient fashi<strong>on</strong>, using <strong>on</strong>ly operati<strong>on</strong>s <strong>on</strong> sets not<br />
exceeding the word-length of the computer. We therefore need to declare<br />
an array of words to represent the two sets, assuming that "wordlength"<br />
is an envir<strong>on</strong>ment enquiry giving the number of bits in a word"<br />
primes, sieve:array O.. W of powerset O.. wordlength - 1<br />
where W = (N + 1) + wordlength + 1.<br />
This means that the two sets may be slightly larger than N, but for c<strong>on</strong>-<br />
venience we shall accept that harmless extensi<strong>on</strong>.<br />
In order to access an individual bit of these sets, it is necessary to know<br />
both the wordnumber and the bitnumber. Since we do not wish to use<br />
divisi<strong>on</strong> to find these, we will represent the counting variables n and next as<br />
Cartesian products<br />
n, next: (w, b :integer);<br />
where w indicates the wordnumber and b indicates the bitnumber.<br />
It is now as well to check the efficiency of this representati<strong>on</strong> by recoding<br />
the innermost loop first.<br />
is recoded as"<br />
for n: = next step next until N flo sieve:- {n };<br />
n: = next;<br />
while n. w ~< W do<br />
begin sieve [n. w]: - {n. b };<br />
n.b: = n.b + next.b;