Lably
Skip to content

Sieve of Eratosthenes

Ancient algorithm for generating prime numbers

  • 13 min read
  • 4 chapters

Related on Lably

Through Mathematics, Algorithm and Prime number

It does so by iteratively marking as 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 constant difference between them that is equal to that prime. This is the sieve's key distinction from using trial division to sequentially test each candidate number for divisibility by each prime. 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 (Ancient Greek: κόσκινον Ἐρατοσθένους, kóskinon Eratosthénous) is in Nicomachus of Gerasa's Introduction to Arithmetic, an early 2nd-century CE book which attributes it to Eratosthenes of Cyrene, a 3rd-century BCE Greek mathematician, though describing the sieving by odd numbers instead of by primes.

One of several prime number sieves, it is one of the most efficient ways to find all of the smaller primes. It may be used to find primes in arithmetic progressions.

Overview

A prime number is a natural number that has exactly two distinct natural number divisors: the number 1 and itself.

To find all the prime numbers less than or equal to a given integer n by Eratosthenes's method:

  1. Create a list of consecutive integers from 2 through n: (2, 3, 4, ..., n).
  2. Initially, let p equal 2, the smallest prime number.
  3. Enumerate the multiples of p by counting in increments of p from 2p to n, and mark them in the list (these will be 2p, 3p, 4p, ...; the p itself should not be marked).
  4. Find the smallest number in the list greater than p 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.
  5. When the algorithm terminates, the numbers remaining not marked in the list are all the primes below n.

The main idea here is that every value given to 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).

The key property of the sieve is that only additions are needed, no multiplications or divisions are used.

As a refinement, it is sufficient to mark the numbers in step 3 starting from p2, 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 p2 is greater than n.

Another refinement is to initially list odd numbers only, (3, 5, ..., n), and count in increments of 2p in step 3, thus marking only odd multiples of p. This actually appears in the original algorithm. 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 p are generated that are coprime with those small primes, in the first place.

Example

To find all the prime numbers less than or equal to 30, proceed as follows.

First, generate a list of natural numbers from 2 to 30:

<span typeof="mw:Entity" id="mwdg"> </span>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):

<span typeof="mw:Entity" id="mweQ"> </span>2 3 <span style="color:gray" about="#mwt50" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s> 4 </s>"}},"i":0}}]}' id="mweg"><s> 4 </s></span> 5 <span style="color:gray" about="#mwt51" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s> 6 </s>"}},"i":0}}]}' id="mwew"><s> 6 </s></span> 7 <span style="color:gray" about="#mwt52" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s> 8 </s>"}},"i":0}}]}' id="mwfA"><s> 8 </s></span> 9 <span style="color:gray" about="#mwt53" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>10</s>"}},"i":0}}]}' id="mwfQ"><s>10</s></span> 11 <span style="color:gray" about="#mwt54" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>12</s>"}},"i":0}}]}' id="mwfg"><s>12</s></span> 13 <span style="color:gray" about="#mwt55" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>14</s>"}},"i":0}}]}' id="mwfw"><s>14</s></span> 15 <span style="color:gray" about="#mwt56" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>16</s>"}},"i":0}}]}' id="mwgA"><s>16</s></span> 17 <span style="color:gray" about="#mwt57" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>18</s>"}},"i":0}}]}' id="mwgQ"><s>18</s></span> 19 <span style="color:gray" about="#mwt58" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>20</s>"}},"i":0}}]}' id="mwgg"><s>20</s></span> 21 <span style="color:gray" about="#mwt59" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>22</s>"}},"i":0}}]}' id="mwgw"><s>22</s></span> 23 <span style="color:gray" about="#mwt60" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>24</s>"}},"i":0}}]}' id="mwhA"><s>24</s></span> 25 <span style="color:gray" about="#mwt61" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>26</s>"}},"i":0}}]}' id="mwhQ"><s>26</s></span> 27 <span style="color:gray" about="#mwt62" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>28</s>"}},"i":0}}]}' id="mwhg"><s>28</s></span> 29 <span style="color:gray" about="#mwt63" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>30</s>"}},"i":0}}]}' id="mwhw"><s>30</s></span>

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):

