Skip to content

Sieve of Eratosthenes

puzzles

Level 1 of 3

Also called Eratosthenes' sieve and Prime number sieve.

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 nn, 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 pp crossed out already is passed over, as its multiples are crossed out already. A prime pp starts at pp times pp, because its smaller multiples are crossed out already. And the method stops once pp times pp passes nn. 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.

primecrosses outcrossings
24, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 3014
39, 12, 15, 18, 21, 24, 27, 308
525, 302
Up to 30. 4 is crossed out, so it crosses nothing out, and 6 times 6 is past 30, so the method stops. 12, 18 and 24 are crossed out twice and 30 three times, so 24 crossings cross out 19 numbers. Left are the 10 primes 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29.
Up to 30: 8 moments of its 31 steps, the steps between them left for the watch page
  1. step 1 of 31

    12345678081624123456789101112131415161718192021222324252627282930

    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.

    watch from here

  2. step 2 of 31

    12345678081624123456789101112131415161718192021222324252627282930

    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.

    watch from here

  3. step 3 of 31

    12345678081624123456789101112131415161718192021222324252627282930

    Cross out 4, which is 2 × 2.

    what it remembers to know where it is
    prime at work
    2
    crossing out
    4

    watch from here

  4. step 17 of 31

    12345678081624123456789101112131415161718192021222324252627282930

    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.

    watch from here

  5. step 19 of 31

    12345678081624123456789101112131415161718192021222324252627282930

    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

    watch from here

  6. step 26 of 31

    12345678081624123456789101112131415161718192021222324252627282930

    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.

    watch from here

  7. step 30 of 31

    12345678081624123456789101112131415161718192021222324252627282930

    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.

    watch from here

  8. step 31 of 31

    12345678081624123456789101112131415161718192021222324252627282930

    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.

    watch from here

the steps, in plain words
  1. write the numbers from 1 to n, eight to a row; 1 is no prime, so cross it outPython lines 6 and 7
  2. for each number p from 2, while p × p is at most nPython lines 8, 9 and 13
  3. if p is crossed out, move on: its multiples are crossed out alreadyPython line 10
  4. otherwise p is a prime: cross out p × p and every p-th number after it, up to nPython lines 11 and 12
  5. 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 nn, 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 nn: 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

slope 1102104106108101102103104105106107slope 1101102103104105106107108101102103104105106107slope 1101102103104105106107108101102103104105106107

nn, the largest number to check, ten times more at each mark to the right

  • finding the primes up to nn, the one run there is for that nnsolid where measured by running it; dashed where its formula carries it on to ten millionslope 1: ten times n, about 11 times the work
how long these crossings take, at a billion a second
nncrossings outtime
1,0001,411about 1.4 microseconds
100,000193,078about 193 microseconds
10,000,00022,850,051about 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 nn alone, so each nn 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 nn is one input, so there is one case. A bound says how fast its cost grows as nn 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.

what each case costs, and how fast that cost grows; section 4 works out every formula in it
worstfinding the primes up to nn, the one run there is for that nn
crossings out⌊np⌋−p+1\lfloor \frac{n}{p} \rfloor - p + 1 for each prime pp with p×p≤np \times p \le n, added up
how the crossings out grow in each case
tight, Θ\Thetanot derived here; a little faster than nn, as section 2 shows
ceiling, OOO(n log⁡2 n)O(n \,\log_2\, n)
floor, Ω\OmegaΩ(n)\Omega(n)

extra memory: n+1n + 1 marks, one for each number from 0 to nn.

Extra memory is the room it needs on top of the input itself, while it runs.

nprimes with p×p≤np \times p \le ncrossingscrossed outprimes left
102, 34 + 1 = 554
162, 37 + 3 = 1096
302, 3, 514 + 8 + 2 = 241910
492, 3, 5, 723 + 14 + 5 + 1 = 433315
642, 3, 5, 731 + 19 + 8 + 3 = 614518
Counted by the run. Every number from 2 to nn is crossed out or left a prime. Crossings count every time a prime crosses a number out. So they are never fewer than the numbers crossed out, and more once nn reaches 12, the first number crossed out twice.

where the formula comes from

Three questions, each worked line by line. Question 1 is the count the plot draws.

