Jump to content

Examine individual changes

This page allows you to examine the variables generated by the Edit Filter for an individual change.

Variables generated for this change

VariableValue
Edit count of the user (user_editcount)
3
Name of the user account (user_name)
'Souljaboy4'
Age of the user account (user_age)
11234974
Groups (including implicit) the user is in (user_groups)
[ 0 => '*', 1 => 'user' ]
Rights that the user has (user_rights)
[ 0 => 'createaccount', 1 => 'read', 2 => 'edit', 3 => 'createtalk', 4 => 'writeapi', 5 => 'viewmywatchlist', 6 => 'editmywatchlist', 7 => 'viewmyprivateinfo', 8 => 'editmyprivateinfo', 9 => 'editmyoptions', 10 => 'abusefilter-log-detail', 11 => 'urlshortener-create-url', 12 => 'centralauth-merge', 13 => 'abusefilter-view', 14 => 'abusefilter-log', 15 => 'vipsscaler-test', 16 => 'collectionsaveasuserpage', 17 => 'reupload-own', 18 => 'move-rootuserpages', 19 => 'createpage', 20 => 'minoredit', 21 => 'editmyusercss', 22 => 'editmyuserjson', 23 => 'editmyuserjs', 24 => 'purge', 25 => 'sendemail', 26 => 'applychangetags', 27 => 'spamblacklistlog', 28 => 'mwoauthmanagemygrants' ]
Whether the user is editing from mobile app (user_app)
false
Whether or not a user is editing through the mobile interface (user_mobile)
false
Page ID (page_id)
73415
Page namespace (page_namespace)
0
Page title without namespace (page_title)
'Sieve of Eratosthenes'
Full page title (page_prefixedtitle)
'Sieve of Eratosthenes'
Edit protection level of the page (page_restrictions_edit)
[]
Last ten users to contribute to the page (page_recent_contributors)
[ 0 => 'Tbreen15', 1 => 'Souljaboy4', 2 => 'WillNess', 3 => 'D.Lazard', 4 => '2600:8801:3586:4800:3CA6:4536:59EE:5598', 5 => 'Elestrophe', 6 => 'Minecraft Group', 7 => 'Bruce1ee', 8 => 'Citation bot', 9 => '23.243.180.148' ]
Page age in seconds (page_age)
651438871
Action (action)
'edit'
Edit summary/reason (summary)
''
Old content model (old_content_model)
'wikitext'
New content model (new_content_model)
'wikitext'
Old page wikitext, before the edit (old_wikitext)
'{{short description|Ancient algorithm for generating prime numbers}} {{For|the sculpture|The Sieve of Eratosthenes (sculpture)}} [[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).]] In [[mathematics]], the '''quandale method''' is an 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 first prime number, 2. The multiples of a given prime are generated as a sequence of numbers starting from that prime, with [[arithmetic progression|constant difference between them]] that is equal to that prime.<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," [https://www.jstor.org/stable/106053 ''Philosophical Transactions'' (1683–1775), Vol. 62. (1772), pp. 327–347].</ref> 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" /> Once all the multiples of each discovered prime have been marked as composites, the remaining unmarked numbers are primes. The earliest known reference to the sieve ({{lang-grc|κόσκινον Ἐρατοσθένους aa willie is the throut goat}}, ''kóskinon Eratosthénous'') is in [[Nicomachus|Nicomachus of Gerasa]]'s ''[[Introduction to Arithmetic]]'',<ref name=nicomachus>{{citation|editor-first=Richard|editor-last=Hoche|editor-link=Richard Hoche|title=Nicomachi Geraseni Pythagorei Introductionis arithmeticae libri II, chapter XIII, 3|year=1866|location= Leipzig|publisher= B.G. Teubner|page=30|url=https://archive.org/stream/nicomachigerasen00nicouoft#page/30/mode/2up}}</ref> an early 2nd cent. CE book which attributes it to [[Eratosthenes|Eratosthenes of Cyrene]], a 3rd cent. BCE [[Greek mathematics|Greek mathematician]], though describing the sieving by odd numbers instead of by primes.<ref name=nicomachus1926>{{citation|author=Nicomachus of Gerasa|title=Introduction to Arithmetic; translated into English by Martin Luther D'Ooge ; with studies in Greek arithmetic by Frank Egleston Robbins and Louis Charles Karpinski, chapter XIII, 3|year=1926|location=New York|publisher=The Macmillan Company|page=204}}</ref> One of a number of [[Generating primes#Prime sieves|prime number sieve]]s, it is one of the most efficient ways to find all of the smaller primes. It 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", [https://www.jstor.org/stable/1967477 ''Annals of Mathematics, Second Series'' '''10''':2 (1909), pp. 88–104].</ref> ==Overview== {{quote box|fontsize = 105%|''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'', 1984, p.&nbsp;170. {{isbn|3-540-11046-1}}.</ref>}} A [[prime number]] is a [[natural number]] that has exactly two distinct natural number [[divisor]]s: the number [[1]] and itself. To find all the prime numbers less than or equal to a given integer {{mvar|n}} by Eratosthenes' method: # Create a list of consecutive integers from 2 through {{mvar|n}}: {{math|(2, 3, 4, ..., ''n'')}}. # Initially, let {{mvar|p}} equal 2, the smallest prime number. # Enumerate the multiples of {{mvar|p}} by counting in increments of {{mvar|p}} from {{math|2''p''}} to {{mvar|n}}, and mark them in the list (these will be {{math|2''p'', 3''p'', 4''p'', ...}}; the {{mvar|p}} itself should not be marked). # Find the smallest number in the list greater than {{mvar|p}} that is not marked. If there was no such number, stop. Otherwise, let {{mvar|p}} now equal this new number (which is the next prime), and repeat from step 3. # When the algorithm terminates, the numbers remaining not marked in the list are all the primes below {{mvar|n}}. The main idea here is that every value given to {{mvar|p}} will be prime, because if it were composite it would be marked as a multiple of some other, smaller prime. Note that some of the numbers may be marked more than once (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 {{math|''p''<sup>2</sup>}}, as all the smaller multiples of {{mvar|p}} will have already been marked at that point. This means that the algorithm is allowed to terminate in step 4 when {{math|''p''<sup>2</sup>}} is greater than {{mvar|n}}.<ref name="horsley" /> <!-- This does not appear in the algorithm as described by Nicomachus who instead describes sieving by odds, starting from the odd itself instead of its square.<ref name="nicomachus1926" /> --> Another refinement is to initially list odd numbers only, {{math|(3, 5, ..., ''n'')}}, and count in increments of {{math|2''p''}} in step 3, thus marking only odd multiples of {{mvar|p}}. This actually appears in the original algorithm.<ref name="horsley" /><ref name="nicomachus1926" /> <!-- The table with divisors is probably not by Nicomachus. --> 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), and counting in the correspondingly adjusted increments so that only such multiples of {{mvar|p}} are generated that are coprime with those small primes, in the first place.<ref name="Runciman">{{Cite journal | doi = 10.1017/S0956796897002670| title = Functional Pearl: Lazy wheel sieves and spirals of primes| journal = Journal of Functional Programming| volume = 7| issue = 2| pages = 219–225| year = 1997| last1 = Runciman | first1 = Colin| s2cid = 2422563| url = http://eprints.whiterose.ac.uk/3784/1/runcimanc1.pdf}}</ref> ===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: &nbsp;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 The first number in the list is 2; cross out every 2nd number in the list after 2 by counting up from 2 in increments of 2 (these will be all the multiples of 2 in the list): &nbsp;2 3 {{gray|<s> 4 </s>}} 5 {{gray|<s> 6 </s>}} 7 {{gray|<s> 8 </s>}} 9 {{gray|<s>10</s>}} 11 {{gray|<s>12</s>}} 13 {{gray|<s>14</s>}} 15 {{gray|<s>16</s>}} 17 {{gray|<s>18</s>}} 19 {{gray|<s>20</s>}} 21 {{gray|<s>22</s>}} 23 {{gray|<s>24</s>}} 25 {{gray|<s>26</s>}} 27 {{gray|<s>28</s>}} 29 {{gray|<s>30</s>}} The next number in the list after 2 is 3; cross out every 3rd number in the list after 3 by counting up from 3 in increments of 3 (these will be all the multiples of 3 in the list): &nbsp;2 3 {{gray|<s> 4 </s>}} 5 {{gray|<s> 6 </s>}} 7 {{gray|<s> 8 </s>}}{{gray|<s> 9 </s>}}{{gray|<s>10</s>}} 11 {{gray|<s>12</s>}} 13 {{gray|<s>14 </s>}}{{gray|<s>15 </s>}}{{gray|<s>16</s>}} 17 {{gray|<s>18</s>}} 19 {{gray|<s>20 </s>}}{{gray|<s>21 </s>}}{{gray|<s>22</s>}} 23 {{gray|<s>24</s>}} 25 {{gray|<s>26 </s>}}{{gray|<s>27 </s>}}{{gray|<s>28</s>}} 29 {{gray|<s>30</s>}} The next number not yet crossed out in the list after 3 is 5; cross out every 5th number in the list after 5 by counting up from 5 in increments of 5 (i.e. all the multiples of 5): &nbsp;2 3 {{gray|<s> 4 </s>}} 5 {{gray|<s> 6 </s>}} 7 {{gray|<s> 8 </s>}}{{gray|<s> 9 </s>}}{{gray|<s>10</s>}} 11 {{gray|<s>12</s>}} 13 {{gray|<s>14 </s>}}{{gray|<s>15 </s>}}{{gray|<s>16</s>}} 17 {{gray|<s>18</s>}} 19 {{gray|<s>20 </s>}}{{gray|<s>21 </s>}}{{gray|<s>22</s>}} 23 {{gray|<s>24 </s>}}{{gray|<s>25 </s>}}{{gray|<s>26 </s>}}{{gray|<s>27 </s>}}{{gray|<s>28</s>}} 29 {{gray|<s>30</s>}} The 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 7, 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 not crossed out at this point in the list are all the prime numbers below 30: &nbsp;2 3 5 7 11 13 17 19 23 29 ==Algorithm and variants== ===Pseudocode=== The sieve of Eratosthenes can be expressed in [[pseudocode]], as follows:<ref name="sedgewick">{{cite book |last1=Sedgewick |first1=Robert |title=Algorithms in C++ |publisher=Addison-Wesley |year=1992 |isbn=978-0-201-51059-1 }}, p.&nbsp;16.</ref><ref name="intro">[http://research.cs.wisc.edu/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> '''algorithm''' Sieve of Eratosthenes '''is''' '''input''': an integer ''n'' > 1. '''output''': all prime numbers from 2 through ''n''. '''let''' ''A'' be an '''array of [[Boolean data type|Boolean]]''' values, indexed by '''integer'''s 2 to ''n'', initially all '''set''' to '''true'''. '''for''' ''i'' = 2, 3, 4, ..., not exceeding {{math|''{{sqrt|n}}''}} '''do''' '''if''' ''A''[''i''] '''is''' '''true''' '''for''' ''j'' = ''i''<sup>2</sup>, ''i''<sup>2</sup>+''i'', ''i''<sup>2</sup>+2''i'', ''i''<sup>2</sup>+3''i'', ..., not exceeding ''n'' '''do''' '''set''' ''A''[''j''] := '''false''' '''return''' all ''i'' such that ''A''[''i''] '''is''' '''true'''. This algorithm produces all primes not greater than {{mvar|n}}. It includes a common optimization, which is to start enumerating the multiples of each prime {{mvar|i}} from {{math|''i''<sup>2</sup>}}. The [[time complexity]] of this algorithm is {{math|''O''(''n'' log log ''n'')}},{{r|intro}} provided the array update is an {{math|''O''(1)}} operation, as is usually the case. ===Segmented sieve=== As Sorenson notes, the problem with the sieve of Eratosthenes is not the number of operations it performs but rather its memory requirements.{{r|intro}} For large {{mvar|n}}, the range of primes may not fit in memory; worse, even for moderate {{mvar|n}}, its [[CPU cache|cache]] use is highly suboptimal. The algorithm walks through the entire array {{mvar|A}}, exhibiting almost no [[locality of reference]]. A solution to these problems is offered by ''segmented'' sieves, 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> These have been known since the 1970s, and work as follows:{{r|intro}}<ref>{{Cite journal | last1 = Bays | first1 = Carter | last2 = Hudson | first2 = Richard H. | year = 1977 | title = The segmented sieve of Eratosthenes and primes in arithmetic progressions to 10<sup>12</sup> | journal = BIT | volume = 17 | issue = 2 | pages = 121–127 | doi = 10.1007/BF01932283 | s2cid = 122592488 }}</ref> # Divide the range 2 through {{mvar|n}} into segments of some size {{math|Δ ≤ {{sqrt|''n''}}}}. # Find the primes in the first (i.e. the lowest) segment, using the regular sieve. # For each of the following segments, in increasing order, with {{mvar|m}} being the segment's topmost value, find the primes in it as follows: ## Set up a Boolean array of size {{math|Δ}}. ## Mark as non-prime the positions in the array corresponding to the multiples of each prime {{math|''p'' ≤ {{sqrt|''m''}}}} found so far, by enumerating its multiples in steps of {{math|''p''}} starting from the lowest multiple of {{math|''p''}} between {{math|{{mvar|m}} - Δ}} and {{mvar|m}}. ## The remaining non-marked positions in the array correspond to the primes in the segment. It isn't necessary to mark any multiples of ''these'' primes, because all of these primes are larger than {{math|{{sqrt|''m''}}}}, as for {{math|''k'' ≥ 1}}, one has <math>(k\Delta + 1)^2 > (k+1)\Delta</math>. If {{math|Δ}} is chosen to be {{math|{{sqrt|''n''}}}}, the space complexity of the algorithm is {{math|''O''({{sqrt|''n''}})}}, while the time complexity is the same as that of the regular sieve.{{r|intro}} For ranges with upper limit {{math|''n''}} so large that the sieving primes below {{math|{{sqrt|''n''}}}} as required by the page segmented sieve of Eratosthenes cannot fit in memory, a slower but much more space-efficient sieve like the [[sieve 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> ===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 {{mvar|p}} are generated directly by counting up from the square of the prime in increments of {{mvar|p}} (or {{math|2''p''}} for odd primes). The generation must be initiated only when the prime's square is reached, to avoid adverse effects on efficiency. It can be expressed symbolically under the [[Dataflow programming|dataflow]] paradigm as ''primes'' = [''2'', ''3'', ...] \ [[''p''², ''p''²+''p'', ...] for ''p'' in ''primes''], using [[list comprehension]] notation with <code>\</code> denoting [[Complement (set theory)#Relative complement|set subtraction]] of [[arithmetic progressions]] of numbers. Primes can also be produced by iteratively sieving out the composites through [[Trial division|divisibility testing]] by sequential primes, one prime at a time. It is not the sieve of Eratosthenes but is often confused with it, even though the sieve of Eratosthenes directly generates the composites instead of testing for them. Trial division has worse theoretical [[Analysis of algorithms|complexity]] than that of the sieve of Eratosthenes in generating ranges of primes.<ref name="ONeill"/> When testing each prime, the ''optimal'' trial division algorithm uses all prime numbers not exceeding its square root, whereas the sieve of Eratosthenes produces each composite from its prime factors only, and gets the primes "for free", between the composites. The widely known 1975 [[functional programming|functional]] sieve 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. (<syntaxhighlight lang="haskell" inline>primes = sieve [2..]; sieve (p:nos) = p:sieve (remove (multsof p) nos); remove m = filter (not . m); multsof p n = rem n p==0</syntaxhighlight>). But see also [http://dl.acm.org/citation.cfm?id=811543&dl=ACM&coll=DL&CFID=663592028&CFTOKEN=36641676 Peter Henderson, Morris, James Jr., A Lazy Evaluator, 1976], where we [http://www.seas.gwu.edu/~rhyspj/cs3221/lab8/henderson.pdf find the following], attributed to P. Quarendon: <syntaxhighlight lang="python" inline>primeswrt[x;l] = if car[l] mod x=0 then primeswrt[x;cdr[l]] else cons[car[l];primeswrt[x;cdr[l]]] ; primes[l] = cons[car[l];primes[primeswrt[car[l];cdr[l]]]] ; primes[integers[2]]</syntaxhighlight>; the priority is unclear.</ref> is often presented as an example of the sieve of Eratosthenes<ref name="Runciman"/> but is actually a sub-optimal trial division sieve.<ref name="ONeill"/> ==Algorithmic complexity== The sieve of Eratosthenes is a popular way to benchmark computer performance.<ref name="peng1985fall">{{cite news | url=https://archive.org/stream/byte-magazine-1985-11/1985_11_BYTE_10-11_Inside_the_IBM_PCs#page/n245/mode/2up | title=One Million Primes Through the Sieve | work=BYTE | date=Fall 1985 | access-date=19 March 2016 | author=Peng, T. A. | pages=243–244}}</ref> The [[time complexity]] of calculating all primes below {{mvar|n}} in the [[random access machine]] model is {{math|''O''(''n'' log log ''n'')}} operations, a direct consequence of the fact that the [[prime harmonic series]] asymptotically approaches {{math|log log ''n''}}. It has an exponential time complexity with regard to input size, though, which makes it a [[Pseudo-polynomial time|pseudo-polynomial]] algorithm. The basic algorithm requires {{math|''O''(''n'')}} of memory. The [[bit complexity]] of the algorithm is {{math|''O''<big>(</big>''n'' (log ''n'') (log log ''n'')<big>)</big>}} bit operations with a memory requirement of {{math|''O''(''n'')}}.<ref>Pritchard, Paul, "Linear prime-number sieves: a family tree," ''Sci. Comput. Programming'' '''9''':1 (1987), pp. 17–35.</ref> The normally implemented page segmented version has the same operational complexity of {{math|''O''(''n'' log log ''n'')}} as the non-segmented version but reduces the space requirements to the very minimal size of the segment page plus the memory required to store the base primes less than the square root of the range used to cull composites from successive page segments of size {{math|''O''<big><big>(</big></big>{{sfrac|{{sqrt|''n''}}|log ''n''}}<big><big>)</big></big>}}. A special (rarely, if ever, implemented) segmented version of the sieve of Eratosthenes, with basic optimizations, uses {{math|''O''(''n'')}} operations and {{math|''O''<big><big>(</big></big>{{sqrt|''n''}}{{sfrac|log log ''n''|log ''n''}}<big><big>)</big></big>}} bits of memory.<ref name="Pritchard1">Paul Pritchard, "A sublinear additive sieve for finding prime numbers", ''Communications of the ACM'' 24 (1981), 18–23. {{MR|600730}}</ref><ref name="Pritchard2">Paul Pritchard, Explaining the wheel sieve, Acta Informatica 17 (1982), 477–485. {{MR|685983}}</ref><ref name="Pritchard3">Paul Pritchard, "Fast compact prime number sieves" (among others), ''Journal of Algorithms'' 4 (1983), 332–344. {{MR|729229}}</ref> Using [[big O notation]] ignores constant factors and offsets that may be very significant for practical ranges: The sieve of Eratosthenes variation known as the Pritchard wheel sieve<ref name="Pritchard1" /><ref name="Pritchard2" /><ref name="Pritchard3" /> has an {{math|''O''(''n'')}} performance, but its basic implementation requires either a "one large array" algorithm which limits its usable range to the amount of available memory else it needs to be page segmented to reduce memory use. When implemented with page segmentation in order to save memory, the basic algorithm still requires about {{math|''O''<big><big>(</big></big>{{sfrac|''n''|log ''n''}}<big><big>)</big></big>}} bits of memory (much more than the requirement of the basic page segmented sieve of Eratosthenes using {{math|''O''<big><big>(</big></big>{{sfrac|{{sqrt|''n''}}|log ''n''}}<big><big>)</big></big>}} bits of memory). Pritchard's work reduced the memory requirement at the cost of a large constant factor. Although the resulting wheel sieve has {{math|''O''(''n'')}} performance and an acceptable memory requirement, it is not faster than a reasonably Wheel Factorized basic sieve of Eratosthenes for practical sieving ranges. ==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" /> The same sieve was rediscovered and observed to take [[linear time]] by {{harvtxt|Gries|Misra|1978}}.<ref>{{citation | last1 = Gries | first1 = David | author1-link = David Gries | last2 = Misra | first2 = Jayadev | date = December 1978 | doi = 10.1145/359657.359660 | issue = 12 | journal = [[Communications of the ACM]] | pages = 999–1003 | title = A linear sieve algorithm for finding prime numbers | volume = 21| hdl = 1813/6407 | s2cid = 11990373 | url = https://ecommons.cornell.edu/bitstream/1813/6407/1/77-313.pdf | hdl-access = free }}.</ref> It, too, starts with a [[list (computing)|list]] of numbers from 2 to {{mvar|n}} in order. On each step the first element is identified as the next prime, is multiplied with each element of the list (thus starting with itself), and the results 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: <div style="font-size:85%;"> <!-- these &nbsp;s are put here hoping to prevent bots messing it up --> &nbsp;[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 ... &nbsp;[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 ... &nbsp;[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 ... &nbsp;[5] (11) 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 ... &nbsp;[...] </div> Here the example is shown starting from odds, after the first step of the algorithm. Thus, on the {{mvar|k}}th step all the remaining multiples of the {{mvar|k}}th prime are removed from the list, which will thereafter contain only numbers coprime with the first {{mvar|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 {{nowrap|1=3 × 3 = 9}}, {{nowrap|1=3 × 5 = 15}}, {{nowrap|1=3 × 7 = 21}}, {{nowrap|1=3 × '''''9''''' = 27}}, ..., {{nowrap|1=3 × '''''15''''' = 45}}, ..., so care must be taken dealing with this.<ref name="intro" /> ==See also== * [[Sieve of Pritchard]] * [[Sieve of Atkin]] * [[Sieve of Sundaram]] * [[Sieve theory]] ==References== {{Reflist|2}} ==External links== * [http://primesieve.org/ primesieve – Very fast highly optimized C/C++ segmented Sieve of Eratosthenes] * [https://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]]. * [https://wiki.haskell.org/Prime_numbers#Sieve_of_Eratosthenes 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] * [https://sites.google.com/site/bbuhrow/home/cuda-sieve-of-eratosthenes Fast optimized highly parallel CUDA segmented Sieve of Eratosthenes in C] * [http://wiki.c2.com/?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]]'
New page wikitext, after the edit (new_wikitext)
'{{short description|Ancient algorithm for generating prime numbers}} {{For|the sculpture|The Sieve of Eratosthenes (sculpture)}} [[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).]] In [[mathematics]], the '''quandale method''' is an 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 first prime number, 2. The multiples of a given prime are generated as a sequence of numbers starting from that prime, with [[arithmetic progression|constant difference between them]] that is equal to that prime.<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," [https://www.jstor.org/stable/106053 ''Philosophical Transactions'' (1683–1775), Vol. 62. (1772), pp. 327–347].</ref> 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" /> Once all the multiples of each discovered prime have been marked as composites, the remaining unmarked numbers are primes. The earliest known reference to the sieve ({{lang-grc|κόσκινον Ἐρατοσθένο sussy amongus , ''kóskinon Eratosthénous'') is in [[Nicomachus|Nicomachus of Gerasa]]'s ''[[Introduction to Arithmetic]]'',<ref name=nicomachus>{{citation|editor-first=Richard|editor-last=Hoche|editor-link=Richard Hoche|title=Nicomachi Geraseni Pythagorei Introductionis arithmeticae libri II, chapter XIII, 3|year=1866|location= Leipzig|publisher= B.G. Teubner|page=30|url=https://archive.org/stream/nicomachigerasen00nicouoft#page/30/mode/2up}}</ref> an early 2nd cent. CE book which attributes it to [[Eratosthenes|Eratosthenes of Cyrene]], a 3rd cent. BCE [[Greek mathematics|Greek mathematician]], though describing the sieving by odd numbers instead of by primes.<ref name=nicomachus1926>{{citation|author=Nicomachus of Gerasa|title=Introduction to Arithmetic; translated into English by Martin Luther D'Ooge ; with studies in Greek arithmetic by Frank Egleston Robbins and Louis Charles Karpinski, chapter XIII, 3|year=1926|location=New York|publisher=The Macmillan Company|page=204}}</ref> One of a number of [[Generating primes#Prime sieves|prime number sieve]]s, it is one of the most efficient ways to find all of the smaller primes. It 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", [https://www.jstor.org/stable/1967477 ''Annals of Mathematics, Second Series'' '''10''':2 (1909), pp. 88–104].</ref> ==Overview== {{quote box|fontsize = 105%|''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'', 1984, p.&nbsp;170. {{isbn|3-540-11046-1}}.</ref>}} A [[prime number]] is a [[natural number]] that has exactly two distinct natural number [[divisor]]s: the number [[1]] and itself. To find all the prime numbers less than or equal to a given integer {{mvar|n}} by Eratosthenes' method: # Create a list of consecutive integers from 2 through {{mvar|n}}: {{math|(2, 3, 4, ..., ''n'')}}. # Initially, let {{mvar|p}} equal 2, the smallest prime number. # Enumerate the multiples of {{mvar|p}} by counting in increments of {{mvar|p}} from {{math|2''p''}} to {{mvar|n}}, and mark them in the list (these will be {{math|2''p'', 3''p'', 4''p'', ...}}; the {{mvar|p}} itself should not be marked). # Find the smallest number in the list greater than {{mvar|p}} that is not marked. If there was no such number, stop. Otherwise, let {{mvar|p}} now equal this new number (which is the next prime), and repeat from step 3. # When the algorithm terminates, the numbers remaining not marked in the list are all the primes below {{mvar|n}}. The main idea here is that every value given to {{mvar|p}} will be prime, because if it were composite it would be marked as a multiple of some other, smaller prime. Note that some of the numbers may be marked more than once (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 {{math|''p''<sup>2</sup>}}, as all the smaller multiples of {{mvar|p}} will have already been marked at that point. This means that the algorithm is allowed to terminate in step 4 when {{math|''p''<sup>2</sup>}} is greater than {{mvar|n}}.<ref name="horsley" /> <!-- This does not appear in the algorithm as described by Nicomachus who instead describes sieving by odds, starting from the odd itself instead of its square.<ref name="nicomachus1926" /> --> Another refinement is to initially list odd numbers only, {{math|(3, 5, ..., ''n'')}}, and count in increments of {{math|2''p''}} in step 3, thus marking only odd multiples of {{mvar|p}}. This actually appears in the original algorithm.<ref name="horsley" /><ref name="nicomachus1926" /> <!-- The table with divisors is probably not by Nicomachus. --> 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), and counting in the correspondingly adjusted increments so that only such multiples of {{mvar|p}} are generated that are coprime with those small primes, in the first place.<ref name="Runciman">{{Cite journal | doi = 10.1017/S0956796897002670| title = Functional Pearl: Lazy wheel sieves and spirals of primes| journal = Journal of Functional Programming| volume = 7| issue = 2| pages = 219–225| year = 1997| last1 = Runciman | first1 = Colin| s2cid = 2422563| url = http://eprints.whiterose.ac.uk/3784/1/runcimanc1.pdf}}</ref> ===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: &nbsp;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 The first number in the list is 2; cross out every 2nd number in the list after 2 by counting up from 2 in increments of 2 (these will be all the multiples of 2 in the list): &nbsp;2 3 {{gray|<s> 4 </s>}} 5 {{gray|<s> 6 </s>}} 7 {{gray|<s> 8 </s>}} 9 {{gray|<s>10</s>}} 11 {{gray|<s>12</s>}} 13 {{gray|<s>14</s>}} 15 {{gray|<s>16</s>}} 17 {{gray|<s>18</s>}} 19 {{gray|<s>20</s>}} 21 {{gray|<s>22</s>}} 23 {{gray|<s>24</s>}} 25 {{gray|<s>26</s>}} 27 {{gray|<s>28</s>}} 29 {{gray|<s>30</s>}} The next number in the list after 2 is 3; cross out every 3rd number in the list after 3 by counting up from 3 in increments of 3 (these will be all the multiples of 3 in the list): &nbsp;2 3 {{gray|<s> 4 </s>}} 5 {{gray|<s> 6 </s>}} 7 {{gray|<s> 8 </s>}}{{gray|<s> 9 </s>}}{{gray|<s>10</s>}} 11 {{gray|<s>12</s>}} 13 {{gray|<s>14 </s>}}{{gray|<s>15 </s>}}{{gray|<s>16</s>}} 17 {{gray|<s>18</s>}} 19 {{gray|<s>20 </s>}}{{gray|<s>21 </s>}}{{gray|<s>22</s>}} 23 {{gray|<s>24</s>}} 25 {{gray|<s>26 </s>}}{{gray|<s>27 </s>}}{{gray|<s>28</s>}} 29 {{gray|<s>30</s>}} The next number not yet crossed out in the list after 3 is 5; cross out every 5th number in the list after 5 by counting up from 5 in increments of 5 (i.e. all the multiples of 5): &nbsp;2 3 {{gray|<s> 4 </s>}} 5 {{gray|<s> 6 </s>}} 7 {{gray|<s> 8 </s>}}{{gray|<s> 9 </s>}}{{gray|<s>10</s>}} 11 {{gray|<s>12</s>}} 13 {{gray|<s>14 </s>}}{{gray|<s>15 </s>}}{{gray|<s>16</s>}} 17 {{gray|<s>18</s>}} 19 {{gray|<s>20 </s>}}{{gray|<s>21 </s>}}{{gray|<s>22</s>}} 23 {{gray|<s>24 </s>}}{{gray|<s>25 </s>}}{{gray|<s>26 </s>}}{{gray|<s>27 </s>}}{{gray|<s>28</s>}} 29 {{gray|<s>30</s>}} The 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 7, 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 not crossed out at this point in the list are all the prime numbers below 30: &nbsp;2 3 5 7 11 13 17 19 23 29 ==Algorithm and variants== ===Pseudocode=== The sieve of Eratosthenes can be expressed in [[pseudocode]], as follows:<ref name="sedgewick">{{cite book |last1=Sedgewick |first1=Robert |title=Algorithms in C++ |publisher=Addison-Wesley |year=1992 |isbn=978-0-201-51059-1 }}, p.&nbsp;16.</ref><ref name="intro">[http://research.cs.wisc.edu/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> '''algorithm''' Sieve of Eratosthenes '''is''' '''input''': an integer ''n'' > 1. '''output''': all prime numbers from 2 through ''n''. '''let''' ''A'' be an '''array of [[Boolean data type|Boolean]]''' values, indexed by '''integer'''s 2 to ''n'', initially all '''set''' to '''true'''. '''for''' ''i'' = 2, 3, 4, ..., not exceeding {{math|''{{sqrt|n}}''}} '''do''' '''if''' ''A''[''i''] '''is''' '''true''' '''for''' ''j'' = ''i''<sup>2</sup>, ''i''<sup>2</sup>+''i'', ''i''<sup>2</sup>+2''i'', ''i''<sup>2</sup>+3''i'', ..., not exceeding ''n'' '''do''' '''set''' ''A''[''j''] := '''false''' '''return''' all ''i'' such that ''A''[''i''] '''is''' '''true'''. This algorithm produces all primes not greater than {{mvar|n}}. It includes a common optimization, which is to start enumerating the multiples of each prime {{mvar|i}} from {{math|''i''<sup>2</sup>}}. The [[time complexity]] of this algorithm is {{math|''O''(''n'' log log ''n'')}},{{r|intro}} provided the array update is an {{math|''O''(1)}} operation, as is usually the case. ===Segmented sieve=== As Sorenson notes, the problem with the sieve of Eratosthenes is not the number of operations it performs but rather its memory requirements.{{r|intro}} For large {{mvar|n}}, the range of primes may not fit in memory; worse, even for moderate {{mvar|n}}, its [[CPU cache|cache]] use is highly suboptimal. The algorithm walks through the entire array {{mvar|A}}, exhibiting almost no [[locality of reference]]. A solution to these problems is offered by ''segmented'' sieves, 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> These have been known since the 1970s, and work as follows:{{r|intro}}<ref>{{Cite journal | last1 = Bays | first1 = Carter | last2 = Hudson | first2 = Richard H. | year = 1977 | title = The segmented sieve of Eratosthenes and primes in arithmetic progressions to 10<sup>12</sup> | journal = BIT | volume = 17 | issue = 2 | pages = 121–127 | doi = 10.1007/BF01932283 | s2cid = 122592488 }}</ref> # Divide the range 2 through {{mvar|n}} into segments of some size {{math|Δ ≤ {{sqrt|''n''}}}}. # Find the primes in the first (i.e. the lowest) segment, using the regular sieve. # For each of the following segments, in increasing order, with {{mvar|m}} being the segment's topmost value, find the primes in it as follows: ## Set up a Boolean array of size {{math|Δ}}. ## Mark as non-prime the positions in the array corresponding to the multiples of each prime {{math|''p'' ≤ {{sqrt|''m''}}}} found so far, by enumerating its multiples in steps of {{math|''p''}} starting from the lowest multiple of {{math|''p''}} between {{math|{{mvar|m}} - Δ}} and {{mvar|m}}. ## The remaining non-marked positions in the array correspond to the primes in the segment. It isn't necessary to mark any multiples of ''these'' primes, because all of these primes are larger than {{math|{{sqrt|''m''}}}}, as for {{math|''k'' ≥ 1}}, one has <math>(k\Delta + 1)^2 > (k+1)\Delta</math>. If {{math|Δ}} is chosen to be {{math|{{sqrt|''n''}}}}, the space complexity of the algorithm is {{math|''O''({{sqrt|''n''}})}}, while the time complexity is the same as that of the regular sieve.{{r|intro}} For ranges with upper limit {{math|''n''}} so large that the sieving primes below {{math|{{sqrt|''n''}}}} as required by the page segmented sieve of Eratosthenes cannot fit in memory, a slower but much more space-efficient sieve like the [[sieve 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> ===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 {{mvar|p}} are generated directly by counting up from the square of the prime in increments of {{mvar|p}} (or {{math|2''p''}} for odd primes). The generation must be initiated only when the prime's square is reached, to avoid adverse effects on efficiency. It can be expressed symbolically under the [[Dataflow programming|dataflow]] paradigm as ''primes'' = [''2'', ''3'', ...] \ [[''p''², ''p''²+''p'', ...] for ''p'' in ''primes''], using [[list comprehension]] notation with <code>\</code> denoting [[Complement (set theory)#Relative complement|set subtraction]] of [[arithmetic progressions]] of numbers. Primes can also be produced by iteratively sieving out the composites through [[Trial division|divisibility testing]] by sequential primes, one prime at a time. It is not the sieve of Eratosthenes but is often confused with it, even though the sieve of Eratosthenes directly generates the composites instead of testing for them. Trial division has worse theoretical [[Analysis of algorithms|complexity]] than that of the sieve of Eratosthenes in generating ranges of primes.<ref name="ONeill"/> When testing each prime, the ''optimal'' trial division algorithm uses all prime numbers not exceeding its square root, whereas the sieve of Eratosthenes produces each composite from its prime factors only, and gets the primes "for free", between the composites. The widely known 1975 [[functional programming|functional]] sieve 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. (<syntaxhighlight lang="haskell" inline>primes = sieve [2..]; sieve (p:nos) = p:sieve (remove (multsof p) nos); remove m = filter (not . m); multsof p n = rem n p==0</syntaxhighlight>). But see also [http://dl.acm.org/citation.cfm?id=811543&dl=ACM&coll=DL&CFID=663592028&CFTOKEN=36641676 Peter Henderson, Morris, James Jr., A Lazy Evaluator, 1976], where we [http://www.seas.gwu.edu/~rhyspj/cs3221/lab8/henderson.pdf find the following], attributed to P. Quarendon: <syntaxhighlight lang="python" inline>primeswrt[x;l] = if car[l] mod x=0 then primeswrt[x;cdr[l]] else cons[car[l];primeswrt[x;cdr[l]]] ; primes[l] = cons[car[l];primes[primeswrt[car[l];cdr[l]]]] ; primes[integers[2]]</syntaxhighlight>; the priority is unclear.</ref> is often presented as an example of the sieve of Eratosthenes<ref name="Runciman"/> but is actually a sub-optimal trial division sieve.<ref name="ONeill"/> ==Algorithmic complexity== The sieve of Eratosthenes is a popular way to benchmark computer performance.<ref name="peng1985fall">{{cite news | url=https://archive.org/stream/byte-magazine-1985-11/1985_11_BYTE_10-11_Inside_the_IBM_PCs#page/n245/mode/2up | title=One Million Primes Through the Sieve | work=BYTE | date=Fall 1985 | access-date=19 March 2016 | author=Peng, T. A. | pages=243–244}}</ref> The [[time complexity]] of calculating all primes below {{mvar|n}} in the [[random access machine]] model is {{math|''O''(''n'' log log ''n'')}} operations, a direct consequence of the fact that the [[prime harmonic series]] asymptotically approaches {{math|log log ''n''}}. It has an exponential time complexity with regard to input size, though, which makes it a [[Pseudo-polynomial time|pseudo-polynomial]] algorithm. The basic algorithm requires {{math|''O''(''n'')}} of memory. The [[bit complexity]] of the algorithm is {{math|''O''<big>(</big>''n'' (log ''n'') (log log ''n'')<big>)</big>}} bit operations with a memory requirement of {{math|''O''(''n'')}}.<ref>Pritchard, Paul, "Linear prime-number sieves: a family tree," ''Sci. Comput. Programming'' '''9''':1 (1987), pp. 17–35.</ref> The normally implemented page segmented version has the same operational complexity of {{math|''O''(''n'' log log ''n'')}} as the non-segmented version but reduces the space requirements to the very minimal size of the segment page plus the memory required to store the base primes less than the square root of the range used to cull composites from successive page segments of size {{math|''O''<big><big>(</big></big>{{sfrac|{{sqrt|''n''}}|log ''n''}}<big><big>)</big></big>}}. A special (rarely, if ever, implemented) segmented version of the sieve of Eratosthenes, with basic optimizations, uses {{math|''O''(''n'')}} operations and {{math|''O''<big><big>(</big></big>{{sqrt|''n''}}{{sfrac|log log ''n''|log ''n''}}<big><big>)</big></big>}} bits of memory.<ref name="Pritchard1">Paul Pritchard, "A sublinear additive sieve for finding prime numbers", ''Communications of the ACM'' 24 (1981), 18–23. {{MR|600730}}</ref><ref name="Pritchard2">Paul Pritchard, Explaining the wheel sieve, Acta Informatica 17 (1982), 477–485. {{MR|685983}}</ref><ref name="Pritchard3">Paul Pritchard, "Fast compact prime number sieves" (among others), ''Journal of Algorithms'' 4 (1983), 332–344. {{MR|729229}}</ref> Using [[big O notation]] ignores constant factors and offsets that may be very significant for practical ranges: The sieve of Eratosthenes variation known as the Pritchard wheel sieve<ref name="Pritchard1" /><ref name="Pritchard2" /><ref name="Pritchard3" /> has an {{math|''O''(''n'')}} performance, but its basic implementation requires either a "one large array" algorithm which limits its usable range to the amount of available memory else it needs to be page segmented to reduce memory use. When implemented with page segmentation in order to save memory, the basic algorithm still requires about {{math|''O''<big><big>(</big></big>{{sfrac|''n''|log ''n''}}<big><big>)</big></big>}} bits of memory (much more than the requirement of the basic page segmented sieve of Eratosthenes using {{math|''O''<big><big>(</big></big>{{sfrac|{{sqrt|''n''}}|log ''n''}}<big><big>)</big></big>}} bits of memory). Pritchard's work reduced the memory requirement at the cost of a large constant factor. Although the resulting wheel sieve has {{math|''O''(''n'')}} performance and an acceptable memory requirement, it is not faster than a reasonably Wheel Factorized basic sieve of Eratosthenes for practical sieving ranges. ==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" /> The same sieve was rediscovered and observed to take [[linear time]] by {{harvtxt|Gries|Misra|1978}}.<ref>{{citation | last1 = Gries | first1 = David | author1-link = David Gries | last2 = Misra | first2 = Jayadev | date = December 1978 | doi = 10.1145/359657.359660 | issue = 12 | journal = [[Communications of the ACM]] | pages = 999–1003 | title = A linear sieve algorithm for finding prime numbers | volume = 21| hdl = 1813/6407 | s2cid = 11990373 | url = https://ecommons.cornell.edu/bitstream/1813/6407/1/77-313.pdf | hdl-access = free }}.</ref> It, too, starts with a [[list (computing)|list]] of numbers from 2 to {{mvar|n}} in order. On each step the first element is identified as the next prime, is multiplied with each element of the list (thus starting with itself), and the results 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: <div style="font-size:85%;"> <!-- these &nbsp;s are put here hoping to prevent bots messing it up --> &nbsp;[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 ... &nbsp;[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 ... &nbsp;[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 ... &nbsp;[5] (11) 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 ... &nbsp;[...] </div> Here the example is shown starting from odds, after the first step of the algorithm. Thus, on the {{mvar|k}}th step all the remaining multiples of the {{mvar|k}}th prime are removed from the list, which will thereafter contain only numbers coprime with the first {{mvar|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 {{nowrap|1=3 × 3 = 9}}, {{nowrap|1=3 × 5 = 15}}, {{nowrap|1=3 × 7 = 21}}, {{nowrap|1=3 × '''''9''''' = 27}}, ..., {{nowrap|1=3 × '''''15''''' = 45}}, ..., so care must be taken dealing with this.<ref name="intro" /> ==See also== * [[Sieve of Pritchard]] * [[Sieve of Atkin]] * [[Sieve of Sundaram]] * [[Sieve theory]] ==References== {{Reflist|2}} ==External links== * [http://primesieve.org/ primesieve – Very fast highly optimized C/C++ segmented Sieve of Eratosthenes] * [https://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]]. * [https://wiki.haskell.org/Prime_numbers#Sieve_of_Eratosthenes 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] * [https://sites.google.com/site/bbuhrow/home/cuda-sieve-of-eratosthenes Fast optimized highly parallel CUDA segmented Sieve of Eratosthenes in C] * [http://wiki.c2.com/?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]]'
Unified diff of changes made by edit (edit_diff)
'@@ -7,5 +7,7 @@ It does so by iteratively marking as [[composite number|composite]] (i.e., not prime) the multiples of each prime, starting with the first prime number, 2. The multiples of a given prime are generated as a sequence of numbers starting from that prime, with [[arithmetic progression|constant difference between them]] that is equal to that prime.<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," [https://www.jstor.org/stable/106053 ''Philosophical Transactions'' (1683–1775), Vol. 62. (1772), pp. 327–347].</ref> 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" /> Once all the multiples of each discovered prime have been marked as composites, the remaining unmarked numbers are primes. -The earliest known reference to the sieve ({{lang-grc|κόσκινον Ἐρατοσθένους aa willie is the throut goat}}, ''kóskinon Eratosthénous'') is in [[Nicomachus|Nicomachus of Gerasa]]'s ''[[Introduction to Arithmetic]]'',<ref name=nicomachus>{{citation|editor-first=Richard|editor-last=Hoche|editor-link=Richard Hoche|title=Nicomachi Geraseni Pythagorei Introductionis arithmeticae libri II, chapter XIII, 3|year=1866|location= Leipzig|publisher= B.G. Teubner|page=30|url=https://archive.org/stream/nicomachigerasen00nicouoft#page/30/mode/2up}}</ref> an early 2nd cent. CE book which attributes it to [[Eratosthenes|Eratosthenes of Cyrene]], a 3rd cent. BCE [[Greek mathematics|Greek mathematician]], though describing the sieving by odd numbers instead of by primes.<ref name=nicomachus1926>{{citation|author=Nicomachus of Gerasa|title=Introduction to Arithmetic; translated into English by Martin Luther D'Ooge ; with studies in Greek arithmetic by Frank Egleston Robbins and Louis Charles Karpinski, chapter XIII, 3|year=1926|location=New York|publisher=The Macmillan Company|page=204}}</ref> +The earliest known reference to the sieve ({{lang-grc|κόσκινον Ἐρατοσθένο sussy amongus + +, ''kóskinon Eratosthénous'') is in [[Nicomachus|Nicomachus of Gerasa]]'s ''[[Introduction to Arithmetic]]'',<ref name=nicomachus>{{citation|editor-first=Richard|editor-last=Hoche|editor-link=Richard Hoche|title=Nicomachi Geraseni Pythagorei Introductionis arithmeticae libri II, chapter XIII, 3|year=1866|location= Leipzig|publisher= B.G. Teubner|page=30|url=https://archive.org/stream/nicomachigerasen00nicouoft#page/30/mode/2up}}</ref> an early 2nd cent. CE book which attributes it to [[Eratosthenes|Eratosthenes of Cyrene]], a 3rd cent. BCE [[Greek mathematics|Greek mathematician]], though describing the sieving by odd numbers instead of by primes.<ref name=nicomachus1926>{{citation|author=Nicomachus of Gerasa|title=Introduction to Arithmetic; translated into English by Martin Luther D'Ooge ; with studies in Greek arithmetic by Frank Egleston Robbins and Louis Charles Karpinski, chapter XIII, 3|year=1926|location=New York|publisher=The Macmillan Company|page=204}}</ref> One of a number of [[Generating primes#Prime sieves|prime number sieve]]s, it is one of the most efficient ways to find all of the smaller primes. It 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", [https://www.jstor.org/stable/1967477 ''Annals of Mathematics, Second Series'' '''10''':2 (1909), pp. 88–104].</ref> '
New page size (new_size)
24738
Old page size (old_size)
24757
Size change in edit (edit_delta)
-19
Lines added in edit (added_lines)
[ 0 => 'The earliest known reference to the sieve ({{lang-grc|κόσκινον Ἐρατοσθένο sussy amongus', 1 => '', 2 => ', ''kóskinon Eratosthénous'') is in [[Nicomachus|Nicomachus of Gerasa]]'s ''[[Introduction to Arithmetic]]'',<ref name=nicomachus>{{citation|editor-first=Richard|editor-last=Hoche|editor-link=Richard Hoche|title=Nicomachi Geraseni Pythagorei Introductionis arithmeticae libri II, chapter XIII, 3|year=1866|location= Leipzig|publisher= B.G. Teubner|page=30|url=https://archive.org/stream/nicomachigerasen00nicouoft#page/30/mode/2up}}</ref> an early 2nd cent. CE book which attributes it to [[Eratosthenes|Eratosthenes of Cyrene]], a 3rd cent. BCE [[Greek mathematics|Greek mathematician]], though describing the sieving by odd numbers instead of by primes.<ref name=nicomachus1926>{{citation|author=Nicomachus of Gerasa|title=Introduction to Arithmetic; translated into English by Martin Luther D'Ooge ; with studies in Greek arithmetic by Frank Egleston Robbins and Louis Charles Karpinski, chapter XIII, 3|year=1926|location=New York|publisher=The Macmillan Company|page=204}}</ref>' ]
Lines removed in edit (removed_lines)
[ 0 => 'The earliest known reference to the sieve ({{lang-grc|κόσκινον Ἐρατοσθένους aa willie is the throut goat}}, ''kóskinon Eratosthénous'') is in [[Nicomachus|Nicomachus of Gerasa]]'s ''[[Introduction to Arithmetic]]'',<ref name=nicomachus>{{citation|editor-first=Richard|editor-last=Hoche|editor-link=Richard Hoche|title=Nicomachi Geraseni Pythagorei Introductionis arithmeticae libri II, chapter XIII, 3|year=1866|location= Leipzig|publisher= B.G. Teubner|page=30|url=https://archive.org/stream/nicomachigerasen00nicouoft#page/30/mode/2up}}</ref> an early 2nd cent. CE book which attributes it to [[Eratosthenes|Eratosthenes of Cyrene]], a 3rd cent. BCE [[Greek mathematics|Greek mathematician]], though describing the sieving by odd numbers instead of by primes.<ref name=nicomachus1926>{{citation|author=Nicomachus of Gerasa|title=Introduction to Arithmetic; translated into English by Martin Luther D'Ooge ; with studies in Greek arithmetic by Frank Egleston Robbins and Louis Charles Karpinski, chapter XIII, 3|year=1926|location=New York|publisher=The Macmillan Company|page=204}}</ref>' ]
Whether or not the change was made through a Tor exit node (tor_exit_node)
false
Unix timestamp of change (timestamp)
'1681226395'