<span typeof="mw:Entity" id="mwig"> </span>2 3 <span style="color:gray" about="#mwt64" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s> 4 </s>"}},"i":0}}]}' id="mwiw"><s> 4 </s></span> 5 <span style="color:gray" about="#mwt65" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s> 6 </s>"}},"i":0}}]}' id="mwjA"><s> 6 </s></span> 7 <span style="color:gray" about="#mwt66" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s> 8 </s>"}},"i":0}}]}' id="mwjQ"><s> 8 </s></span><span style="color:gray" about="#mwt67" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s> 9 </s>"}},"i":0}}]}' id="mwjg"><s> 9 </s></span><span style="color:gray" about="#mwt68" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>10</s>"}},"i":0}}]}' id="mwjw"><s>10</s></span> 11 <span style="color:gray" about="#mwt69" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>12</s>"}},"i":0}}]}' id="mwkA"><s>12</s></span> 13 <span style="color:gray" about="#mwt70" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>14 </s>"}},"i":0}}]}' id="mwkQ"><s>14 </s></span><span style="color:gray" about="#mwt71" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>15 </s>"}},"i":0}}]}' id="mwkg"><s>15 </s></span><span style="color:gray" about="#mwt72" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>16</s>"}},"i":0}}]}' id="mwkw"><s>16</s></span> 17 <span style="color:gray" about="#mwt73" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>18</s>"}},"i":0}}]}' id="mwlA"><s>18</s></span> 19 <span style="color:gray" about="#mwt74" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>20 </s>"}},"i":0}}]}' id="mwlQ"><s>20 </s></span><span style="color:gray" about="#mwt75" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>21 </s>"}},"i":0}}]}' id="mwlg"><s>21 </s></span><span style="color:gray" about="#mwt76" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>22</s>"}},"i":0}}]}' id="mwlw"><s>22</s></span> 23 <span style="color:gray" about="#mwt77" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>24</s>"}},"i":0}}]}' id="mwmA"><s>24</s></span> 25 <span style="color:gray" about="#mwt78" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>26 </s>"}},"i":0}}]}' id="mwmQ"><s>26 </s></span><span style="color:gray" about="#mwt79" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>27 </s>"}},"i":0}}]}' id="mwmg"><s>27 </s></span><span style="color:gray" about="#mwt80" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>28</s>"}},"i":0}}]}' id="mwmw"><s>28</s></span> 29 <span style="color:gray" about="#mwt81" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>30</s>"}},"i":0}}]}' id="mwnA"><s>30</s></span>

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):

<span typeof="mw:Entity" id="mwnw"> </span>2 3 <span style="color:gray" about="#mwt82" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s> 4 </s>"}},"i":0}}]}' id="mwoA"><s> 4 </s></span> 5 <span style="color:gray" about="#mwt83" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s> 6 </s>"}},"i":0}}]}' id="mwoQ"><s> 6 </s></span> 7 <span style="color:gray" about="#mwt84" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s> 8 </s>"}},"i":0}}]}' id="mwog"><s> 8 </s></span><span style="color:gray" about="#mwt85" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s> 9 </s>"}},"i":0}}]}' id="mwow"><s> 9 </s></span><span style="color:gray" about="#mwt86" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>10</s>"}},"i":0}}]}' id="mwpA"><s>10</s></span> 11 <span style="color:gray" about="#mwt87" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>12</s>"}},"i":0}}]}' id="mwpQ"><s>12</s></span> 13 <span style="color:gray" about="#mwt88" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>14 </s>"}},"i":0}}]}' id="mwpg"><s>14 </s></span><span style="color:gray" about="#mwt89" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>15 </s>"}},"i":0}}]}' id="mwpw"><s>15 </s></span><span style="color:gray" about="#mwt90" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>16</s>"}},"i":0}}]}' id="mwqA"><s>16</s></span> 17 <span style="color:gray" about="#mwt91" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>18</s>"}},"i":0}}]}' id="mwqQ"><s>18</s></span> 19 <span style="color:gray" about="#mwt92" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>20 </s>"}},"i":0}}]}' id="mwqg"><s>20 </s></span><span style="color:gray" about="#mwt93" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>21 </s>"}},"i":0}}]}' id="mwqw"><s>21 </s></span><span style="color:gray" about="#mwt94" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>22</s>"}},"i":0}}]}' id="mwrA"><s>22</s></span> 23 <span style="color:gray" about="#mwt95" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>24 </s>"}},"i":0}}]}' id="mwrQ"><s>24 </s></span><span style="color:gray" about="#mwt96" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>25 </s>"}},"i":0}}]}' id="mwrg"><s>25 </s></span><span style="color:gray" about="#mwt97" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>26 </s>"}},"i":0}}]}' id="mwrw"><s>26 </s></span><span style="color:gray" about="#mwt98" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>27 </s>"}},"i":0}}]}' id="mwsA"><s>27 </s></span><span style="color:gray" about="#mwt99" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>28</s>"}},"i":0}}]}' id="mwsQ"><s>28</s></span> 29 <span style="color:gray" about="#mwt100" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"gray","href":"./Template:Gray"},"params":{"1":{"wt":"<s>30</s>"}},"i":0}}]}' id="mwsg"><s>30</s></span>

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:

<span typeof="mw:Entity" id="mwtQ"> </span>2 3 5 7 11 13 17 19 23 29

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Number theory