Sieve of Eratosthenes
how it works
The idea up to 30, as the watch page runs it.
A prime is a whole number from 2 that only 1 and itself divide: 2, 3, 5, 7, 11. To find every prime up to , write the numbers down. 2 is a prime, so cross out its multiples: 4, 6, 8 and on. The next number left, 3, is a prime too, so cross out its multiples. Go on like that. A number never crossed out has no factor but 1 and itself, so it is a prime, and nothing was ever divided. Three shortcuts save work. A crossed out already is passed over, as its multiples are crossed out already. A prime starts at times , because its smaller multiples are crossed out already. And the method stops once times passes . This page counts crossings, one each time a prime crosses a number out, crossed out before or not. The code below, in the language Python, does the same.
| prime | crosses out | crossings |
|---|---|---|
| 2 | 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 | 14 |
| 3 | 9, 12, 15, 18, 21, 24, 27, 30 | 8 |
| 5 | 25, 30 | 2 |
step 1 of 31
Write the numbers from 1 to 30, eight to a row. 1 is no prime, so cross it out first. Every number crossed out from here on has a factor bigger than 1 and smaller than itself, and every one left at the end is a prime.
what it remembers to know where it is
- prime at work
- not yet
- crossing out
- not yet
Where it says not yet, nothing is held there at that moment.
step 2 of 31
2 is not crossed out, so no number between 1 and 2 divides it: 2 is a prime. Cross out its multiples from 4 on. Each multiple of 2 below 4, besides 2 itself, is a smaller number times 2, so it is crossed out already.
what it remembers to know where it is
- prime at work
- 2
- crossing out
- not yet
Where it says not yet, nothing is held there at that moment.
step 3 of 31
Cross out 4, which is 2 × 2.
what it remembers to know where it is
- prime at work
- 2
- crossing out
- 4
step 17 of 31
3 is not crossed out, so no number between 1 and 3 divides it: 3 is a prime. Cross out its multiples from 9 on. Each multiple of 3 below 9, besides 3 itself, is a smaller number times 3, so it is crossed out already.
what it remembers to know where it is
- prime at work
- 3
- crossing out
- not yet
Where it says not yet, nothing is held there at that moment.
step 19 of 31
12 is 4 × 3, but it is crossed out already, as a multiple of 2. Crossing it out again changes nothing, but it still takes a turn.
what it remembers to know where it is
- prime at work
- 3
- crossing out
- 12
step 26 of 31
4 is crossed out already, so a smaller prime divides it, and that prime crossed out every multiple of 4 too. Move on.
what it remembers to know where it is
- prime at work
- not yet
- crossing out
- not yet
Where it says not yet, nothing is held there at that moment.
step 30 of 31
6 × 6 is 36, past 30, so stop. Every number from 2 to 30 that is no prime has a factor bigger than 1 and smaller than 6, so it is crossed out already.
what it remembers to know where it is
- prime at work
- not yet
- crossing out
- not yet
Where it says not yet, nothing is held there at that moment.
step 31 of 31
Every number left is a prime: 10 primes up to 30, found with 24 crossings out.
what it remembers to know where it is
- prime at work
- not yet
- crossing out
- not yet
Where it says not yet, nothing is held there at that moment.
the steps, in plain words
choose a step to mark the Python, a programming language, that does it
- write the numbers from 1 to n, eight to a row; 1 is no prime, so cross it outPython lines 6 and 7
- for each number p from 2, while p × p is at most nPython lines 8, 9 and 13
- if p is crossed out, move on: its multiples are crossed out alreadyPython line 10
- otherwise p is a prime: cross out p × p and every p-th number after it, up to nPython lines 11 and 12
- every number not crossed out is a primePython line 14
the same in Python
The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.
def sieve_of_eratosthenes(n):
"""Find every prime up to n by crossing out multiples, never dividing.
n is a whole number from 2. Returns the primes up to n, smallest first.
"""
crossed = [False] * (n + 1) # crossed[k] is True once k is crossed out
crossed[0] = crossed[1] = True # 0 and 1 are no primes
p = 2
while p * p <= n: # once p times p passes n, every number left is a prime
if not crossed[p]: # a p crossed out is passed over; any other is a prime
for m in range(p * p, n + 1, p): # p times p, then every p-th number
crossed[m] = True # crossed out, whether it was before or not
p += 1
return [k for k in range(2, n + 1) if not crossed[k]] # every number left
how it scales
The plot counts the crossings the method makes up to , measured from 10 to 1,000,000.
Each mark across or up is ten times the one before. The line climbs a little more than one mark up for each mark across: 16,981 crossings at 10,000 and 193,078 at 100,000, about 11 times. So the crossings grow a little faster than : about 1 for each number at 100, and about 2 at a million. Question 1 in section 4 derives a floor and a ceiling.
crossings out, ten times more at each mark up
, the largest number to check, ten times more at each mark to the right
- finding the primes up to , the one run there is for that solid where measured by running it; dashed where its formula carries it on to ten millionslope 1: ten times n, about 11 times the work
| crossings out | time | |
|---|---|---|
| 1,000 | 1,411 | about 1.4 microseconds |
| 100,000 | 193,078 | about 193 microseconds |
| 10,000,000 | 22,850,051 | about 23 milliseconds |
The crossings out are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.
make these counts yourself
Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.
"""Count the sieve of Eratosthenes's crossings yourself, as the plot on this page does.
Run it with Python 3.12 or later. For each n it prints the crossings the method
makes and how many primes it finds, up to n = 1,000,000. The counts are the same
on any computer; only the time differs.
"""
def sieve_of_eratosthenes(n):
"""Find every prime up to n by crossing out multiples, never dividing.
n is a whole number from 2. Returns the primes up to n, smallest first.
"""
crossed = [False] * (n + 1) # crossed[k] is True once k is crossed out
crossed[0] = crossed[1] = True # 0 and 1 are no primes
p = 2
while p * p <= n: # once p times p passes n, every number left is a prime
if not crossed[p]: # a p crossed out is passed over; any other is a prime
for m in range(p * p, n + 1, p): # p times p, then every p-th number
crossed[m] = True # crossed out, whether it was before or not
p += 1
return [k for k in range(2, n + 1) if not crossed[k]] # every number left
def count(n):
"""The same method on n, counting its crossings: one for every number it
crosses out, whether that number was crossed out before or not."""
crossed = [False] * (n + 1)
crossings = 0
p = 2
while p * p <= n:
if not crossed[p]:
for m in range(p * p, n + 1, p):
crossed[m] = True
crossings += 1
p += 1
return crossings
if __name__ == "__main__":
print(f"{'n':>9} {'crossings':>10} {'primes':>7}")
for n in (10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10_000, 20_000, 50_000, 100_000, 200_000, 500_000, 1_000_000):
print(f"{n:>9} {count(n):>10} {len(sieve_of_eratosthenes(n)):>7}")
best, worst and average
Best, worst and average usually name different inputs of one size. Here the input is alone, so each is one input and one run, the same every time. So the table has one column, under the name worst, and no average is worked out.
A case is one kind of input. Here each is one input, so there is one case. A bound says how fast its cost grows as grows.
The counts are in the grid. A count is often the sum of pieces that grow at different speeds, and on long inputs the fastest-growing piece swamps the rest. For example, a count of half of n times n, less half of n: at 2,000 items, 2,000,000 less 1,000. So how a cost grows is what tells one method from another, and the bounds keep just that.
| worstfinding the primes up to , the one run there is for that | |
|---|---|
| crossings out | for each prime with , added up |
| how the crossings out grow in each case | |
| tight, | not derived here; a little faster than , as section 2 shows |
| ceiling, | |
| floor, | |
extra memory: marks, one for each number from 0 to .
Extra memory is the room it needs on top of the input itself, while it runs.
| n | primes with | crossings | crossed out | primes left |
|---|---|---|---|---|
| 10 | 2, 3 | 4 + 1 = 5 | 5 | 4 |
| 16 | 2, 3 | 7 + 3 = 10 | 9 | 6 |
| 30 | 2, 3, 5 | 14 + 8 + 2 = 24 | 19 | 10 |
| 49 | 2, 3, 5, 7 | 23 + 14 + 5 + 1 = 43 | 33 | 15 |
| 64 | 2, 3, 5, 7 | 31 + 19 + 8 + 3 = 61 | 45 | 18 |
where the formula comes from
Three questions, each worked line by line. Question 1 is the count the plot draws.
the move: count each prime's crossings
the line:prime crosses out as crossings 2 4, 6, 8, 10, 12, 14, 16 2 times 2 to 8 7 3 9, 12, 15 3 times 3 to 5 3 Up to 16. 4 is crossed out, so it crosses nothing out. 12 is crossed out twice. why it is allowed: By question 2, the not crossed out are the primes: a that is no prime is crossed out by its smallest prime factor, which comes first. So only primes cross numbers out, those with times at most . A prime crosses out times , then times the next number, , and on: 2 times 2, then 2 times 3. It stops at times , the most that still fits in . Counting from to , both ends in, is numbers, as 2 to 8 is 8 - 2 + 1, which is 7. Each counts, crossed out before or not. For 16, 2 makes 7 and 3 makes 5 - 3 + 1, which is 3. In all, 10.
the move: keep only 2's crossings
the line:why it is allowed: From 4 up, 2 times 2 is at most , so 2's term is there: , less 2, plus 1. Every other term is at least 1, as times at most puts at most , so leaving them out never raises the total. For 16, 10 is at least 7.
guess it, thenthe move: drop the rounding
the line:why it is allowed: Rounding a half down loses at most a half, so is at least . For 16, 7 is at least 6.5.
guess it, thenthe move: one fraction
the line:why it is allowed: 1 is 2 halves, and less 2 is . For 16, 6.5.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 4. From 6 up, is at least half of , so is at least a quarter of , the fixed multiple. means at least a fixed multiple of , above 0, from some on.
guess it, thenthe move: drop the rounding
the line:why it is allowed: Rounding down makes no bigger, so is at most . For 16, 2's term stays 7, and 3's becomes 3.33.
guess it, thenthe move: drop what is below 0
the line:why it is allowed: is below 0, as is at least 2. For 16, 7 becomes 8, and 3.33 becomes 5.33.
guess it, thenthe move: every number, not only primes
the line:why it is allowed: puts at most , so at most . Add a term for every whole number from 2 to there, prime or not: more terms, each above 0. For 16, the root is 4, and 4 adds 16 over 4, so 13.33 becomes 17.33.
guess it, thenthe move: name the harmonic sum
the line:where does this come from?
question 4 of the page expected costHow does grow? the move: name the sum
the line:why it is allowed: Let be the harmonic sum to 1 over , for from 2.
the move: group in blocks
the line:The sum to 1 over 12, about 3.10, each term a column 1 over k tall. Behind each place of a block stands its first term, dashed, so 4 dashed columns of 1 over 4 make 1. The last block is cut short. why it is allowed: Number blocks by from 0. Block starts at 1 over 2 to the power : 1, 1 over 2, 1 over 4, 1 over 8. It stops just before the next start, so it has 2 to the power terms: 1 over 4 to 1 over 7 are 4. The last block may be cut short at 1 over .
the move: each block at most 1
the line:why it is allowed: Each term of block is at most its first, 1 over 2 to the power , as a bigger number underneath makes a smaller fraction. So its 2 to the power terms, or fewer, add to at most 1. is at most 1 per block: 4 for 12.
the move: count the blocks
the line:where does this come from?
question 2 of the page halving and logarithmsHalve , rounding down each time. How many halvings reach 1? the move: name the log rounded down
the line:Rounding down, 11 halves to 5, 2 and 1. In blue, each log rounded down: 3, 2, 1, 0, one less at each halving. why it is allowed: Here is the number halved, from 2 up; for 11, is 3.
the move: rounding down never adds
the line:why it is allowed: For 11, 3 is at most 3.46.
the move: rounding down takes off less than 1
the line:why it is allowed: For 11, 4 is above 3.46.
the move: raise 2 to each side of line 2
the line:why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.
the move: 2 to the log of n is n
the line:why it is allowed: By what a log is. For 11, 8 is at most 11.
the move: raise 2 to each side of line 3
the line:why it is allowed: As in line 4. For 11, about 11 is below 16.
the move: 2 to the log of n is n
the line:why it is allowed: As in line 5. For 11, 11 is below 16.
the move: halve both sides of line 5
the line:why it is allowed: For 11, 4 is at most 5.5, which is not whole.
the move: write the half as a power
the line:why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.
the move: round the half down
the line:why it is allowed: is the half rounded down, 5 for 11. A whole number at most 5.5 is at most 5. The left side, 4 for 11, is whole, as is at least 1 for from 2 up.
the move: halve both sides of line 7
the line:why it is allowed: For 11, 5.5 is below 8.
the move: write the half as a power
the line:why it is allowed: As in line 9. Half of 16 is 8.
the move: round the half down
the line:why it is allowed: Rounding down never adds. For 11, 5 is below 8.
the move: take the log of line 10
the line:why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, the log of 4 is 2, at most about 2.32.
the move: take the log of line 13
the line:why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, about 2.32 is below 3.
the move: round the log down
the line:the number at least below its log rounded down 11 8 16 3 5 4 8 2 2 2 4 1 1 1 2 0 Each halving takes 1 off the log rounded down. why it is allowed: By lines 15 and 17, the log of the half is at least one less than and below , so it rounds down to one less. For 11, about 2.32 rounds down to 2.
the move: use line 18 again and again
the line:why it is allowed: While the log rounded down is 1 or more, the number is at least 2 to the power 1, which is 2. So line 18 takes 1 off each halving, down to 0 after . For 11, 3, 2, 1, 0.
the move: a log rounded down to 0 belongs to 1
the line:why it is allowed: Such a log is at least 0 and below 1, so its number is at least 1 and below 2. It is 1.
why it is allowed: is read the log of rounded down. By lines 5 and 7 of the page halving and logarithms, 2 to that power is at most , and the next power of 2 is above . So blocks start at 1 over 2 to the powers 0 to . For 12, 3.58 rounds down to 3: the 4 blocks of line 2.
the move: drop the rounding
the line:why it is allowed: Rounding down never adds, so putting for can never lower the right side. For 12, 4 becomes about 4.58.
the move: a fact about n
the line:why it is allowed: is at least 2, the log of 2 is 1, and a bigger number has a bigger log.
the move: put the log in place of the 1
the line:why it is allowed: By line 6 this makes the right side of line 5 no smaller. For 12, 4.58 becomes about 7.17.
the move: add the logs
the line:why it is allowed: Two lots of .
the move: a ceiling
the line:why it is allowed: By line 8, is at most 2 times for every from 2. , read is in big O of , says a fixed multiple of is a ceiling of : here 2.
the move: blocks after the first term
the line:The sum to 1 over 12. Inside each term of a block stands its last term, dashed, so 2 dashed columns of 1 over 4 make one half. 1 over 9 to 1 over 12 are left over. why it is allowed: For a floor, group the terms after the first in blocks ending at 1 over 2, 1 over 4, 1 over 8 and on. Each term is then at least its block's last. Whole blocks end at 1 over the powers of 2 from 2 to .
the move: drop the leftovers
the line:why it is allowed: Leftover terms are above zero, so leaving them out makes the total no bigger.
the move: whole blocks at least one half
the line:why it is allowed: Number the whole blocks by from 1. Block runs from just after 1 over half of 2 to the power to 1 over 2 to the power . That is half that many terms, each at least its last, so at least one half. For 12, 1 over 3 + 1 over 4 is at least 2 times 1 over 4. With line 11, is at least 1 plus one half per whole block: 2.5.
the move: count the whole blocks
the line:where does this come from?
question 2 of the page halving and logarithmsHalve , rounding down each time. How many halvings reach 1? the move: name the log rounded down
the line:Rounding down, 11 halves to 5, 2 and 1. In blue, each log rounded down: 3, 2, 1, 0, one less at each halving. why it is allowed: Here is the number halved, from 2 up; for 11, is 3.
the move: rounding down never adds
the line:why it is allowed: For 11, 3 is at most 3.46.
the move: rounding down takes off less than 1
the line:why it is allowed: For 11, 4 is above 3.46.
the move: raise 2 to each side of line 2
the line:why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.
the move: 2 to the log of n is n
the line:why it is allowed: By what a log is. For 11, 8 is at most 11.
the move: raise 2 to each side of line 3
the line:why it is allowed: As in line 4. For 11, about 11 is below 16.
the move: 2 to the log of n is n
the line:why it is allowed: As in line 5. For 11, 11 is below 16.
the move: halve both sides of line 5
the line:why it is allowed: For 11, 4 is at most 5.5, which is not whole.
the move: write the half as a power
the line:why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.
the move: round the half down
the line:why it is allowed: is the half rounded down, 5 for 11. A whole number at most 5.5 is at most 5. The left side, 4 for 11, is whole, as is at least 1 for from 2 up.
the move: halve both sides of line 7
the line:why it is allowed: For 11, 5.5 is below 8.
the move: write the half as a power
the line:why it is allowed: As in line 9. Half of 16 is 8.
the move: round the half down
the line:why it is allowed: Rounding down never adds. For 11, 5 is below 8.
the move: take the log of line 10
the line:why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, the log of 4 is 2, at most about 2.32.
the move: take the log of line 13
the line:why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, about 2.32 is below 3.
the move: round the log down
the line:the number at least below its log rounded down 11 8 16 3 5 4 8 2 2 2 4 1 1 1 2 0 Each halving takes 1 off the log rounded down. why it is allowed: By lines 15 and 17, the log of the half is at least one less than and below , so it rounds down to one less. For 11, about 2.32 rounds down to 2.
the move: use line 18 again and again
the line:why it is allowed: While the log rounded down is 1 or more, the number is at least 2 to the power 1, which is 2. So line 18 takes 1 off each halving, down to 0 after . For 11, 3, 2, 1, 0.
the move: a log rounded down to 0 belongs to 1
the line:why it is allowed: Such a log is at least 0 and below 1, so its number is at least 1 and below 2. It is 1.
why it is allowed: As in line 4, whole blocks end at 1 over 2 to the powers 1 to , that many blocks. For 12, 3.
the move: rounding takes off less than 1
the line:why it is allowed: Rounding down takes off less than 1, so putting less 1 for can only lower the right side. For 12, 2.5 becomes about 2.29.
the move: halve each part
the line:why it is allowed: Half of ( less 1) is half of , less one half.
the move: change the order
the line:why it is allowed: Adding and taking away go in any order.
the move: 1 less one half
the line:why it is allowed: 1 less one half is one half.
the move: drop the one half
the line:why it is allowed: Taking away one half makes the right side smaller. For 12, about 1.79.
the move: a floor
the line:- the ceiling,
- the harmonic sum
- the floor,
From 2 terms on, the harmonic sum lies between its ceiling and floor. At 16: 8, about 3.38, and 2. why it is allowed: By line 18, is at least one half times for every from 2. , read is in big omega of , says a fixed multiple of , above zero, is a floor of .
the move: ceiling and floor together
the line:why it is allowed: , read is in big theta of , says both: by lines 9 and 19, for every from 2, lies between one half and 2 times . It grows like .
why it is allowed: Take out of each term, leaving times (1 over 2, plus 1 over 3, and on). That is without its first 1. For 16, 16 over 2, plus 16 over 3, plus 16 over 4, is 16 times ( less 1), about 17.33. The fold shows how grows.
guess it, thenthe move: how big the harmonic sum gets
the line:why it is allowed: The fold at line 9 shows this in its line 5, with in place of its . From 4 up, that is at least 2, as the fold needs. For 16, is about 2.08, at most 3.
guess it, thenthe move: put it in
the line:why it is allowed: Put line 10 into line 9. Its + 1 and - 1 cancel. For 16, 16 times 2, which is 32.
guess it, thenthe move: drop the rounding
the line:why it is allowed: is at most , and a bigger number has a bigger log.
guess it, thenthe move: half the log
the line:- the floor,
- the crossings,
- the ceiling,
The floor, the crossings and the ceiling on a log-log plot, each mark ten times the one before, as in section 2. The crossings lie between them, about 2 times the floor at 100 and 4 times at a million. why it is allowed: For 16, its root is 4, which is 2 to the power 2, and 16 is 2 to the power 4: half of 4 is 2. In general, 2 to the power of half of , times itself, is 2 to the power , which is . So it is , and is half of . For 16, 8 times 4, which is 32.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 13, with the fixed multiple a half. Lines 5 and 14 grow differently, as and , so they make no . The tight growth needs Mertens's 1874 sum over the primes, not worked out here. At a million, the floor gives 499,999, the count is 2,122,048 and the ceiling 9,965,784, so the count is about 4 times the floor and a fifth of the ceiling.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: a small factor
the line:number a times b a times a 12 2 times 6 4 15 3 times 5 9 25 5 times 5 25 49 7 times 7 49 Each number with its smallest factor above 1 first. never passes the number. why it is allowed: A number from 4 that is no prime is times , both above 1, with at most . Then is at most times , the number itself. For 15, 3 times 5, and 3 times 3 is 9.
the move: the smallest is a prime
the line:why it is allowed: A factor above 1 that is no prime has a smaller factor above 1, which divides the number too, so it is not the smallest. For 12, 4 divides it, and so does 2.
guess it, thenthe move: never a prime
the line:why it is allowed: Each number a crosses out is times a whole number at least 2, so divides it, and is between 1 and the number. A prime has no such factor. So every prime is left.
guess it, thenthe move: so a prime crosses it out
the line:why it is allowed: Take its smallest factor above 1, . By line 2, is a prime, so by line 3 it is never crossed out, and it crosses out its multiples. By line 1, is at most the number, so at most . The number is times a whole number at least , as is its smallest factor. So crosses it out, and every number left is a prime. For 15, 3 does, as 3 times 5.
guess it, thenthe move: why start at p times p
the line:multiple of 5 as first crossed out by 10 5 times 2 2 15 5 times 3 3 20 5 times 4 2 25 5 times 5 5 Up to 25, 5's multiples. Each below 25 is crossed out already, so 5 starts at 25. why it is allowed: It is times some from 2 up to 1 less than . By line 2, 's smallest factor above 1 is a prime , at most , so below . divides , so it divides times . times is at most times , which is below times , so came first and crossed it out. For 5, 10, 15 and 20 are crossed out by 2, 3 and 2.
guess it, thenthe move: where it stops
the line:why it is allowed: By line 4, primes with times at most cross out every number that is no prime, so climbs while times is at most . times itself is at most , and one more, times itself, is past . For 16, 4, as 5 times 5 is 25.
guess it, then
try it with a small : at , line 6 gives , which you can work out by hand
the move: count the steps
the line:up to 16 how many each p's mark read 3 crossings 10 numbers read at the end 15 The steps up to 16, 28 in all. why it is allowed: By question 2's line 6, runs from 2 to , each mark read once. is question 1's crossings. At the end, 2 to are read once each, numbers. For 16, 3 + 10 + 15, which is 28.
the move: drop the other two parts
the line:why it is allowed: The other two parts are at least 0. For 16, 28 is at least 15.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 2. From 2 up, is at least half of , the fixed multiple a half.
guess it, thenthe move: bound each part
the line:why it is allowed: is at most , question 1's line 13 bounds , and is at most .
guess it, thenthe move: root n at most n
the line:why it is allowed: From 1 up, is at most .
guess it, thenthe move: 2n at most n log n
the line:why it is allowed: From 4 up, is at least 2, so is at most . For 16, 64 becomes 96.
guess it, thenthe move: add like terms
the line:why it is allowed: A half and 1 lots of make 3 halves. For 16, 28 is at most 96.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 7, with the fixed multiple 3 halves.
guess it, thenthe move: the room it takes
the line:why it is allowed: The method keeps every mark at once, so its room grows as , whatever the steps do. Section 1's code keeps them as a list of True and False.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
count it yourself
Up to 16, step by step. 1, crossed out at the start by no prime, is not a crossing. A crossing of a number crossed out before still adds 1.
| step | what happens | crossings out |
|---|---|---|
| 1 | Write the numbers from 1 to 16, eight to a row. 1 is no prime, so cross it out first. Every number crossed out from here on has a factor bigger than 1 and smaller than itself, and every one left at the end is a prime. | 0 |
| 2 | 2 is not crossed out, so no number between 1 and 2 divides it: 2 is a prime. Cross out its multiples from 4 on. Each multiple of 2 below 4, besides 2 itself, is a smaller number times 2, so it is crossed out already. | 0 |
| 3 | Cross out 4, which is 2 × 2. | 1 |
| 4 | Cross out 6, which is 3 × 2. | 2 |
| 5 | Cross out 8, which is 4 × 2. | 3 |
| 6 | Cross out 10, which is 5 × 2. | 4 |
| 7 | Cross out 12, which is 6 × 2. | 5 |
| 8 | Cross out 14, which is 7 × 2. | 6 |
| 9 | Cross out 16, which is 8 × 2. | 7 |
| 10 | 3 is not crossed out, so no number between 1 and 3 divides it: 3 is a prime. Cross out its multiples from 9 on. Each multiple of 3 below 9, besides 3 itself, is a smaller number times 3, so it is crossed out already. | 7 |
| 11 | Cross out 9, which is 3 × 3. | 8 |
| 12 | 12 is 4 × 3, but it is crossed out already, as a multiple of 2. Crossing it out again changes nothing, but it still takes a turn. | 9 |
| 13 | Cross out 15, which is 5 × 3. | 10 |
| 14 | 4 is crossed out already, so a smaller prime divides it, and that prime crossed out every multiple of 4 too. Move on. | 10 |
| 15 | 5 × 5 is 25, past 16, so stop. Every number from 2 to 16 that is no prime has a factor bigger than 1 and smaller than 5, so it is crossed out already. | 10 |
| 16 | Every number left is a prime: 6 primes up to 16, found with 10 crossings out. | 10 |
In all, 10 crossings out. The formula from section 4, , gives at : the same.
where you meet it
The sieve is named after Eratosthenes, who lived over 2,000 years ago, and it still finds primes today.
Eratosthenes
Eratosthenes of Cyrene, who also measured the size of the Earth, worked in Alexandria in the 200s BC. The sieve carries his name, and Nicomachus described it about 100 AD.
source: the MacTutor History of Mathematics, Eratosthenes of Cyrene (opens in a new tab), and the Dictionary of Scientific Biography, Nicomachus of Gerasa (opens in a new tab)
primesieve
primesieve is a free library that lists primes very fast, far past what fits in memory at once. It sieves in blocks small enough for the fast memory beside the processor, so it never keeps a mark for every number.
source: primesieve's own documentation (opens in a new tab)
SymPy
SymPy, a Python library for maths, keeps a sieve of Eratosthenes that grows when asked for a bigger prime.
source: the SymPy documentation, sieve (opens in a new tab)
what is used instead
To test one small number, use trial division up to its square root, as question 2 shows why. For a big one, libraries use faster tests, as SymPy does. For numbers past the computer's memory, sieve in blocks, as primesieve does.
how it compares
Trial division asks each number in turn whether 2, 3 and on up to its square root divide it. The sieve divides nothing: each prime crosses out its own multiples. Two of section 1's shortcuts keep its crossings low: it passes over a crossed out already, and starts at times .
| way | up to 100 | up to 1,000,000 |
|---|---|---|
| this sieve | 104 crossings | 2,122,048 crossings |
| starting at 2 times p, not p times p | 113 crossings | 2,197,839 crossings |
| crossing out for every p, not only primes | 146 crossings | 5,985,517 crossings |
| trial division of each number | 236 divisions | 67,740,404 divisions |