HOME

TheInfoList



OR:

In
mathematics Mathematics is an area of knowledge that includes the topics of numbers, formulas and related structures, shapes and the spaces in which they are contained, and quantities and their changes. These topics are represented in modern mathematics ...
, the sieve of Pritchard is an
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
for finding all
prime number A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways ...
s up to a specified bound. Like the ancient
sieve of Eratosthenes In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime n ...
, it has a simple conceptual basis in
number theory Number theory (or arithmetic or higher arithmetic in older usage) is a branch of pure mathematics devoted primarily to the study of the integers and arithmetic function, integer-valued functions. German mathematician Carl Friedrich Gauss (1777â ...
. It is especially suited to quick hand computation for small bounds. Whereas the sieve of Eratosthenes marks off each non-prime for each of its prime factors, the sieve of Pritchard avoids considering almost all non-prime numbers by building progressively larger wheels, which represent the pattern of numbers not divisible by any of the primes processed thus far. It thereby achieves a better
asymptotic complexity In computer science, the computational complexity or simply complexity of an algorithm is the amount of resources required to run it. Particular focus is given to computation time (generally measured by the number of needed elementary operations) ...
, and was the first sieve with a running time sublinear in the specified bound. Its asymptotic running-time has not been improved on, and it deletes fewer composites than any other known sieve. It was created in 1979 by Paul Pritchard. Since Pritchard has created a number of other sieve algorithms for finding prime numbers, the sieve of Pritchard is sometimes singled out by being called ''the wheel sieve'' (by Pritchard himself) or ''the dynamic wheel sieve''.


Overview

A
prime number A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways ...
is a
natural number In mathematics, the natural numbers are those numbers used for counting (as in "there are ''six'' coins on the table") and ordering (as in "this is the ''third'' largest city in the country"). Numbers used for counting are called ''Cardinal n ...
that has no natural number
divisor In mathematics, a divisor of an integer n, also called a factor of n, is an integer m that may be multiplied by some integer to produce n. In this case, one also says that n is a multiple of m. An integer n is divisible or evenly divisible by ...
s other than the number 1 and itself. To find all the prime numbers less than or equal to a given integer N, a sieve algorithm examines a
set Set, The Set, SET or SETS may refer to: Science, technology, and mathematics Mathematics *Set (mathematics), a collection of elements *Category of sets, the category whose objects and morphisms are sets and total functions, respectively Electro ...
of candidates in the range 2,3,...,N, and eliminates those that are not prime, leaving the primes at the end. The
sieve of Eratosthenes In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime n ...
examines all of the range, first removing all multiples of the first prime 2, then of the next prime 3, and so on. The sieve of Pritchard instead examines a subset of the range consisting of numbers that occur on successive wheels, which represent the pattern of numbers left after each successive prime is processed by the sieve of Eratosthenes. For i>0 the i'th wheel W_i represents this pattern. It is a subset of the set of numbers between 1 and the product P_i=p_1*p_2*...*p_i of the first i prime numbers (and is said to have an associated ''length'' P_i). This is because adding P_i to a number doesn't change whether or not it is divisible by one of the first i prime numbers, since the remainder on division by any one of these primes is unchanged. So W_1=\ with length P_1=2 represents the pattern of odd numbers; W_2=\ with length P_2=6 represents the pattern of numbers not divisible by 2 or 3; etc. Wheels are so-called because W_i can be usefully visualized as a circle of circumference P_i with its members marked at their corresponding distances from an origin. Then rolling the wheel along the number line marks points corresponding to successive numbers not divisible by one of the first i prime numbers. The animation shows W_2 being rolled up to 30. It's useful to define W_i\rightarrow n for n>0 to be the result of rolling W_i up to n. Then the animation generates W_2\rightarrow 30=\. Note that up to 5^2-1=24, this consists only of 1 and the primes between 5 and 25. The sieve of Pritchard is derived from the observation that this holds generally: for all i>0, the values in W_i\rightarrow are 1 and the primes between p_ and p_^2. It even holds for i=0, where the wheel has length 1 and contains just 1 (representing all the natural numbers). So the sieve of Pritchard starts with the trivial wheel W_0 and builds successive wheels until the square of the wheel's first member after 1 is at least N. Wheels grow very quickly, but only their values up to N are needed and generated. It remains to find a method for generating the next wheel. Note in the animation that W_3=\-\ can be obtained by rolling W_2 up to 30 and then removing 5 times each member of W_2. This also holds generally: for all i\geq 0, W_ = (W_i\rightarrow P_) - \. Rolling W_i past P_i just adds values to W_i, so the current wheel is first extended by getting each successive member starting with w=1, adding P_i to it, and inserting the result in the set. Then the multiples of p_ are deleted. Care must be taken to avoid a number being deleted that itself needs to be multiplied by p_. The sieve of Pritchard as originally presented does so by first skipping past successive members until finding the maximum one needed, and then doing the deletions in reverse order by working back through the set. This is the method used in the first animation above. A simpler approach is just to gather the multiples of p_ in a list, and then delete them. Another approach is given by Gries and Misra. If the main loop terminates with a wheel whose length is less than N, it is extended up to N to generate the remaining primes. The algorithm, for finding all primes up to ''N'', is therefore as follows: # Start with a set ''W''= and ''length''=1 representing wheel 0, and prime ''p''=2. # As long as ''p''2 <= ''N'', do the following ## if ''length'' < ''N'' then ### extend ''W'' by repeatedly getting successive members ''w'' of ''W'' starting with 1 and inserting ''length''+''w'' into ''W'' as long as it doesn't exceed ''p''*''length'' or ''N''; ### increase ''length'' to the minimum of ''p''*''length'' and ''N''. ## repeatedly delete ''p'' times each member of ''W'' by first finding the largest <= ''length'' and then working backwards. ## note the prime ''p'', then set ''p'' to the next member of ''W'' after 1 (or 3 if ''p'' was 2). # if ''length'' < ''N'' then extend ''W'' to ''N'' by repeatedly getting successive members ''w'' of ''W'' starting with 1 and inserting ''length''+''w'' into ''W'' as long as it doesn't exceed ''N''; # On termination, the rest of the primes up to ''N'' are the members of ''W'' after 1.


Example

To find all the prime numbers less than or equal to 150, proceed as follows. Start with wheel 0 with length 1, representing all natural numbers 1, 2, 3...:  1 The first number after 1 for wheel 0 (when rolled) is 2; note it as a prime. Now form wheel 1 with length 2x1=2 by first extending wheel 0 up to 2 and then deleting 2 times each number in wheel 0, to get:  1 The first number after 1 for wheel 1 (when rolled) is 3; note it as a prime. Now form wheel 2 with length 3x2=6 by first extending wheel 1 up to 6 and then deleting 3 times each number in wheel 1, to get  1 5 The first number after 1 for wheel 2 is 5; note it as a prime. Now form wheel 3 with length 5x6=30 by first extending wheel 2 up to 30 and then deleting 5 times each number in wheel 2 (in reverse order!), to get  1 7 11 13 17 19 23 29 The first number after 1 for wheel 3 is 7; note it as a prime. Now wheel 4 has length 7x30=210, so we only extend wheel 3 up to our limit 150. (No further extending will be done now that the limit has been reached.) We then delete 7 times each number in wheel 3 until we exceed our limit 150, to get the elements in wheel 4 up to 150:  1 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 121 127 131 137 139 143 149 The first number after 1 for this partial wheel 4 is 11; note it as a prime. Since we've finished with rolling, we delete 11 times each number in the partial wheel 4 until we exceed our limit 150, to get the elements in wheel 5 up to 150:  1 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 The first number after 1 for this partial wheel 5 is 13. Since 13 squared is at least our limit 150, we stop. The remaining numbers (other than 1) are the rest of the primes up to our limit 150. Just 8 composite numbers are removed, once each. The rest of the numbers considered (other than 1) are prime. In comparison, the natural version of Eratosthenes sieve (stopping at the same point) removes composite numbers 184 times.


Pseudocode

The sieve of Pritchard can be expressed in
pseudocode In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine re ...
, as follows: algorithm Sieve of Pritchard is input: an integer ''N'' >= 2. output: the set of prime numbers in . let ''W'' and ''Pr'' be sets of
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language ...
values, and all other variables
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language ...
values. ''k'', ''W'', ''length'', ''p'', ''Pr'' := 1, , 2, 3, ; while ''p''2 <= ''N'' do if (''length'' < ''N'') then Extend ''W'',''length'' to minimum of ''p''*''length'',''N''; Delete multiples of ''p'' from ''W''; Insert ''p'' into ''Pr''; ''k'', ''p'' := ''k''+1, next(''W'', 1) if (''length'' < ''N'') then Extend ''W'',''length'' to ''N''; return ''Pr'' \cup ''W'' - ; where next(''W'', w) is the next value in the ordered set ''W'' after ''w''. procedure Extend ''W'',''length'' to ''n'' is
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language ...
w, x; ''w'', ''x'' := 1, ''length''+1; while ''x'' <= ''n'' do Insert ''x'' into ''W''; ''w'' := next(''W'',''w''); ''x'' := ''length'' + ''w''; ''length'' := ''n''; procedure Delete multiples of ''p'' from ''W'',''length'' is
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language ...
w; ''w'' := ''p''; while ''p''*''w'' <= ''length'' do ''w'' := next(''W'',''w''); while ''w'' > 1 do ''w'' := prev(''W'',''w''); Remove ''p''*''w'' from ''W''; where prev(''W'', w) is the previous value in the ordered set ''W'' before ''w''. The algorithm can be initialized with W_0 instead of W_1 at the minor complicaion of making next(''W'',1) a special case when ''k'' = 0. This abstract algorithm uses ordered sets supporting the operations of insertion of a value greater than the maximum, deletion of a member, getting the next value after a member, and getting the previous value before a member. Using one of
Mertens' theorems In number theory, Mertens' theorems are three 1874 results related to the density of prime numbers proved by Franz Mertens.F. Mertens. J. reine angew. Math. 78 (1874), 46–6Ein Beitrag zur analytischen Zahlentheorie/ref> "Mertens' theorem" may a ...
(the third) it can be shown to use O(N/\log\log N) of these operations and additions and multiplications.


Implementation

An array-based doubly-linked list ''s'' can be used to implement the ordered set ''W'', with ''s'' 'w''storing next(''W'',''w'') and ''s'' 'w''-1storing prev(''W'',''w''). This permits each abstract operation to be implemented in a small number of operations. (The array can also be used to store the set ''Pr'' "for free".) Therefore the
time complexity In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
of the sieve of Pritchard to calculate the primes up to N in the
random access machine In computer science, random-access machine (RAM) is an abstract machine in the general class of register machines. The RAM is very similar to the counter machine but with the added capability of 'indirect addressing' of its registers. Like the cou ...
model is O(N/\log\log N) operations on words of size O(\log N). Pritchard also shows how multiplications can be eliminated by using very small multiplication tables, so the
bit complexity In computer science, the computational complexity or simply complexity of an algorithm is the amount of resources required to run it. Particular focus is given to time complexity, computation time (generally measured by the number of needed elemen ...
is O(N\log N/\log\log N) bit operations. In the same model, the
space complexity The space complexity of an algorithm or a computer program is the amount of memory space required to solve an instance of the computational problem as a function of characteristics of the input. It is the memory required by an algorithm until it ex ...
is O(N) words, i.e., O(N\log N) bits. The sieve of Eratosthenes requires only 1 bit for each candidate in the range 2 through N, so its space complexity is lower at O(N) bits. Note that space needed for the primes is not counted, since they can printed or written to external storage as they are found. Pritchard presented a variant of his sieve that requires only O(N/\log\log N) bits without compromising the sublinear time complexity, making it asymptotically superior to the natural version of the sieve of Eratostheses in both time and space. However, the sieve of Eratostheses can be optimized to require much less memory by operating on successive segments of the natural numbers. Its space complexity can be reduced to O(\sqrt N) bits without increasing its time complexity This means that in practice it can be used for much larger limits N than would otherwise fit in memory, and also take advantage of fast cache memory. For maximum speed it is also optimized using a small wheel to avoid sieving with the first few primes (although this does not change its asymptotic time complexity). Therefore the sieve of Pritchard is not competitive as a practical sieve over sufficiently large ranges.


Geometric model

At the heart of the sieve of Pritchard is an algorithm for building successive wheels. It has a simple geometric model as follows: # Start with a circle of circumference 1 with a mark at 1 # To generate the next wheel: ## Go around the wheel and find (the distance to) the first mark after 1; call it p ## Create a new circle with p times the circumference of the current wheel ## Roll the current wheel around the new circle, marking it where a mark touches it ## Magnify the current wheel by p and remove the marks that coincide Note that for the first 2 iterations it is necessary to continue round the circle until 1 is reached again. The first circle represents W_0=\, and successive circles represent wheels W_1, W_2,.... The animation on the right shows this model in action up to W_3. It is apparent from the model that wheels are symmetric. This is because P_k-w is not divisible by one of the first k primes if and only if w is not so divisible. It is possible to exploit this to avoid processing some composites, but at the cost of a more complex algorithm.


Related sieves

Once the wheel in the sieve of Pritchard reaches its maximum size, the remaining operations are equivalent to those performed by Euler's sieve. The sieve of Pritchard is unique in conflating the set of prime candidates with a dynamic wheel used to speed up the sifting process. But a separate static wheel (as frequently used to speed up the sieve of Eratosthenes) can give an O(\log\log N) speedup to the latter, or to linear sieves, provided it is large enough (as a function of N). Examples are the use of the largest wheel of length not exceeding \sqrt/log^N to get a version of the sieve of Eratosthenes that takes O(N) additions and requires only O(\sqrt N/\log\log N) bits, and the speedup of the naturally linear
sieve of Atkin In mathematics, the sieve of Atkin is a modern algorithm for finding all prime numbers up to a specified integer. Compared with the ancient sieve of Eratosthenes, which marks off multiples of primes, the sieve of Atkin does some preliminary work a ...
to get a sublinear optimized version. Bengalloun found a linear ''smoothly incremental'' sieve, i.e., one that (in theory) can run indefinitely and takes a bounded number of operations to increment the current bound N. He also showed how to make it sublinear by adapting the sieve of Pritchard to incrementally build the next dynamic wheel while the current one is being used. Pritchard showed how to avoid multiplications, thereby obtaining the same asymptotic bit-complexity as the sieve of Pritchard. Runciman provides a functional algorithm inspired by the sieve of Pritchard.


See also

*
Sieve of Eratosthenes In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime n ...
*
Sieve of Atkin In mathematics, the sieve of Atkin is a modern algorithm for finding all prime numbers up to a specified integer. Compared with the ancient sieve of Eratosthenes, which marks off multiples of primes, the sieve of Atkin does some preliminary work a ...
*
Sieve theory Sieve theory is a set of general techniques in number theory, designed to count, or more realistically to estimate the size of, sifted sets of integers. The prototypical example of a sifted set is the set of prime numbers up to some prescribed lim ...


References

{{DEFAULTSORT:Sieve Of Pritchard Primality tests Articles with example pseudocode Sieve theory Algorithms