 .  The sieve works by first writing down all
numbers up to
.  The sieve works by first writing down all
numbers up to  , noting that
, noting that  is prime, and crossing off all
multiples of
 is prime, and crossing off all
multiples of  .  Next, note that the first number not crossed off is
.  Next, note that the first number not crossed off is  , 
which is prime, and cross off all multiples of
, 
which is prime, and cross off all multiples of  , etc.  
Repeating this process, we obtain a list of the primes up to
, etc.  
Repeating this process, we obtain a list of the primes up to  .
Formally, the algorithm is as follows:
.
Formally, the algorithm is as follows:
 , this algorithm computes a list of the 
primes up to
, this algorithm computes a list of the 
primes up to  .
.
![$ X=[3,5,\ldots]$](img252.png) be the list
of all odd integers between
 be the list
of all odd integers between  and
 and  .  Let
.  Let ![$ P=[2]$](img253.png) be the list
of primes found so far.
 be the list
of primes found so far.  
 be the first element of
 be the first element of  .
If
.
If 
 , append each element of
, append each element of  to
to  and terminate.   Otherwise append
 and terminate.   Otherwise append  to
 to  .
.
 equal to the sublist of elements in
 equal to the sublist of elements in  that
are not divisible by
 that
are not divisible by  .  
Go to step 2.
.  
Go to step 2.
 using the sieve, we
proceed as follows.  First
 using the sieve, we
proceed as follows.  First ![$ P=[2]$](img253.png) and
 and 
![$\displaystyle X = [3,5,7,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39].$](img258.png) 
We append
 to
 to  and cross off all multiples of
 and cross off all multiples of  to obtain
the new list
 to obtain
the new list
![$\displaystyle X = [5,7,11,13,17,19,23,25,29,31,35,37].$](img259.png) 
Next we append
 to
 to  , obtaining
, obtaining ![$ P=[2,3,5]$](img260.png) , and cross off
the multiples of
, and cross off
the multiples of  , to obtain
, to obtain 
![$ X = [7,11,13,17,19,23,29,31,37].$](img261.png) Because
Because 
 , we append
, we append  to
 to  and find that the
primes less than
 and find that the
primes less than  are
 are
 
 of
 of  satisfies
 satisfies 
 , 
then each element of
, 
then each element of  is prime.  
To see this, suppose
 is prime.  
To see this, suppose  is in
 is in  , so
, so 
 and that
 and that  is divisible by
no prime that is
 is divisible by
no prime that is 
 .  Write
.  Write 
 with
the
 with
the  distinct primes ordered so that
 distinct primes ordered so that 
 .  If
.  If 
 for each
for each  and there is more than one
 and there is more than one  , then
, then  ,
a contradiction.  Thus some
,
a contradiction.  Thus some  is less than
 is less than  ,
which also contradicts our assumptions on
,
which also contradicts our assumptions on  .
.
  
sage: eratosthenes(50) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
William 2007-06-01