Question 1: how many crossings, CC, does the method make up to nn, for nn of 4 or more?
  1. the move: count each prime's crossings

    the line:
    C=∑p primep×p≤n(⌊np⌋−p+1)C = \sum_{\substack{p \text{ prime} \\ p \times p \le n}} \Bigl(\lfloor \frac{n}{p} \rfloor - p + 1\Bigr)
    primecrosses outascrossings
    24, 6, 8, 10, 12, 14, 162 times 2 to 87
    39, 12, 153 times 3 to 53
    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 pp not crossed out are the primes: a pp that is no prime is crossed out by its smallest prime factor, which comes first. So only primes cross numbers out, those with pp times pp at most nn. A prime pp crosses out pp times pp, then pp times the next number, p+1p + 1, and on: 2 times 2, then 2 times 3. It stops at pp times ⌊np⌋\lfloor \frac{n}{p} \rfloor, the most that still fits in nn. Counting from pp to ⌊np⌋\lfloor \frac{n}{p} \rfloor, both ends in, is ⌊np⌋−p+1\lfloor \frac{n}{p} \rfloor - p + 1 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.

  2. the move: keep only 2's crossings

    the line:
    C≥⌊n2⌋−1C \ge \lfloor \frac{n}{2} \rfloor - 1

    why it is allowed: From 4 up, 2 times 2 is at most nn, so 2's term is there: ⌊n2⌋\lfloor \frac{n}{2} \rfloor, less 2, plus 1. Every other term is at least 1, as pp times pp at most nn puts pp at most ⌊np⌋\lfloor \frac{n}{p} \rfloor, so leaving them out never raises the total. For 16, 10 is at least 7.

  3. the move: drop the rounding

    the line:
    C≥n−12−1C \ge \frac{n - 1}{2} - 1

    why it is allowed: Rounding a half down loses at most a half, so ⌊n2⌋\lfloor \frac{n}{2} \rfloor is at least n−12\frac{n - 1}{2}. For 16, 7 is at least 6.5.

  4. the move: one fraction

    the line:
    C≥n−32C \ge \frac{n - 3}{2}

    why it is allowed: 1 is 2 halves, and n−1n - 1 less 2 is n−3n - 3. For 16, 6.5.

  5. the move: read the bound

    the line:
    C∈Ω(n)C \in \Omega(n)

    why it is allowed: By line 4. From 6 up, n−3n - 3 is at least half of nn, so CC is at least a quarter of nn, the fixed multiple. Ω(n)\Omega(n) means at least a fixed multiple of nn, above 0, from some nn on.

  6. the move: drop the rounding

    the line:
    C≤∑p primep×p≤n(np−p+1)C \le \sum_{\substack{p \text{ prime} \\ p \times p \le n}} \Bigl(\frac{n}{p} - p + 1\Bigr)

    why it is allowed: Rounding down makes no bigger, so ⌊np⌋\lfloor \frac{n}{p} \rfloor is at most np\frac{n}{p}. For 16, 2's term stays 7, and 3's becomes 3.33.

  7. the move: drop what is below 0

    the line:
    C≤∑p primep×p≤nnpC \le \sum_{\substack{p \text{ prime} \\ p \times p \le n}} \frac{n}{p}

    why it is allowed: −p+1- p + 1 is below 0, as pp is at least 2. For 16, 7 becomes 8, and 3.33 becomes 5.33.

  8. the move: every number, not only primes

    the line:
    C≤∑k=2⌊n⌋nkC \le \sum_{k=2}^{\lfloor \sqrt{n} \rfloor} \frac{n}{k}

    why it is allowed: p×p≤np \times p \le n puts pp at most n\sqrt{n}, so at most ⌊n⌋\lfloor \sqrt{n} \rfloor. Add a term for every whole number kk 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.

  9. the move: name the harmonic sum

    the line:
    C≤n(H(⌊n⌋)−1)C \le n \bigl(H(\lfloor \sqrt{n} \rfloor) - 1\bigr)
    where does this come from?
    question 4 of the page expected costHow does 1+12+13+⋯+1n1 + \frac{1}{2} + \frac{1}{3} + \dots + \frac{1}{n} grow?
    1. the move: name the sum

      the line:
      H=∑k=1n1kH = \sum_{k=1}^{n} \frac{1}{k}

      why it is allowed: Let HH be the harmonic sum to 1 over nn, for nn from 2.

    2. the move: group in blocks

      the line:
      blocks, each starting at1 over a power of 2\begin{gathered} \text{blocks, each starting at} \\ \text{1 over a power of 2} \end{gathered}
      blocktermsadds to01≤ 112≤ 124≤ 135≤ 1
      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 jj from 0. Block jj starts at 1 over 2 to the power jj: 1, 1 over 2, 1 over 4, 1 over 8. It stops just before the next start, so it has 2 to the power jj terms: 1 over 4 to 1 over 7 are 4. The last block may be cut short at 1 over nn.

    3. the move: each block at most 1

      the line:
      H≤the number of blocksH \le \text{the number of blocks}

      why it is allowed: Each term of block jj is at most its first, 1 over 2 to the power jj, as a bigger number underneath makes a smaller fraction. So its 2 to the power jj terms, or fewer, add to at most 1. HH is at most 1 per block: 4 for 12.

    4. the move: count the blocks

      the line:
      H≤⌊log⁡2 n⌋+1H \le \lfloor \log_2\, n \rfloor + 1
      where does this come from?
      question 2 of the page halving and logarithmsHalve nn, rounding down each time. How many halvings reach 1?
      1. the move: name the log rounded down

        the line:
        K=⌊log⁡2 n⌋K = \lfloor \log_2\, n \rfloor
        numberlog× 2+ 14283164113.46352.322211100
        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 nn is the number halved, from 2 up; for 11, KK is 3.

      2. the move: rounding down never adds

        the line:
        ⌊log⁡2 n⌋≤log⁡2 n\lfloor \log_2\, n \rfloor \le \log_2\, n

        why it is allowed: For 11, 3 is at most 3.46.

      3. the move: rounding down takes off less than 1

        the line:
         log⁡2 n<⌊log⁡2 n⌋+1\begin{gathered} \,\log_2\, n \\ < \lfloor \log_2\, n \rfloor + 1 \end{gathered}

        why it is allowed: For 11, 4 is above 3.46.

      4. the move: raise 2 to each side of line 2

        the line:
        2⌊log⁡2 n⌋≤2log⁡2 n2^{\lfloor \log_2\, n \rfloor} \le 2^{\log_2\, n}

        why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.

      5. the move: 2 to the log of n is n

        the line:
        2⌊log⁡2 n⌋≤n2^{\lfloor \log_2\, n \rfloor} \le n

        why it is allowed: By what a log is. For 11, 8 is at most 11.

      6. the move: raise 2 to each side of line 3

        the line:
        2log⁡2 n<2⌊log⁡2 n⌋+12^{\log_2\, n} < 2^{\lfloor \log_2\, n \rfloor + 1}

        why it is allowed: As in line 4. For 11, about 11 is below 16.

      7. the move: 2 to the log of n is n

        the line:
        n<2⌊log⁡2 n⌋+1n < 2^{\lfloor \log_2\, n \rfloor + 1}

        why it is allowed: As in line 5. For 11, 11 is below 16.

      8. the move: halve both sides of line 5

        the line:
        2⌊log⁡2 n⌋/2≤n/22^{\lfloor \log_2\, n \rfloor} / 2 \le n / 2

        why it is allowed: For 11, 4 is at most 5.5, which is not whole.

      9. the move: write the half as a power

        the line:
        2⌊log⁡2 n⌋−1≤n/22^{\lfloor \log_2\, n \rfloor - 1} \le n / 2

        why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.

      10. the move: round the half down

        the line:
        2⌊log⁡2 n⌋−1≤⌊n/2⌋2^{\lfloor \log_2\, n \rfloor - 1} \le \lfloor n / 2 \rfloor

        why it is allowed: ⌊n/2⌋\lfloor n / 2 \rfloor 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 KK is at least 1 for nn from 2 up.

      11. the move: halve both sides of line 7

        the line:
        n/2<2⌊log⁡2 n⌋+1/2n / 2 < 2^{\lfloor \log_2\, n \rfloor + 1} / 2

        why it is allowed: For 11, 5.5 is below 8.

      12. the move: write the half as a power

        the line:
        n/2<2⌊log⁡2 n⌋n / 2 < 2^{\lfloor \log_2\, n \rfloor}

        why it is allowed: As in line 9. Half of 16 is 8.

      13. the move: round the half down

        the line:
        ⌊n/2⌋<2⌊log⁡2 n⌋\lfloor n / 2 \rfloor < 2^{\lfloor \log_2\, n \rfloor}

        why it is allowed: Rounding down never adds. For 11, 5 is below 8.

      14. the move: take the log of line 10

        the line:
         log⁡2 2⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \,\log_2\, 2^{\lfloor \log_2\, n \rfloor - 1} \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

        why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.

      15. the move: the log of a power of 2 is the power

        the line:
        ⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \lfloor \log_2\, n \rfloor - 1 \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

        why it is allowed: For 11, the log of 4 is 2, at most about 2.32.

      16. the move: take the log of line 13

        the line:
         log⁡2 ⌊n/2⌋<log⁡2 2⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \log_2\, 2^{\lfloor \log_2\, n \rfloor} \end{gathered}

        why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.

      17. the move: the log of a power of 2 is the power

        the line:
         log⁡2 ⌊n/2⌋<⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \lfloor \log_2\, n \rfloor \end{gathered}

        why it is allowed: For 11, about 2.32 is below 3.

      18. the move: round the log down

        the line:
        ⌊log⁡2 ⌊n/2⌋⌋=⌊log⁡2 n⌋−1\begin{gathered} \lfloor \log_2\, \lfloor n / 2 \rfloor \rfloor \\ = \lfloor \log_2\, n \rfloor - 1 \end{gathered}
        the numberat leastbelowits log rounded down
        118163
        5482
        2241
        1120
        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 KK and below KK, so it rounds down to one less. For 11, about 2.32 rounds down to 2.

      19. the move: use line 18 again and again

        the line:
        after K halvings,rounding down,the log roundeddown is 0\begin{gathered} \text{after } K \text{ halvings,} \\ \text{rounding down,} \\ \text{the log rounded} \\ \text{down is 0} \end{gathered}

        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 KK. For 11, 3, 2, 1, 0.

      20. the move: a log rounded down to 0 belongs to 1

        the line:
        K halvings,rounding down,bring n to 1\begin{gathered} K \text{ halvings,} \\ \text{rounding down,} \\ \text{bring } n \text{ to 1} \end{gathered}

        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.

      the whole page: halving and logarithms

      why it is allowed: ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor is read the log of nn rounded down. By lines 5 and 7 of the page halving and logarithms, 2 to that power is at most nn, and the next power of 2 is above nn. So ⌊log⁡2 n⌋+1\lfloor \log_2\, n \rfloor + 1 blocks start at 1 over 2 to the powers 0 to ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor. For 12, 3.58 rounds down to 3: the 4 blocks of line 2.

    5. the move: drop the rounding

      the line:
      H≤log⁡2 n+1H \le \log_2\, n + 1

      why it is allowed: Rounding down never adds, so putting log⁡2 n\log_2\, n for ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor can never lower the right side. For 12, 4 becomes about 4.58.

    6. the move: a fact about n

      the line:
      1≤log⁡2 n1 \le \log_2\, n

      why it is allowed: nn is at least 2, the log of 2 is 1, and a bigger number has a bigger log.

    7. the move: put the log in place of the 1

      the line:
      H≤log⁡2 n+log⁡2 nH \le \log_2\, n + \log_2\, n

      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.

    8. the move: add the logs

      the line:
      H≤2 log⁡2 nH \le 2 \,\log_2\, n

      why it is allowed: Two lots of log⁡2 n\log_2\, n.

    9. the move: a ceiling

      the line:
      H∈O(log⁡2 n)H \in O(\log_2\, n)

      why it is allowed: By line 8, HH is at most 2 times log⁡2 n\log_2\, n for every nn from 2. H∈O(log⁡2 n)H \in O(\log_2\, n), read HH is in big O of log⁡2 n\log_2\, n, says a fixed multiple of log⁡2 n\log_2\, n is a ceiling of HH: here 2.

    10. the move: blocks after the first term

      the line:
      after the first term,blocks, each ending at1 over a power of 2\begin{gathered} \text{after the first term,} \\ \text{blocks, each ending at} \\ \text{1 over a power of 2} \end{gathered}
      blocktermsadds tofirst11≥ ½22≥ ½34≥ ½left over
      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 nn.

    11. the move: drop the leftovers

      the line:
      H is at least 1 plusthe whole blocksadded up\begin{gathered} H \text{ is at least 1 plus} \\ \text{the whole blocks} \\ \text{added up} \end{gathered}

      why it is allowed: Leftover terms are above zero, so leaving them out makes the total no bigger.

    12. the move: whole blocks at least one half

      the line:
      H≥1+12×the numberof whole blocks\begin{gathered} H \ge 1 + \frac{1}{2} \times \text{the number} \\ \text{of whole blocks} \end{gathered}

      why it is allowed: Number the whole blocks by ii from 1. Block ii runs from just after 1 over half of 2 to the power ii to 1 over 2 to the power ii. 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, HH is at least 1 plus one half per whole block: 2.5.

    13. the move: count the whole blocks

      the line:
      H≥1+⌊log⁡2 n⌋2H \ge 1 + \frac{\lfloor \log_2\, n \rfloor}{2}
      where does this come from?
      question 2 of the page halving and logarithmsHalve nn, rounding down each time. How many halvings reach 1?
      1. the move: name the log rounded down

        the line:
        K=⌊log⁡2 n⌋K = \lfloor \log_2\, n \rfloor
        numberlog× 2+ 14283164113.46352.322211100
        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 nn is the number halved, from 2 up; for 11, KK is 3.

      2. the move: rounding down never adds

        the line:
        ⌊log⁡2 n⌋≤log⁡2 n\lfloor \log_2\, n \rfloor \le \log_2\, n

        why it is allowed: For 11, 3 is at most 3.46.

      3. the move: rounding down takes off less than 1

        the line:
         log⁡2 n<⌊log⁡2 n⌋+1\begin{gathered} \,\log_2\, n \\ < \lfloor \log_2\, n \rfloor + 1 \end{gathered}

        why it is allowed: For 11, 4 is above 3.46.

      4. the move: raise 2 to each side of line 2

        the line:
        2⌊log⁡2 n⌋≤2log⁡2 n2^{\lfloor \log_2\, n \rfloor} \le 2^{\log_2\, n}

        why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.

      5. the move: 2 to the log of n is n

        the line:
        2⌊log⁡2 n⌋≤n2^{\lfloor \log_2\, n \rfloor} \le n

        why it is allowed: By what a log is. For 11, 8 is at most 11.

      6. the move: raise 2 to each side of line 3

        the line:
        2log⁡2 n<2⌊log⁡2 n⌋+12^{\log_2\, n} < 2^{\lfloor \log_2\, n \rfloor + 1}

        why it is allowed: As in line 4. For 11, about 11 is below 16.

      7. the move: 2 to the log of n is n

        the line:
        n<2⌊log⁡2 n⌋+1n < 2^{\lfloor \log_2\, n \rfloor + 1}

        why it is allowed: As in line 5. For 11, 11 is below 16.

      8. the move: halve both sides of line 5

        the line:
        2⌊log⁡2 n⌋/2≤n/22^{\lfloor \log_2\, n \rfloor} / 2 \le n / 2

        why it is allowed: For 11, 4 is at most 5.5, which is not whole.

      9. the move: write the half as a power

        the line:
        2⌊log⁡2 n⌋−1≤n/22^{\lfloor \log_2\, n \rfloor - 1} \le n / 2

        why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.

      10. the move: round the half down

        the line:
        2⌊log⁡2 n⌋−1≤⌊n/2⌋2^{\lfloor \log_2\, n \rfloor - 1} \le \lfloor n / 2 \rfloor

        why it is allowed: ⌊n/2⌋\lfloor n / 2 \rfloor 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 KK is at least 1 for nn from 2 up.

      11. the move: halve both sides of line 7

        the line:
        n/2<2⌊log⁡2 n⌋+1/2n / 2 < 2^{\lfloor \log_2\, n \rfloor + 1} / 2

        why it is allowed: For 11, 5.5 is below 8.

      12. the move: write the half as a power

        the line:
        n/2<2⌊log⁡2 n⌋n / 2 < 2^{\lfloor \log_2\, n \rfloor}

        why it is allowed: As in line 9. Half of 16 is 8.

      13. the move: round the half down

        the line:
        ⌊n/2⌋<2⌊log⁡2 n⌋\lfloor n / 2 \rfloor < 2^{\lfloor \log_2\, n \rfloor}

        why it is allowed: Rounding down never adds. For 11, 5 is below 8.

      14. the move: take the log of line 10

        the line:
         log⁡2 2⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \,\log_2\, 2^{\lfloor \log_2\, n \rfloor - 1} \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

        why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.

      15. the move: the log of a power of 2 is the power

        the line:
        ⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \lfloor \log_2\, n \rfloor - 1 \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

        why it is allowed: For 11, the log of 4 is 2, at most about 2.32.

      16. the move: take the log of line 13

        the line:
         log⁡2 ⌊n/2⌋<log⁡2 2⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \log_2\, 2^{\lfloor \log_2\, n \rfloor} \end{gathered}

        why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.

      17. the move: the log of a power of 2 is the power

        the line:
         log⁡2 ⌊n/2⌋<⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \lfloor \log_2\, n \rfloor \end{gathered}

        why it is allowed: For 11, about 2.32 is below 3.

      18. the move: round the log down

        the line:
        ⌊log⁡2 ⌊n/2⌋⌋=⌊log⁡2 n⌋−1\begin{gathered} \lfloor \log_2\, \lfloor n / 2 \rfloor \rfloor \\ = \lfloor \log_2\, n \rfloor - 1 \end{gathered}
        the numberat leastbelowits log rounded down
        118163
        5482
        2241
        1120
        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 KK and below KK, so it rounds down to one less. For 11, about 2.32 rounds down to 2.

      19. the move: use line 18 again and again

        the line:
        after K halvings,rounding down,the log roundeddown is 0\begin{gathered} \text{after } K \text{ halvings,} \\ \text{rounding down,} \\ \text{the log rounded} \\ \text{down is 0} \end{gathered}

        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 KK. For 11, 3, 2, 1, 0.

      20. the move: a log rounded down to 0 belongs to 1

        the line:
        K halvings,rounding down,bring n to 1\begin{gathered} K \text{ halvings,} \\ \text{rounding down,} \\ \text{bring } n \text{ to 1} \end{gathered}

        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.

      the whole page: halving and logarithms

      why it is allowed: As in line 4, whole blocks end at 1 over 2 to the powers 1 to ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor, that many blocks. For 12, 3.

    14. the move: rounding takes off less than 1

      the line:
      H≥1+log⁡2 n−12H \ge 1 + \frac{\log_2\, n - 1}{2}

      why it is allowed: Rounding down takes off less than 1, so putting log⁡2 n\log_2\, n less 1 for ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor can only lower the right side. For 12, 2.5 becomes about 2.29.

    15. the move: halve each part

      the line:
      H≥1+log⁡2 n2−12H \ge 1 + \frac{\log_2\, n}{2} - \frac{1}{2}

      why it is allowed: Half of (log⁡2 n\log_2\, n less 1) is half of log⁡2 n\log_2\, n, less one half.

    16. the move: change the order

      the line:
      H≥1−12+log⁡2 n2H \ge 1 - \frac{1}{2} + \frac{\log_2\, n}{2}

      why it is allowed: Adding and taking away go in any order.

    17. the move: 1 less one half

      the line:
      H≥12+log⁡2 n2H \ge \frac{1}{2} + \frac{\log_2\, n}{2}

      why it is allowed: 1 less one half is one half.

    18. the move: drop the one half

      the line:
      H≥log⁡2 n2H \ge \frac{\log_2\, n}{2}

      why it is allowed: Taking away one half makes the right side smaller. For 12, about 1.79.

    19. the move: a floor

      the line:
      H∈Ω(log⁡2 n)H \in \Omega(\log_2\, n)
      the sumterms024681005101520the sumterms024681005101520
      • the ceiling, 2 log⁡2 n2 \,\log_2\, n
      • the harmonic sum
      • the floor, log⁡2 n2\frac{\log_2\, n}{2}
      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, HH is at least one half times log⁡2 n\log_2\, n for every nn from 2. H∈Ω(log⁡2 n)H \in \Omega(\log_2\, n), read HH is in big omega of log⁡2 n\log_2\, n, says a fixed multiple of log⁡2 n\log_2\, n, above zero, is a floor of HH.

    20. the move: ceiling and floor together

      the line:
      H∈Θ(log⁡2 n)H \in \Theta(\log_2\, n)

      why it is allowed: H∈Θ(log⁡2 n)H \in \Theta(\log_2\, n), read HH is in big theta of log⁡2 n\log_2\, n, says both: by lines 9 and 19, for every nn from 2, HH lies between one half and 2 times log⁡2 n\log_2\, n. It grows like log⁡2 n\log_2\, n.

    the whole page: expected cost

    why it is allowed: Take nn out of each term, leaving nn times (1 over 2, plus 1 over 3, and on). That is HH without its first 1. For 16, 16 over 2, plus 16 over 3, plus 16 over 4, is 16 times (H(4)H(4) less 1), about 17.33. The fold shows how HH grows.

  10. the move: how big the harmonic sum gets

    the line:
    H(⌊n⌋)≤log⁡2 ⌊n⌋+1H(\lfloor \sqrt{n} \rfloor) \le \log_2\, \lfloor \sqrt{n} \rfloor + 1

    why it is allowed: The fold at line 9 shows this in its line 5, with ⌊n⌋\lfloor \sqrt{n} \rfloor in place of its nn. From 4 up, that is at least 2, as the fold needs. For 16, H(4)H(4) is about 2.08, at most 3.

  11. the move: put it in

    the line:
    C≤n log⁡2 ⌊n⌋C \le n \,\log_2\, \lfloor \sqrt{n} \rfloor

    why it is allowed: Put line 10 into line 9. Its + 1 and - 1 cancel. For 16, 16 times 2, which is 32.

  12. the move: drop the rounding

    the line:
    C≤n log⁡2 nC \le n \,\log_2\, \sqrt{n}

    why it is allowed: ⌊n⌋\lfloor \sqrt{n} \rfloor is at most n\sqrt{n}, and a bigger number has a bigger log.

  13. the move: half the log

    the line:
    C≤n2 log⁡2 nC \le \frac{n}{2} \,\log_2\, n
    102104106108101102103104105106107101102103104105106107108109101102103104105106107
    • the floor, ⌊n2⌋−1\lfloor \frac{n}{2} \rfloor - 1
    • the crossings, CC
    • the ceiling, n2 log⁡2 n\frac{n}{2} \,\log_2\, n
    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 log⁡2 n\log_2\, n, times itself, is 2 to the power log⁡2 n\log_2\, n, which is nn. So it is n\sqrt{n}, and log⁡2 n\log_2\, \sqrt{n} is half of log⁡2 n\log_2\, n. For 16, 8 times 4, which is 32.

  14. the move: read the bound

    the line:
    C∈O(n log⁡2 n)C \in O(n \,\log_2\, n)

    why it is allowed: By line 13, with the fixed multiple a half. Lines 5 and 14 grow differently, as nn and n log⁡2 nn \,\log_2\, n, so they make no Θ\Theta. 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.

