Conjunctive normal form: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>LilHelpa
m →‎Converting from first-order logic: typo: repatedly → repeatedly
en>Myasuda
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
[[File:Sieve of Eratosthenes animation.gif|right|frame|Sieve of Eratosthenes: algorithm steps for primes below 121 (including optimization of starting from prime's square).]]
Name: Lettie Brownlow<br>My age: 25<br>Country: Australia<br>City: Booral <br>ZIP: 2425<br>Address: 44 Ugoa Street<br><br>My web blog; [http://safedietplansforwomen.com/how-to-lose-weight-fast lose weight]
 
In [[mathematics]], the '''[[Sieve theory|sieve]] of Eratosthenes''' ({{lang-el|κόσκινον Ἐρατοσθένους}}), one of a number of [[Generating primes#Prime sieves|prime number sieve]]s, is a simple, ancient [[algorithm]] for finding all [[prime number]]s up to any given limit. It does so by iteratively marking as [[composite number|composite]] (i.e. not prime) the multiples of each prime, starting with the multiples of 2.<ref name="horsley">Horsley, Rev. Samuel, F. R. S., "''{{lang|el|Κόσκινον Ερατοσθένους}}''  or, The Sieve of Eratosthenes. Being an account of his method of finding all the Prime Numbers," [http://www.jstor.org/stable/106053 ''Philosophical Transactions'' (1683–1775), Vol. 62. (1772), pp. 327–347].</ref>
 
The multiples of a given prime are generated as a sequence of numbers starting from that prime, with [[arithmetic progression|constant difference between them]]<!-- "ταυτη διαχωριζομεν" says Nicomachus, p.29, which I take to mean "equally separated"--> which is equal to that prime.<ref name="horsley" /> This is the sieve's key distinction from using [[trial division]] to sequentially test each candidate number for divisibility by each prime.<ref name="ONeill" />
 
The sieve of Eratosthenes is one of the most efficient ways to find all of the smaller primes (below 10 million or so).<ref>The Prime Glossary: "The Sieve of Eratosthenes", http://primes.utm.edu/glossary/page.php?sort=SieveOfEratosthenes, references 16. November 2008.</ref> It is named after [[Eratosthenes|Eratosthenes of Cyrene]], a [[Greek mathematics|Greek mathematician]]; although none of his works has survived, the sieve was described and attributed to Eratosthenes in the ''[[Introduction to Arithmetic]]'' by [[Nicomachus]].<ref>[[Nicomachus]], ''Introduction to Arithmetic'', I, 13. [http://www.archive.org/stream/nicomachigerasen00nicouoft#page/29/mode/1up]</ref>
 
The sieve may be used to find primes in [[arithmetic progression]]s.<ref>J. C. Morehead, "Extension of the Sieve of Eratosthenes to arithmetical progressions and applications", [http://www.jstor.org/stable/1967477 Annals of Mathematics, Second Series '''10''':2 (1909), pp. 88–104].</ref>
 
==Algorithm description==
{{quote box|fontsize = 95%|''Sift the Two's and Sift the Three's,''<br />''The Sieve of Eratosthenes.''<br />''When the multiples sublime,''<br />''The numbers that remain are Prime.''|quoted=1|salign=center|source=Anonymous<ref>Clocksin, William F., Christopher S. Mellish, Programming in Prolog, 1981, p. 174. ISBN 3-540-11046-1.</ref><ref>{{cite web|url=http://c2.com/cgi/wiki?SieveOfEratosthenes|title=Sieve Of Eratosthenes|last=Merritt|first=Doug|date=December 14, 2008|accessdate=2009-03-26}}</ref><ref>{{cite web|url=http://www.cs.uku.fi/~mnykanen/FOH/lectures.pdf|title=An Introduction to Functional Programming with the Programming Language Haskell|last=Nykänen|first=Matti|date=October 26, 2007|accessdate=2009-03-26}}</ref>}}
 
A [[prime number]] is a [[natural number]] which has exactly two distinct natural number [[divisor]]s: [[1 (number)|1]] and itself.
 
To find all the prime numbers less than or equal to a given integer ''n'' by Eratosthenes' method:
 
# Create a list of consecutive integers from 2 through ''n'': (2, 3, 4, ..., ''n'').
# Initially, let ''p'' equal 2, the first prime number.
# Starting from ''p'', enumerate its multiples by counting to ''n'' in increments of ''p'', and mark them in the list (these will be 2''p'', 3''p'', 4''p'', etc.; the ''p'' itself should not be marked).
# Find the first number greater than ''p'' in the list that is not marked. If there was no such number, stop. Otherwise, let ''p'' now equal this new number (which is the next prime), and repeat from step 3.
 
When the algorithm terminates, all the numbers in the list that are not marked are prime.
 
The main idea here is that every value for ''p'' is prime, because we have already marked all the multiples of the numbers less than ''p''. Note that some of the numbers being marked may have already been marked earlier (e.g. 15 will be marked both for 3 and 5).
 
As a refinement, it is sufficient to mark the numbers in step 3 starting from ''p''<sup>2</sup>, as all the smaller multiples of ''p'' will have already been marked at that point. This means that the algorithm is allowed to terminate in step 4 when ''p''<sup>2</sup> is greater than ''n''.<ref name="horsley" /> <!-- This does not appear in the algorithm as described by Nicomachus.<ref>Nicomachus, ibid., p. 31, where e.g. 93 is marked as a multiple of 31.</ref> ---- The above is based on wrong reading of the table in Nicomachus p. 31: main factors actually appear in upper row in cells, and their corresponding coefficients in the bottom row; 5 first appears in bottom row for 15 but in top row for 25, and 7 for 49. For 93, 31 is marked in bottom row, below 3. (He also marks 81 as 9*9 and 3*27, so wasn't working with only prime factors, but that's besides the point here.) What the text is saying in Latin or Greek, I have no idea. -->
 
Another refinement is to initially list odd numbers only, (3, 5, ..., ''n''), and count in increments of 2''p'' in step 3, thus marking only odd multiples of ''p''. This actually appears in the original algorithm.<ref name="horsley" /> <!--  <ref>Nicomachus, ibid., p. 31, where only odd numbers appear in the table.</ref>  Again, the table is not by Nicomachus; it is in the margins, and in Latin.  -->  This can be generalized with [[wheel factorization]], forming the initial list only from numbers [[coprime]] with the first few primes and not just from odds, i.e. numbers coprime with ''2''.<ref name="Runciman">Colin Runciman, [http://portal.acm.org/citation.cfm?id=969898 "FUNCTIONAL PEARL: Lazy wheel sieves and spirals of primes"], Journal of Functional Programming, Volume 7 Issue 2, March 1997; also [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.55.7096&rank=3 here].</ref>
 
===Incremental sieve===
An incremental formulation of the sieve<ref name="ONeill">O'Neill, Melissa E., [http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf "The Genuine Sieve of Eratosthenes"], Journal of Functional Programming, Published online by Cambridge University Press 9 October 2008 {{doi|10.1017/S0956796808007004}}, pp. 10, 11 (contains two incremental sieves in Haskell: a priority-queue–based one by O'Neill and a list–based, by Richard Bird).</ref> generates primes indefinitely (i.e. without an upper bound) by interleaving the generation of primes with the generation of their multiples (so that primes can be found in gaps between the multiples), where the multiples of each prime ''p'' are generated directly, by counting up from the square of the prime in increments of ''p'' (or ''2p'' for odd primes). The generation must be initiated only when the prime's square is reached, to avoid adverse effects on efficiency.
 
===Trial division===
[[Trial division]] can be used to produce primes by filtering out the composites found by testing each candidate number for divisibility by its preceding primes. It is often confused with the sieve of Eratosthenes, although the latter directly generates the composites instead of testing for them. Trial division has worse theoretical [[computational complexity|complexity]] than that of the sieve of Eratosthenes in generating ranges of primes.<ref name="ONeill"/>
 
When testing each candidate number, the optimal trial division algorithm uses just those prime numbers not exceeding its square root. The widely known 1975 [[functional programming|functional]] code by [[David Turner (computer scientist)|David Turner]]<ref>Turner, David A. SASL language manual. Tech. rept. CS/75/1. Department of Computational Science, University of St. Andrews 1975. (<code>sieve (p:xs) = p : sieve [x | x <- xs, rem x p > 0]; primes = sieve [2..]</code>)</ref> is often presented as an example of the sieve of Eratosthenes<ref name="Runciman"/> but is actually a sub-optimal trial division algorithm.<ref name="ONeill"/>
 
==Example==
To find all the prime numbers less than or equal to 30, proceed as follows.
 
First generate a list of integers from 2 to 30:
 
  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 
First number in the list is 2; cross out every 2nd number in the list after it (by counting up in increments of 2), i.e. all the multiples of 2:
 
  2  3 <s> 4 </s> 5 <s> 6 </s> 7 <s> 8 </s> 9  <s>10</s> 11 <s>12</s> 13 <s>14</s> 15 <s>16</s> 17 <s>18</s> 19 <s>20</s> 21 <s>22</s> 23 <s>24</s> 25 <s>26</s> 27 <s>28</s> 29 <s>30</s>
 
Next number in the list after 2 is 3; cross out every 3rd number in the list after it (by counting up in increments of 3), i.e. all the multiples of 3:
 
  2  3 <s> 4 </s> 5 <s> 6 </s> 7 <s> 8 </s><s> 9 </s><s> 10</s> 11 <s>12</s> 13 <s>14 </s><s>15 </s><s>16</s> 17 <s>18</s> 19 <s>20 </s><s>21 </s><s>22</s> 23 <s>24</s> 25 <s>26 </s><s>27 </s><s>28</s> 29 <s>30</s>
 
Next number not yet crossed out in the list after 3 is 5; cross out every 5th number in the list after it (by counting up in increments of 5), i.e. all the multiples of 5:
 
  2  3 <s> 4 </s> 5 <s> 6 </s> 7 <s> 8 </s><s> 9 </s><s> 10</s> 11 <s>12</s> 13 <s>14 </s><s>15 </s><s>16</s> 17 <s>18</s> 19 <s>20 </s><s>21 </s><s>22</s> 23 <s>24 </s><s>25 </s><s>26 </s><s>27 </s><s>28</s> 29 <s>30</s>
 
Next number not yet crossed out in the list after 5 is 7; the next step would be to cross out every 7th number in the list after it, but they are all already crossed out at this point, as these numbers (14, 21, 28) are also multiples of smaller primes because 7*7 is greater than 30. The numbers left not crossed out in the list at this point are all the prime numbers below 30:
 
  2  3    5    7          11    13          17    19          23                29
 
==Algorithm complexity==
Time complexity of calculating all primes below ''n'' in the [[random access machine]] model is <math>O(n \log\log n)</math> operations, a direct consequence of the fact that the [[prime harmonic series]] asymptotically approaches <math>\log \log n</math>.
It has an exponential time complexity with regard to input size, though, which makes it a [[Pseudo-polynomial time|pseudo-polynomial]] algorithm.
 
The [[bit complexity]] of the algorithm is <math>O(n (\log n) (\log \log n))</math> bit operations with a memory requirement of <math>O(n)</math>.<ref>Pritchard, Paul, "Linear prime-number sieves: a family tree," ''Sci. Comput. Programming'' '''9''':1 (1987), pp. 17–35.</ref>
 
The segmented version of the sieve of Eratosthenes, with basic optimizations, uses <math>O(n)</math> operations and <math>O(n^{1/2}\log\log n/\log n)</math> bits of memory.<ref>A. O. L. Atkin and D. J. Bernstein, [http://www.ams.org/mcom/2004-73-246/S0025-5718-03-01501-1/S0025-5718-03-01501-1.pdf "Prime sieves using binary quadratic forms"], ''Mathematics of Computation'' '''73''' (2004), pp. 1023–1030.</ref>
 
==Implementation==
An optimized implementation (starting from squares), in [[pseudocode]]:<ref name="sedgewick">{{cite book
|last1=Sedgewick |first1=Robert |title=Algorithms in C++
|publisher=Addison-Wesley |year=1992 |isbn=0-201-51059-6 |accessdate=2011-10-26
}}, p. 16.</ref><ref name="intro">[ftp://ftp.cs.wisc.edu/pub/techreports/1990/TR909.pdf Jonathan Sorenson, An Introduction to Prime Number Sieves], Computer Sciences Technical Report #909, Department of Computer Sciences University of Wisconsin-Madison, January 2 1990 (the use of optimization of starting from squares, and thus using only the numbers whose square is below the upper limit, is shown).</ref>
 
'''Input''': an integer ''n'' > 1
&nbsp;<!-- these &nbsp; prevent bots messing it up -->
Let ''A'' be an array of Boolean values, indexed by integers 2 to ''n'',
initially all set to '''true'''.
&nbsp;
  '''for''' ''i'' = 2, 3, 4, ..., not exceeding {{math|''{{sqrt|n}}''}}:
  '''if''' ''A''[''i''] is '''true''':
    '''for''' ''j'' = ''i<sup>2</sup>'', ''i<sup>2</sup>+i'', ''i<sup>2</sup>+2i'', ..., not exceeding ''n''&thinsp;:
      ''A''[''j''] := '''false'''
 
The result of the algorithm is an array <code>A[]</code> where  <code>A[i]</code> is true  if and only if  <code>i</code> is prime.
 
Large ranges may not fit entirely in memory. In these cases it is necessary to use a ''segmented'' sieve where only portions of the range are sieved at a time.<ref>Crandall & Pomerance, ''Prime Numbers: A Computational Perspective'', second edition, Springer: 2005, pp. 121–24.</ref> 
 
For ranges with upper limit {{math|''n''}} so large that the sieving primes below {{math|''{{sqrt|n}}''}} as required by the sieve of Eratosthenes cannot fit in memory, a slower but much more space-efficient sieve like that of Sorenson can be used instead.<ref>J. Sorenson, [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.94.1737 The pseudosquares prime sieve], Proceedings of the 7th International Symposium on Algorithmic Number Theory. (ANTS-VII, 2006).</ref>
 
==Euler's Sieve==
Euler's [[Proof of the Euler product formula for the Riemann zeta function#Proof_of_the_Euler_product_formula|proof of the zeta product formula]] contains a version of the sieve of Eratosthenes in which each composite number is eliminated exactly once.<ref name="intro" /> It, too, starts with a [[list (computing)|list]] of numbers from ''2'' to ''n'' in order. On each step the first element is identified as the next prime and the results of multiplying this prime with each element of the list are marked in the list for subsequent deletion. The initial element and the marked elements are then removed from the working sequence, and the process is repeated:
 
[2] (3) 5  7  <u>9</u>  11  13 <u>15</u> 17 19 <u>21</u> 23 25 <u>27</u> 29 31 <u>33</u> 35 37 <u>39</u> 41 43 <u>45</u> 47 49 <u>51</u> 53 55 <u>57</u> 59 61 <u>63</u> 65 67 <u>69</u> 71 73 <u>75</u> 77 79  ...
[3]    (5) 7    11  13    17 19    23 <u>25</u>    29 31    <u>35</u> 37    41 43    47 49    53 <u>55</u>    59 61    <u>65</u> 67    71 73    77 79  ...
[4]      (7)    11  13    17 19    23      29 31      37    41 43    47 <u>49</u>    53      59 61      67    71 73    <u>77</u> 79  ...
[5]            (11) 13    17 19    23      29 31      37    41 43    47      53      59 61      67    71 73      79  ...
[...]
 
Here the example is shown starting from odds, after the first step of the algorithm. Thus, on the ''k''th step all the remaining multiples of the ''k''th prime are removed from the list, which will thereafter contain only numbers coprime with the first ''k'' primes (cf., [[wheel factorization]]), so that the list will start with the next prime, and all the numbers in it below the square of its first element will be prime too.
 
Thus, when generating a bounded sequence of primes, when the next identified prime exceeds the square root of the upper limit, all the remaining numbers in the list are prime.<ref name="intro" /> In the example given above that is achieved on identifying 11 as next prime, giving a list of all primes less than or equal to 80.
 
Note that numbers that will be discarded by a step are still used while marking the multiples in that step, e.g., for the multiples of 3 it is {{math|1=3 · 3 = 9}}, {{math|1=3 · 5 = 15}}, {{math|1=3 · 7 = 21}}, {{math|1=3 · '''''9''''' = 27}}, ..., {{math|1=3 · '''''15''''' = 45}}, ... , so care must be taken dealing with this.<ref name="intro" />
 
==See also==
* [[Sieve of Atkin]]
* [[Sieve of Sundaram]]
* [[Sieve theory]]
 
==References==
{{Reflist|2}}
 
==External links==
* [http://www.encyclopediaofmath.org/index.php/Eratosthenes,_sieve_of ''Eratosthenes, sieve of'' at Encyclopaedia of Mathematics]
* [http://www.hbmeyer.de/eratosiv.htm Interactive JavaScript Page]
* [http://demonstrations.wolfram.com/SieveOfEratosthenes/ Sieve of Eratosthenes] by George Beck, [[Wolfram Demonstrations Project]].
* [http://www.haskell.org/haskellwiki/Prime_numbers Sieve of Eratosthenes in Haskell]
* [http://www.algolist.net/Algorithms/Number_theoretic_algorithms/Sieve_of_Eratosthenes Sieve of Eratosthenes algorithm illustrated and explained. Java and C++ implementations.]
* [http://zsmith.co/primes.html A related sieve written in x86 assembly language]
* [http://sites.google.com/site/bbuhrow/ A highly optimized Sieve of Eratosthenes in C]
* [http://paratechnical.blogspot.com/2011/01/c-implementation-of-parallel-sieve-of.html A parallel implementation in C#]
* [http://c2.com/cgi/wiki?SieveOfEratosthenesInManyProgrammingLanguages SieveOfEratosthenesInManyProgrammingLanguages c2 wiki page]
* [http://wwwhomes.uni-bielefeld.de/achim/prime_sieve.html The Art of Prime Sieving] Sieve of Eratosthenes in C from 1998 with nice features and algorithmic tricks explained.
 
<!--spacing-->
{{number theoretic algorithms}}
 
{{DEFAULTSORT:Sieve Of Eratosthenes}}
[[Category:Primality tests]]
[[Category:Articles with example pseudocode]]
[[Category:Sieve theory| ]]
[[Category:Algorithms]]

Latest revision as of 18:17, 1 January 2015

Name: Lettie Brownlow
My age: 25
Country: Australia
City: Booral
ZIP: 2425
Address: 44 Ugoa Street

My web blog; lose weight