<s>
In	O
mathematics	O
,	O
the	B-Algorithm
sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
is	O
an	O
ancient	O
algorithm	O
for	O
finding	O
all	O
prime	O
numbers	O
up	O
to	O
any	O
given	O
limit	O
.	O
</s>
<s>
This	O
is	O
the	O
sieve	O
's	O
key	O
distinction	O
from	O
using	O
trial	B-Algorithm
division	I-Algorithm
to	O
sequentially	O
test	O
each	O
candidate	O
number	O
for	O
divisibility	O
by	O
each	O
prime	O
.	O
</s>
<s>
This	O
can	O
be	O
generalized	O
with	O
wheel	B-Algorithm
factorization	I-Algorithm
,	O
forming	O
the	O
initial	O
list	O
only	O
from	O
numbers	O
coprime	O
with	O
the	O
first	O
few	O
primes	O
and	O
not	O
just	O
from	O
odds	O
(	O
i.e.	O
,	O
numbers	O
coprime	O
with	O
2	O
)	O
,	O
and	O
counting	O
in	O
the	O
correspondingly	O
adjusted	O
increments	O
so	O
that	O
only	O
such	O
multiples	O
of	O
are	O
generated	O
that	O
are	O
coprime	O
with	O
those	O
small	O
primes	O
,	O
in	O
the	O
first	O
place	O
.	O
</s>
<s>
The	B-Algorithm
sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
can	O
be	O
expressed	O
in	O
pseudocode	B-Language
,	O
as	O
follows	O
:	O
</s>
<s>
The	O
time	O
complexity	B-General_Concept
of	O
this	O
algorithm	O
is	O
,	O
provided	O
the	O
array	O
update	O
is	O
an	O
operation	O
,	O
as	O
is	O
usually	O
the	O
case	O
.	O
</s>
<s>
As	O
Sorenson	O
notes	O
,	O
the	O
problem	O
with	O
the	B-Algorithm
sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
is	O
not	O
the	O
number	O
of	O
operations	O
it	O
performs	O
but	O
rather	O
its	O
memory	O
requirements	O
.	O
</s>
<s>
For	O
large	O
,	O
the	O
range	O
of	O
primes	O
may	O
not	O
fit	O
in	O
memory	O
;	O
worse	O
,	O
even	O
for	O
moderate	O
,	O
its	O
cache	B-General_Concept
use	O
is	O
highly	O
suboptimal	O
.	O
</s>
<s>
The	O
algorithm	O
walks	O
through	O
the	O
entire	O
array	O
,	O
exhibiting	O
almost	O
no	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
.	O
</s>
<s>
If	O
is	O
chosen	O
to	O
be	O
,	O
the	O
space	O
complexity	B-General_Concept
of	O
the	O
algorithm	O
is	O
,	O
while	O
the	O
time	O
complexity	B-General_Concept
is	O
the	O
same	O
as	O
that	O
of	O
the	O
regular	O
sieve	O
.	O
</s>
<s>
For	O
ranges	O
with	O
upper	O
limit	O
so	O
large	O
that	O
the	O
sieving	O
primes	O
below	O
as	O
required	O
by	O
the	O
page	O
segmented	O
sieve	B-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
cannot	O
fit	O
in	O
memory	O
,	O
a	O
slower	O
but	O
much	O
more	O
space-efficient	O
sieve	O
like	O
the	O
sieve	O
of	O
Sorenson	O
can	O
be	O
used	O
instead	O
.	O
</s>
<s>
using	O
list	B-Language
comprehension	I-Language
notation	O
with	O
\	O
denoting	O
set	O
subtraction	O
of	O
arithmetic	O
progressions	O
of	O
numbers	O
.	O
</s>
<s>
Primes	O
can	O
also	O
be	O
produced	O
by	O
iteratively	O
sieving	O
out	O
the	O
composites	O
through	O
divisibility	B-Algorithm
testing	I-Algorithm
by	O
sequential	O
primes	O
,	O
one	O
prime	O
at	O
a	O
time	O
.	O
</s>
<s>
It	O
is	O
not	O
the	B-Algorithm
sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
but	O
is	O
often	O
confused	O
with	O
it	O
,	O
even	O
though	O
the	B-Algorithm
sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
directly	O
generates	O
the	O
composites	O
instead	O
of	O
testing	O
for	O
them	O
.	O
</s>
<s>
Trial	B-Algorithm
division	I-Algorithm
has	O
worse	O
theoretical	O
complexity	B-General_Concept
than	O
that	O
of	O
the	B-Algorithm
sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
in	O
generating	O
ranges	O
of	O
primes	O
.	O
</s>
<s>
When	O
testing	O
each	O
prime	O
,	O
the	O
optimal	O
trial	B-Algorithm
division	I-Algorithm
algorithm	O
uses	O
all	O
prime	O
numbers	O
not	O
exceeding	O
its	O
square	O
root	O
,	O
whereas	O
the	B-Algorithm
sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
produces	O
each	O
composite	O
from	O
its	O
prime	O
factors	O
only	O
,	O
and	O
gets	O
the	O
primes	O
"	O
for	O
free	O
"	O
,	O
between	O
the	O
composites	O
.	O
</s>
<s>
The	O
widely	O
known	O
1975	O
functional	B-Language
sieve	O
code	O
by	O
David	O
Turner	O
is	O
often	O
presented	O
as	O
an	O
example	O
of	O
the	B-Algorithm
sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
but	O
is	O
actually	O
a	O
sub-optimal	O
trial	B-Algorithm
division	I-Algorithm
sieve	O
.	O
</s>
<s>
The	B-Algorithm
sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
is	O
a	O
popular	O
way	O
to	O
benchmark	O
computer	O
performance	O
.	O
</s>
<s>
The	O
time	O
complexity	B-General_Concept
of	O
calculating	O
all	O
primes	O
below	O
in	O
the	O
random	B-Application
access	I-Application
machine	I-Application
model	O
is	O
operations	O
,	O
a	O
direct	O
consequence	O
of	O
the	O
fact	O
that	O
the	O
prime	O
harmonic	O
series	O
asymptotically	O
approaches	O
.	O
</s>
<s>
It	O
has	O
an	O
exponential	O
time	O
complexity	B-General_Concept
with	O
regard	O
to	O
input	O
size	O
,	O
though	O
,	O
which	O
makes	O
it	O
a	O
pseudo-polynomial	B-Algorithm
algorithm	O
.	O
</s>
<s>
The	O
bit	O
complexity	B-General_Concept
of	O
the	O
algorithm	O
is	O
bit	O
operations	O
with	O
a	O
memory	O
requirement	O
of	O
.	O
</s>
<s>
The	O
normally	O
implemented	O
page	O
segmented	O
version	O
has	O
the	O
same	O
operational	O
complexity	B-General_Concept
of	O
as	O
the	O
non-segmented	O
version	O
but	O
reduces	O
the	O
space	O
requirements	O
to	O
the	O
very	O
minimal	O
size	O
of	O
the	O
segment	O
page	O
plus	O
the	O
memory	O
required	O
to	O
store	O
the	O
base	O
primes	O
less	O
than	O
the	O
square	O
root	O
of	O
the	O
range	O
used	O
to	O
cull	O
composites	O
from	O
successive	O
page	O
segments	O
of	O
size	O
.	O
</s>
<s>
A	O
special	O
(	O
rarely	O
,	O
if	O
ever	O
,	O
implemented	O
)	O
segmented	O
version	O
of	O
the	B-Algorithm
sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
,	O
with	O
basic	O
optimizations	O
,	O
uses	O
operations	O
and	O
bits	O
of	O
memory	O
.	O
</s>
<s>
Using	O
big	O
O	O
notation	O
ignores	O
constant	O
factors	O
and	O
offsets	O
that	O
may	O
be	O
very	O
significant	O
for	O
practical	O
ranges	O
:	O
The	B-Algorithm
sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
variation	O
known	O
as	O
the	O
Pritchard	O
wheel	O
sieve	O
has	O
an	O
performance	O
,	O
but	O
its	O
basic	O
implementation	O
requires	O
either	O
a	O
"	O
one	O
large	O
array	O
"	O
algorithm	O
which	O
limits	O
its	O
usable	O
range	O
to	O
the	O
amount	O
of	O
available	O
memory	O
else	O
it	O
needs	O
to	O
be	O
page	O
segmented	O
to	O
reduce	O
memory	O
use	O
.	O
</s>
<s>
When	O
implemented	O
with	O
page	O
segmentation	O
in	O
order	O
to	O
save	O
memory	O
,	O
the	O
basic	O
algorithm	O
still	O
requires	O
about	O
bits	O
of	O
memory	O
(	O
much	O
more	O
than	O
the	O
requirement	O
of	O
the	O
basic	O
page	O
segmented	O
sieve	B-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
using	O
bits	O
of	O
memory	O
)	O
.	O
</s>
<s>
Although	O
the	O
resulting	O
wheel	O
sieve	O
has	O
performance	O
and	O
an	O
acceptable	O
memory	O
requirement	O
,	O
it	O
is	O
not	O
faster	O
than	O
a	O
reasonably	O
Wheel	O
Factorized	O
basic	O
sieve	B-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
for	O
practical	O
sieving	O
ranges	O
.	O
</s>
<s>
Euler	O
's	O
proof	O
of	O
the	O
zeta	O
product	O
formula	O
contains	O
a	O
version	O
of	O
the	B-Algorithm
sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
in	O
which	O
each	O
composite	O
number	O
is	O
eliminated	O
exactly	O
once	O
.	O
</s>
<s>
wheel	B-Algorithm
factorization	I-Algorithm
)	O
,	O
so	O
that	O
the	O
list	O
will	O
start	O
with	O
the	O
next	O
prime	O
,	O
and	O
all	O
the	O
numbers	O
in	O
it	O
below	O
the	O
square	O
of	O
its	O
first	O
element	O
will	O
be	O
prime	O
too	O
.	O
</s>