try it with a small nn: at n=16n = 16, line 1 gives C=10C = 10, which you can work out by hand

Question 2: why is every number left a prime, and where does pp stop? Let SS be the last pp whose mark the method reads, for nn of 4 or more.
  1. the move: a small factor

    the line:
    a number from 4 that is no primehas a factor a above 1with a×a at mostthe number\begin{gathered} \text{a number from 4 that is no prime} \\ \text{has a factor } a \text{ above 1} \\ \text{with } a \times a \text{ at most} \\ \text{the number} \end{gathered}
    numbera times ba times a
    122 times 64
    153 times 59
    255 times 525
    497 times 749
    Each number with its smallest factor above 1 first. a×aa \times a never passes the number.

    why it is allowed: A number from 4 that is no prime is aa times bb, both above 1, with aa at most bb. Then a×aa \times a is at most aa times bb, the number itself. For 15, 3 times 5, and 3 times 3 is 9.

  2. the move: the smallest is a prime

    the line:
    its smallest factor above 1is a prime\begin{gathered} \text{its smallest factor above 1} \\ \text{is a prime} \end{gathered}

    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.

  3. the move: never a prime

    the line:
    no prime isever crossed out\begin{gathered} \text{no prime is} \\ \text{ever crossed out} \end{gathered}

    why it is allowed: Each number a pp crosses out is pp times a whole number at least 2, so pp divides it, and pp is between 1 and the number. A prime has no such factor. So every prime is left.

  4. the move: so a prime crosses it out

    the line:
    every number from 2 to nthat is no primeis crossed out\begin{gathered} \text{every number from 2 to } n \\ \text{that is no prime} \\ \text{is crossed out} \end{gathered}

    why it is allowed: Take its smallest factor above 1, pp. By line 2, pp is a prime, so by line 3 it is never crossed out, and it crosses out its multiples. By line 1, p×pp \times p is at most the number, so at most nn. The number is pp times a whole number at least pp, as pp is its smallest factor. So pp crosses it out, and every number left is a prime. For 15, 3 does, as 3 times 5.

  5. the move: why start at p times p

    the line:
    each multiple of p from 2pbelow p×p iscrossed out already\begin{gathered} \text{each multiple of } p \text{ from } 2p \\ \text{below } p \times p \text{ is} \\ \text{crossed out already} \end{gathered}
    multiple of 5asfirst crossed out by
    105 times 22
    155 times 33
    205 times 42
    255 times 55
    Up to 25, 5's multiples. Each below 25 is crossed out already, so 5 starts at 25.

    why it is allowed: It is pp times some kk from 2 up to 1 less than pp. By line 2, kk's smallest factor above 1 is a prime qq, at most kk, so below pp. qq divides kk, so it divides pp times kk. qq times qq is at most qq times kk, which is below pp times kk, so qq came first and crossed it out. For 5, 10, 15 and 20 are crossed out by 2, 3 and 2.

  6. the move: where it stops

    the line:
    S=⌊n⌋S = \lfloor \sqrt{n} \rfloor

    why it is allowed: By line 4, primes with pp times pp at most nn cross out every number that is no prime, so pp climbs while pp times pp is at most nn. ⌊n⌋\lfloor \sqrt{n} \rfloor times itself is at most nn, and one more, times itself, is past nn. For 16, 4, as 5 times 5 is 25.

