Prime Numbers

Prime Numbers Prime Numbers

thales.doa.fmph.uniba.sk
from thales.doa.fmph.uniba.sk More from this publisher
10.12.2012 Views

3.2 Sieving 121 3.2 Sieving Sieving can be a highly efficient means of determining primality and factoring when one is interested in the results for every number in a large, regularly spaced set of integers. On average, the number of arithmetic operations spent per number in the set can be very small, essentially bounded. 3.2.1 Sieving to recognize primes Most readers are likely to be familiar with the sieve of Eratosthenes. In its most common form it is a device for finding the primes up to some number N. StartwithanarrayofN− 1 “ones,” corresponding to the numbers from 2toN. The first one corresponds to “2,” so the ones in locations 4, 6, 8, and so on, are all changed to zeros. The next one is in the position “3,” and we read this as an instruction to change any ones in locations 6, 9, 12, and so on, into zeros. (Entries that are already zeros in these locations are left unchanged.) We continue in this fashion. If the next entry one corresponds to “p,” we change to zero any entry one at locations 2p, 3p, 4p, and so on. However, if p is so large that p2 >N, we may stop this process. This exit point can be readily detected by noticing that when we attempt to sieve by p there are no changes of ones to zeros to be made. At this point the one entries in the list correspond to the primes not exceeding N, while the zero entries correspond to the composites. In passing through the list 2p, 3p, 4p, and so on, one starts from the initial number p and sequentially adds p until we arrive at a number exceeding N. Thus the arithmetic operations in the sieve are all additions. The number of steps in the sieve of Eratosthenes is proportional to N/p, wherep runs over primes. But p≤N N p p≤N = N ln ln N + O(N); (3.1) see Theorem 427 in [Hardy and Wright 1979]. Thus, the number of steps needed per number up to N is proportional to ln ln N. It should be noted that ln ln N, though it does go to infinity, does so very slowly. For example, ln ln N

122 Chapter 3 RECOGNIZING PRIMES AND COMPOSITES unsolved problem to come up with a method of finding all the primes in the interval N,N + N 1/4 that is appreciably faster than individually examining each number. This problem is specified in Exercise 3.46. 3.2.2 Eratosthenes pseudocode We now give practical pseudocode for implementing the ordinary Eratosthenes sieve to find primes in an interval. Algorithm 3.2.1 (Practical Eratosthenes sieve). This algorithm finds all primes in an interval (L, R) by establishing Boolean primality bits for successive runs of B odd numbers. We assume L, R even, with R>L, B | R − L and L>P = ⌈ √ R⌉. We also assume the availability of a table of the π(P ) primes pk ≤ P . 1. [Initialize the offsets] for(k ∈ [2,π(P )]) qk = − 1 2 (L +1+pk) mod pk; 2. [Process blocks] T = L; while(T

122 Chapter 3 RECOGNIZING PRIMES AND COMPOSITES<br />

unsolved problem to come up with a method of finding all the primes in the<br />

interval N,N + N 1/4 that is appreciably faster than individually examining<br />

each number. This problem is specified in Exercise 3.46.<br />

3.2.2 Eratosthenes pseudocode<br />

We now give practical pseudocode for implementing the ordinary Eratosthenes<br />

sieve to find primes in an interval.<br />

Algorithm 3.2.1 (Practical Eratosthenes sieve). This algorithm finds all<br />

primes in an interval (L, R) by establishing Boolean primality bits for successive<br />

runs of B odd numbers. We assume L, R even, with R>L, B | R − L and<br />

L>P = ⌈ √ R⌉. We also assume the availability of a table of the π(P ) primes<br />

pk ≤ P .<br />

1. [Initialize the offsets]<br />

for(k ∈ [2,π(P )]) qk = − 1<br />

2 (L +1+pk) mod pk;<br />

2. [Process blocks]<br />

T = L;<br />

while(T

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

Saved successfully!

Ooh no, something went wrong!