<s>
A	O
primality	B-Algorithm
test	I-Algorithm
is	O
an	O
algorithm	O
for	O
determining	O
whether	O
an	O
input	O
number	O
is	O
prime	O
.	O
</s>
<s>
Unlike	O
integer	O
factorization	O
,	O
primality	B-Algorithm
tests	I-Algorithm
do	O
not	O
generally	O
give	O
prime	O
factors	O
,	O
only	O
stating	O
whether	O
the	O
input	O
number	O
is	O
prime	O
or	O
not	O
.	O
</s>
<s>
Factorization	O
is	O
thought	O
to	O
be	O
a	O
computationally	O
difficult	O
problem	O
,	O
whereas	O
primality	B-Algorithm
testing	I-Algorithm
is	O
comparatively	O
easy	O
(	O
its	O
running	O
time	O
is	O
polynomial	O
in	O
the	O
size	O
of	O
the	O
input	O
)	O
.	O
</s>
<s>
Some	O
primality	B-Algorithm
tests	I-Algorithm
prove	O
that	O
a	O
number	O
is	O
prime	O
,	O
while	O
others	O
like	O
Miller	B-Algorithm
–	I-Algorithm
Rabin	I-Algorithm
prove	O
that	O
a	O
number	O
is	O
composite	O
.	O
</s>
<s>
Therefore	O
,	O
the	O
latter	O
might	O
more	O
accurately	O
be	O
called	O
compositeness	B-Algorithm
tests	I-Algorithm
instead	O
of	O
primality	B-Algorithm
tests	I-Algorithm
.	O
</s>
<s>
The	O
simplest	O
primality	B-Algorithm
test	I-Algorithm
is	O
trial	B-Algorithm
division	I-Algorithm
:	O
given	O
an	O
input	O
number	O
,	O
n	O
,	O
</s>
<s>
An	O
example	O
is	O
to	O
use	O
trial	B-Algorithm
division	I-Algorithm
to	O
test	O
the	O
primality	O
of	O
17	O
.	O
</s>
<s>
Generalising	O
further	O
,	O
all	O
primes	O
greater	O
than	O
c#	O
(	O
c	B-Language
primorial	O
)	O
are	O
of	O
the	O
form	O
c#	O
k	O
+	O
i	O
,	O
for	O
i	O
<	O
c#	O
,	O
where	O
c	B-Language
and	O
k	O
are	O
integers	O
and	O
i	O
represents	O
the	O
numbers	O
that	O
are	O
coprime	O
to	O
c#	O
.	O
</s>
<s>
In	O
order	O
to	O
match	O
the	O
previous	O
method	O
of	O
allowing	O
for	O
negative	O
i	O
,	O
instead	O
of	O
checking	O
each	O
i	O
from	O
1	O
to	O
c#	O
-1	O
(	O
because	O
0	O
and	O
c#	O
are	O
always	O
even	O
)	O
,	O
check	O
each	O
i	O
from	O
1	O
to	O
,	O
which	O
will	O
be	O
the	O
list	O
of	O
values	O
i	O
such	O
that	O
all	O
integers	O
are	O
of	O
the	O
form	O
.	O
</s>
<s>
Note	O
that	O
this	O
list	O
will	O
always	O
include	O
1	O
and	O
the	O
set	O
of	O
primes	O
greater	O
than	O
c	B-Language
but	O
smaller	O
than	O
.	O
</s>
<s>
For	O
example	O
,	O
437	O
is	O
of	O
the	O
form	O
of	O
c	B-Language
#k	O
+	O
i	O
for	O
c	B-Language
=	O
7	O
,	O
c#	O
=	O
210	O
,	O
k	O
=	O
2	O
,	O
i	O
=	O
17	O
.	O
</s>
<s>
For	O
this	O
method	O
,	O
it	O
is	O
also	O
necessary	O
to	O
check	O
for	O
divisibility	O
by	O
all	O
primes	O
that	O
are	O
less	O
than	O
c	B-Language
.	O
Observations	O
analogous	O
to	O
the	O
preceding	O
can	O
be	O
applied	O
recursively	O
,	O
giving	O
the	B-Algorithm
Sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
.	O
</s>
<s>
(	O
Such	O
a	O
list	O
can	O
be	O
computed	O
with	O
the	B-Algorithm
Sieve	I-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
or	O
by	O
an	O
algorithm	O
that	O
tests	O
each	O
incremental	O
m	O
against	O
all	O
known	O
primes	O
<	O
)	O
.	O
</s>
<s>
A	O
simple	O
but	O
very	O
inefficient	O
primality	B-Algorithm
test	I-Algorithm
uses	O
Wilson	O
's	O
theorem	O
,	O
which	O
states	O
that	O
p	O
is	O
prime	O
if	O
and	O
only	O
if	O
:	O
</s>
<s>
The	O
following	O
is	O
a	O
simple	O
primality	B-Algorithm
test	I-Algorithm
in	O
Python	B-Language
using	O
the	O
simple	O
optimization	O
mentioned	O
earlier	O
.	O
</s>
<s>
The	O
following	O
is	O
a	O
primality	B-Algorithm
test	I-Algorithm
in	O
the	O
C	B-Language
family	O
of	O
languages	O
using	O
the	O
same	O
optimization	O
as	O
above	O
.	O
</s>
<s>
The	O
following	O
is	O
a	O
primality	B-Algorithm
test	I-Algorithm
in	O
Java	B-Language
using	O
the	O
same	O
optimization	O
as	O
above	O
.	O
</s>
<s>
The	O
following	O
is	O
a	O
primality	B-Algorithm
test	I-Algorithm
in	O
JavaScript	B-Language
using	O
the	O
same	O
optimization	O
as	O
above	O
.	O
</s>
<s>
The	O
following	O
is	O
a	O
primality	B-Algorithm
test	I-Algorithm
in	O
R	B-Language
(	O
programming	O
language	O
)	O
using	O
the	O
same	O
optimization	O
as	O
above	O
.	O
</s>
<s>
The	O
following	O
is	O
a	O
primality	B-Algorithm
test	I-Algorithm
in	O
Dart	O
(	O
programming_language	O
)	O
using	O
the	O
same	O
optimization	O
as	O
above	O
.	O
</s>
<s>
The	O
following	O
is	O
a	O
primality	B-Algorithm
test	I-Algorithm
in	O
Free	B-Operating_System
Pascal	I-Operating_System
using	O
the	O
same	O
optimization	O
as	O
above	O
.	O
</s>
<s>
The	O
Fermat	B-Algorithm
test	I-Algorithm
and	O
the	O
Fibonacci	O
test	O
are	O
simple	O
examples	O
,	O
and	O
they	O
are	O
very	O
effective	O
when	O
combined	O
.	O
</s>
<s>
where	O
fk	O
is	O
the	O
k-th	O
Fibonacci	B-Algorithm
number	I-Algorithm
.	O
</s>
<s>
The	O
first	O
condition	O
is	O
the	O
Fermat	B-Algorithm
primality	I-Algorithm
test	I-Algorithm
using	O
base	O
2	O
.	O
</s>
<s>
f(x )	O
k	O
is	O
the	O
k-th	O
Fibonacci	B-Algorithm
polynomial	I-Algorithm
at	O
x	O
.	O
</s>
<s>
Probabilistic	B-General_Concept
tests	I-General_Concept
are	O
more	O
rigorous	O
than	O
heuristics	O
in	O
that	O
they	O
provide	O
provable	O
bounds	O
on	O
the	O
probability	O
of	O
being	O
fooled	O
by	O
a	O
composite	O
number	O
.	O
</s>
<s>
Many	O
popular	O
primality	B-Algorithm
tests	I-Algorithm
are	O
probabilistic	B-General_Concept
tests	I-General_Concept
.	O
</s>
<s>
These	O
tests	O
use	O
,	O
apart	O
from	O
the	O
tested	O
number	O
n	O
,	O
some	O
other	O
numbers	O
a	O
which	O
are	O
chosen	O
at	O
random	O
from	O
some	O
sample	O
space	O
;	O
the	O
usual	O
randomized	O
primality	B-Algorithm
tests	I-Algorithm
never	O
report	O
a	O
prime	O
number	O
as	O
composite	O
,	O
but	O
it	O
is	O
possible	O
for	O
a	O
composite	O
number	O
to	O
be	O
reported	O
as	O
prime	O
.	O
</s>
<s>
The	O
basic	O
structure	O
of	O
randomized	O
primality	B-Algorithm
tests	I-Algorithm
is	O
as	O
follows	O
:	O
</s>
<s>
After	O
one	O
or	O
more	O
iterations	O
,	O
if	O
n	O
is	O
not	O
found	O
to	O
be	O
a	O
composite	O
number	O
,	O
then	O
it	O
can	O
be	O
declared	O
probably	B-Algorithm
prime	I-Algorithm
.	O
</s>
<s>
The	O
simplest	O
probabilistic	O
primality	B-Algorithm
test	I-Algorithm
is	O
the	O
Fermat	B-Algorithm
primality	I-Algorithm
test	I-Algorithm
(	O
actually	O
a	O
compositeness	B-Algorithm
test	I-Algorithm
)	O
.	O
</s>
<s>
pseudoprime	B-Algorithm
to	O
base	O
a	O
.	O
</s>
<s>
There	O
are	O
only	O
21853	O
pseudoprimes	B-Algorithm
base	O
2	O
that	O
are	O
less	O
than	O
2.5	O
(	O
see	O
page	O
1005	O
of	O
)	O
.	O
</s>
<s>
This	O
means	O
that	O
,	O
for	O
n	O
up	O
to	O
2.5	O
,	O
if	O
2n−1	O
(	O
modulo	O
n	O
)	O
equals	O
1	O
,	O
then	O
n	O
is	O
prime	O
,	O
unless	O
n	O
is	O
one	O
of	O
these	O
21853	O
pseudoprimes	B-Algorithm
.	O
</s>
<s>
Nevertheless	O
,	O
the	O
Fermat	B-Algorithm
test	I-Algorithm
is	O
often	O
used	O
if	O
a	O
rapid	O
screening	O
of	O
numbers	O
is	O
needed	O
,	O
for	O
instance	O
in	O
the	O
key	O
generation	O
phase	O
of	O
the	O
RSA	B-Architecture
public	I-Architecture
key	I-Architecture
cryptographic	I-Architecture
algorithm	I-Architecture
.	O
</s>
<s>
The	O
Miller	B-Algorithm
–	I-Algorithm
Rabin	I-Algorithm
primality	I-Algorithm
test	I-Algorithm
and	O
Solovay	O
–	O
Strassen	O
primality	B-Algorithm
test	I-Algorithm
are	O
more	O
sophisticated	O
variants	O
,	O
which	O
detect	O
all	O
composites	O
(	O
once	O
again	O
,	O
this	O
means	O
:	O
for	O
every	O
composite	O
number	O
n	O
,	O
at	O
least	O
3/4	O
(	O
Miller	B-Algorithm
–	I-Algorithm
Rabin	I-Algorithm
)	O
or	O
1/2	O
(	O
Solovay	O
–	O
Strassen	O
)	O
of	O
numbers	O
a	O
are	O
witnesses	O
of	O
compositeness	O
of	O
n	O
)	O
.	O
</s>
<s>
These	O
are	O
also	O
compositeness	B-Algorithm
tests	I-Algorithm
.	O
</s>
<s>
The	O
Miller	B-Algorithm
–	I-Algorithm
Rabin	I-Algorithm
primality	I-Algorithm
test	I-Algorithm
works	O
as	O
follows	O
:	O
</s>
<s>
The	O
Miller	B-Algorithm
–	I-Algorithm
Rabin	I-Algorithm
test	I-Algorithm
is	O
a	O
strong	B-Algorithm
probable	I-Algorithm
prime	I-Algorithm
test	O
(	O
see	O
PSW	O
page	O
1004	O
)	O
.	O
</s>
<s>
The	O
Solovay	O
–	O
Strassen	O
test	O
is	O
an	O
Euler	B-Algorithm
probable	I-Algorithm
prime	I-Algorithm
test	O
(	O
see	O
PSW	O
page	O
1003	O
)	O
.	O
</s>
<s>
For	O
each	O
individual	O
value	O
of	O
a	O
,	O
the	O
Solovay	O
–	O
Strassen	O
test	O
is	O
weaker	O
than	O
the	O
Miller	B-Algorithm
–	I-Algorithm
Rabin	I-Algorithm
test	I-Algorithm
.	O
</s>
<s>
For	O
example	O
,	O
if	O
n	O
=	O
1905	O
and	O
a	O
=	O
2	O
,	O
then	O
the	O
Miller-Rabin	B-Algorithm
test	I-Algorithm
shows	O
that	O
n	O
is	O
composite	O
,	O
but	O
the	O
Solovay	O
–	O
Strassen	O
test	O
does	O
not	O
.	O
</s>
<s>
pseudoprime	B-Algorithm
base	O
2	O
but	O
not	O
a	O
strong	B-Algorithm
pseudoprime	I-Algorithm
base	O
2	O
(	O
this	O
is	O
illustrated	O
in	O
Figure	O
1	O
of	O
PSW	O
)	O
.	O
</s>
<s>
The	O
Miller	B-Algorithm
–	I-Algorithm
Rabin	I-Algorithm
and	O
the	O
Solovay	O
–	O
Strassen	O
primality	B-Algorithm
tests	I-Algorithm
are	O
simple	O
and	O
are	O
much	O
faster	O
than	O
other	O
general	O
primality	B-Algorithm
tests	I-Algorithm
.	O
</s>
<s>
One	O
method	O
of	O
improving	O
efficiency	O
further	O
in	O
some	O
cases	O
is	O
the	O
Frobenius	B-Algorithm
pseudoprimality	I-Algorithm
test	I-Algorithm
;	O
a	O
round	O
of	O
this	O
test	O
takes	O
about	O
three	O
times	O
as	O
long	O
as	O
a	O
round	O
of	O
Miller	B-Algorithm
–	I-Algorithm
Rabin	I-Algorithm
,	O
but	O
achieves	O
a	O
probability	O
bound	O
comparable	O
to	O
seven	O
rounds	O
of	O
Miller	B-Algorithm
–	I-Algorithm
Rabin	I-Algorithm
.	O
</s>
<s>
The	O
Frobenius	O
test	O
is	O
a	O
generalization	O
of	O
the	O
Lucas	B-Algorithm
probable	I-Algorithm
prime	I-Algorithm
test	O
.	O
</s>
<s>
The	O
Baillie	B-Algorithm
–	I-Algorithm
PSW	I-Algorithm
primality	I-Algorithm
test	I-Algorithm
is	O
a	O
probabilistic	O
primality	B-Algorithm
test	I-Algorithm
that	O
combines	O
a	O
Fermat	O
or	O
Miller	B-Algorithm
–	I-Algorithm
Rabin	I-Algorithm
test	I-Algorithm
with	O
a	O
Lucas	B-Algorithm
probable	I-Algorithm
prime	I-Algorithm
test	O
to	O
get	O
a	O
primality	B-Algorithm
test	I-Algorithm
that	O
has	O
no	O
known	O
counterexamples	O
.	O
</s>
<s>
That	O
is	O
,	O
there	O
are	O
no	O
known	O
composite	O
n	O
for	O
which	O
this	O
test	O
reports	O
that	O
n	O
is	O
probably	B-Algorithm
prime	I-Algorithm
.	O
</s>
<s>
Leonard	O
Adleman	O
and	O
Ming-Deh	O
Huang	O
presented	O
an	O
errorless	O
(	O
but	O
expected	O
polynomial-time	O
)	O
variant	O
of	O
the	O
elliptic	B-Algorithm
curve	I-Algorithm
primality	I-Algorithm
test	I-Algorithm
.	O
</s>
<s>
Unlike	O
the	O
other	O
probabilistic	B-General_Concept
tests	I-General_Concept
,	O
this	O
algorithm	O
produces	O
a	O
primality	B-Algorithm
certificate	I-Algorithm
,	O
and	O
thus	O
can	O
be	O
used	O
to	O
prove	O
that	O
a	O
number	O
is	O
prime	O
.	O
</s>
<s>
If	O
quantum	B-Architecture
computers	I-Architecture
were	O
available	O
,	O
primality	O
could	O
be	O
tested	O
asymptotically	O
faster	O
than	O
by	O
using	O
classical	O
computers	O
.	O
</s>
<s>
A	O
combination	O
of	O
Shor	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
,	O
an	O
integer	O
factorization	O
method	O
,	O
with	O
the	O
Pocklington	B-Algorithm
primality	I-Algorithm
test	I-Algorithm
could	O
solve	O
the	O
problem	O
in	O
.	O
</s>
<s>
This	O
resulted	O
in	O
the	O
Pocklington	B-Algorithm
primality	I-Algorithm
test	I-Algorithm
.	O
</s>
<s>
The	O
first	O
deterministic	B-General_Concept
primality	B-Algorithm
test	I-Algorithm
significantly	O
faster	O
than	O
the	O
naive	O
methods	O
was	O
the	O
cyclotomy	B-Algorithm
test	I-Algorithm
;	O
its	O
runtime	O
can	O
be	O
proven	O
to	O
be	O
O((logn )	O
c	B-Language
log	O
log	O
log	O
n	O
)	O
,	O
where	O
n	O
is	O
the	O
number	O
to	O
test	O
for	O
primality	O
and	O
c	B-Language
is	O
a	O
constant	O
independent	O
of	O
n	O
.	O
Many	O
further	O
improvements	O
were	O
made	O
,	O
but	O
none	O
could	O
be	O
proven	O
to	O
have	O
polynomial	O
running	O
time	O
.	O
</s>
<s>
(	O
Note	O
that	O
running	O
time	O
is	O
measured	O
in	O
terms	O
of	O
the	O
size	O
of	O
the	O
input	O
,	O
which	O
in	O
this	O
case	O
is	O
~	O
logn	O
,	O
that	O
being	O
the	O
number	O
of	O
bits	O
needed	O
to	O
represent	O
the	O
number	O
n	O
.	O
)	O
The	O
elliptic	B-Algorithm
curve	I-Algorithm
primality	I-Algorithm
test	I-Algorithm
can	O
be	O
proven	O
to	O
run	O
in	O
O((logn )	O
6	O
)	O
,	O
if	O
some	O
conjectures	O
on	O
analytic	O
number	O
theory	O
are	O
true	O
.	O
</s>
<s>
Similarly	O
,	O
under	O
the	O
generalized	O
Riemann	O
hypothesis	O
,	O
the	O
deterministic	B-General_Concept
Miller	O
's	O
test	O
,	O
which	O
forms	O
the	O
basis	O
of	O
the	O
probabilistic	O
Miller	B-Algorithm
–	I-Algorithm
Rabin	I-Algorithm
test	I-Algorithm
,	O
can	O
be	O
proved	O
to	O
run	O
in	O
Õ((logn )	O
4	O
)	O
.	O
</s>
<s>
In	O
2002	O
,	O
the	O
first	O
provably	O
unconditional	O
deterministic	B-General_Concept
polynomial	O
time	O
test	O
for	O
primality	O
was	O
invented	O
by	O
Manindra	O
Agrawal	O
,	O
Neeraj	O
Kayal	O
,	O
and	O
Nitin	O
Saxena	O
.	O
</s>
<s>
The	O
AKS	B-Algorithm
primality	I-Algorithm
test	I-Algorithm
runs	O
in	O
Õ((logn )	O
12	O
)	O
(	O
improved	O
to	O
Õ((logn )	O
7.5	O
)	O
in	O
the	O
published	O
revision	O
of	O
their	O
paper	O
)	O
,	O
which	O
can	O
be	O
further	O
reduced	O
to	O
Õ((logn )	O
6	O
)	O
if	O
the	O
Sophie	O
Germain	O
conjecture	O
is	O
true	O
.	O
</s>
<s>
See	O
primality	B-Algorithm
certificate	I-Algorithm
for	O
details	O
.	O
</s>
<s>
The	O
subsequent	O
discovery	O
of	O
the	O
Solovay	O
–	O
Strassen	O
and	O
Miller	B-Algorithm
–	I-Algorithm
Rabin	I-Algorithm
algorithms	O
put	O
PRIMES	O
in	O
coRP	O
.	O
</s>
<s>
The	O
Adleman	B-Algorithm
–	I-Algorithm
Pomerance	I-Algorithm
–	I-Algorithm
Rumely	I-Algorithm
primality	I-Algorithm
test	I-Algorithm
from	O
1983	O
put	O
PRIMES	O
in	O
QP	O
(	O
quasi-polynomial	O
time	O
)	O
,	O
which	O
is	O
not	O
known	O
to	O
be	O
comparable	O
with	O
the	O
classes	O
mentioned	O
above	O
.	O
</s>
<s>
The	O
existence	O
of	O
the	O
AKS	B-Algorithm
primality	I-Algorithm
test	I-Algorithm
finally	O
settled	O
this	O
long-standing	O
question	O
and	O
placed	O
PRIMES	O
in	O
P	O
.	O
However	O
,	O
PRIMES	O
is	O
not	O
known	O
to	O
be	O
P-complete	O
,	O
and	O
it	O
is	O
not	O
known	O
whether	O
it	O
lies	O
in	O
classes	O
lying	O
inside	O
P	O
such	O
as	O
NC	O
or	O
L	O
.	O
It	O
is	O
known	O
that	O
PRIMES	O
is	O
not	O
in	O
AC0	O
.	O
</s>
<s>
Certain	O
number-theoretic	O
methods	O
exist	O
for	O
testing	O
whether	O
a	O
number	O
is	O
prime	O
,	O
such	O
as	O
the	O
Lucas	B-Algorithm
test	I-Algorithm
and	O
Proth	B-Algorithm
's	I-Algorithm
test	I-Algorithm
.	O
</s>
<s>
These	O
tests	O
typically	O
require	O
factorization	O
of	O
n+1	O
,	O
n	O
−	O
1	O
,	O
or	O
a	O
similar	O
quantity	O
,	O
which	O
means	O
that	O
they	O
are	O
not	O
useful	O
for	O
general-purpose	O
primality	B-Algorithm
testing	I-Algorithm
,	O
but	O
they	O
are	O
often	O
quite	O
powerful	O
when	O
the	O
tested	O
number	O
n	O
is	O
known	O
to	O
have	O
a	O
special	O
form	O
.	O
</s>
<s>
The	O
Lucas	B-Algorithm
test	I-Algorithm
relies	O
on	O
the	O
fact	O
that	O
the	O
multiplicative	O
order	O
of	O
a	O
number	O
a	O
modulo	O
n	O
is	O
n	O
−	O
1	O
for	O
a	O
prime	O
n	O
when	O
a	O
is	O
a	O
primitive	O
root	O
modulo	O
n	O
.	O
If	O
we	O
can	O
show	O
a	O
is	O
primitive	O
for	O
n	O
,	O
we	O
can	O
show	O
n	O
is	O
prime	O
.	O
</s>