try it with a small nn: at n=16n = 16, line 6 gives S=4S = 4, which you can work out by hand

Question 3: what does the whole method cost, for nn of 4 or more? Let TT be its steps: each pp whose mark is read, each crossing, and each number read at the end.
  1. the move: count the steps

    the line:
    T=(⌊n⌋−1)+C+(n−1)T = (\lfloor \sqrt{n} \rfloor - 1) + C + (n - 1)
    up to 16how many
    each p's mark read3
    crossings10
    numbers read at the end15
    The steps up to 16, 28 in all.

    why it is allowed: By question 2's line 6, pp runs from 2 to ⌊n⌋\lfloor \sqrt{n} \rfloor, each mark read once. CC is question 1's crossings. At the end, 2 to nn are read once each, n−1n - 1 numbers. For 16, 3 + 10 + 15, which is 28.

  2. the move: drop the other two parts

    the line:
    T≥n−1T \ge n - 1

    why it is allowed: The other two parts are at least 0. For 16, 28 is at least 15.

  3. the move: read the bound

    the line:
    T∈Ω(n)T \in \Omega(n)

    why it is allowed: By line 2. From 2 up, n−1n - 1 is at least half of nn, the fixed multiple a half.

  4. the move: bound each part

    the line:
    T≤n+n2 log⁡2 n+nT \le \sqrt{n} + \frac{n}{2} \,\log_2\, n + n

    why it is allowed: ⌊n⌋−1\lfloor \sqrt{n} \rfloor - 1 is at most n\sqrt{n}, question 1's line 13 bounds CC, and n−1n - 1 is at most nn.

  5. the move: root n at most n

    the line:
    T≤n2 log⁡2 n+2nT \le \frac{n}{2} \,\log_2\, n + 2n

    why it is allowed: From 1 up, n\sqrt{n} is at most nn.

  6. the move: 2n at most n log n

    the line:
    T≤n2 log⁡2 n+n log⁡2 nT \le \frac{n}{2} \,\log_2\, n + n \,\log_2\, n

    why it is allowed: From 4 up, log⁡2 n\log_2\, n is at least 2, so 2n2n is at most n log⁡2 nn \,\log_2\, n. For 16, 64 becomes 96.

  7. the move: add like terms

    the line:
    T≤32n log⁡2 nT \le \frac{3}{2} n \,\log_2\, n

    why it is allowed: A half and 1 lots of n log⁡2 nn \,\log_2\, n make 3 halves. For 16, 28 is at most 96.

  8. the move: read the bound

    the line:
    T∈O(n log⁡2 n)T \in O(n \,\log_2\, n)

    why it is allowed: By line 7, with the fixed multiple 3 halves.

  9. the move: the room it takes

    the line:
    n+1 marks, one foreach number from 0 to n\begin{gathered} n + 1 \text{ marks, one for} \\ \text{each number from 0 to } n \end{gathered}

    why it is allowed: The method keeps every mark at once, so its room grows as nn, whatever the steps do. Section 1's code keeps them as a list of True and False.

