07.01.2013 Views

Dietzfelbinger M. Primality testing in polynomial time ... - tiera.ru

Dietzfelbinger M. Primality testing in polynomial time ... - tiera.ru

Dietzfelbinger M. Primality testing in polynomial time ... - tiera.ru

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

40 3. Fundamentals from Number Theory<br />

It is not very hard to see that the numbers that are left unmarked are<br />

exactly the prime numbers <strong>in</strong> the <strong>in</strong>terval [2,n]. Indeed, no prime number is<br />

ever marked, s<strong>in</strong>ce only numbers sj with s ≥ j ≥ 2 are marked. Conversely, if<br />

k ≤ n is composite, then write k as a product ab with b ≥ a ≥ 2. Obviously,<br />

then, a ≤ √ k ≤ √ n. By Lemma 3.5.2 there is some prime number p that<br />

divides a. Thenwemaywritek = sp for some s ≥ b, hence s ≥ p. Whenj<br />

atta<strong>in</strong>s the value p, this prime number turns out to be unmarked, and the<br />

number k becomes marked as the multiple sp of p.<br />

A slightly more elaborate version of the Sieve of Eratosthenes, given next,<br />

even marks each composite number <strong>in</strong> [2,n] with its smallest prime divisor.<br />

Algorithm 3.5.4 (The Sieve of Eratosthenes)<br />

Input: Integer n ≥ 2<br />

Method:<br />

1 m[2..n]: array of <strong>in</strong>teger;<br />

2 for j from 2 to n do m[j] ← 0;<br />

3 j ← 2;<br />

4 while j · j ≤ n do<br />

5 if m[j] =0then<br />

6 i ← j · j;<br />

7 while i ≤ n do<br />

8 if m[i] =0then m[i] ← j;<br />

9 i ← i + j;<br />

10 j ← j +1;<br />

11 return m[2..n];<br />

The algorithm varies the idea sketched above as follows. Instead of attach<strong>in</strong>g<br />

a “mark” to k, m[k] is assigned a nonzero value. The j-loop (l<strong>in</strong>es<br />

4–10) treatsthenumbersj =2, 3,...,⌊ √ n⌋, <strong>in</strong>order.Ifm[j] turns out to<br />

be nonzero when j has value j, then<strong>in</strong>thei-loop (l<strong>in</strong>es 7–9) theunmarked<br />

multiples of j are marked with j. — The algorithm is easily analyzed. As<br />

before, if k ≤ n is a prime number, then the value m[k] stays 0 throughout.<br />

Assume now that k ≤ n is composite. Consider the smallest prime number<br />

p that is a divisor of k, andwritek = sp for some s ≥ 2. By Lemma 3.5.2,<br />

s is divisible by some prime number p ′ , which then must exceed p; hence<br />

p 2 ≤ sp = k ≤ n. From the algorithm it is then obvious that <strong>in</strong> the iteration<br />

of the j-loop (l<strong>in</strong>es 4–10) <strong>in</strong> which the variable j conta<strong>in</strong>s p the array component<br />

m[k] will be assigned the value p. (It cannot get a value different from 0<br />

before that, s<strong>in</strong>ce this would imply that k is divisible by some number j

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!