try it with a small nn: at n=16n = 16, line 1 gives T=28T = 28, 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.

16, every step written down
stepwhat happenscrossings out
1Write 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
22 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
3Cross out 4, which is 2 × 2.1
4Cross out 6, which is 3 × 2.2
5Cross out 8, which is 4 × 2.3
6Cross out 10, which is 5 × 2.4
7Cross out 12, which is 6 × 2.5
8Cross out 14, which is 7 × 2.6
9Cross out 16, which is 8 × 2.7
103 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
11Cross out 9, which is 3 × 3.8
1212 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
13Cross out 15, which is 5 × 3.10
144 is crossed out already, so a smaller prime divides it, and that prime crossed out every multiple of 4 too. Move on.10
155 × 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
16Every 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, ∑p primep×p≤n(⌊np⌋−p+1)\sum_{\substack{p \text{ prime} \\ p \times p \le n}} \Bigl(\lfloor \frac{n}{p} \rfloor - p + 1\Bigr), gives C=10C = 10 at n=16n = 16: 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 pp crossed out already, and starts at pp times pp.

wayup to 100up to 1,000,000
this sieve104 crossings2,122,048 crossings
starting at 2 times p, not p times p113 crossings2,197,839 crossings
crossing out for every p, not only primes146 crossings5,985,517 crossings
trial division of each number236 divisions67,740,404 divisions
Each row counted by running the method. A division and a crossing are different steps, so compare how each row grows from 100 to a million.