Primalilty tests
by iarnab_kundu, Jan 13, 2014, 4:31 PM
There are numerous primality tests that are eminently efficient. One of them is the recent Agrawal–Kayal–Saxena primality test. But our main focus is on one of the most ancient test, a.k.a Pseudoprimality testing.
Algorithm- Take a positive integer
. Take another positive integer
. Compute
. If the remnant value is
we say it's prime. Otherwise not.
Take an integer
. Take
. We know that by Fermat's Little Theorem,
, if
is an odd prime. Is the converse true? Let's check for some values.
it's true. So it seems true. Right?
Bazinga! It turns that it fails for
.
Let's take another
, let it equal
. Again we see there is a number passing through a loophole.
These numbers like
which contradict the algorithm for
are now called as pseudoprime base
. Similarly there exists numbers pseudoprime base
.
Now the question arises, are there finitely many of them? Unfortunately there are infinitely many so for any base
. As a matter of fact if
is a psedoprime base
then
.
If we see the pseudoprime tables then we would notice that there are not much intersection in between. So can we check for a large number of bases and if that they turn out to be "prime" for each case then that implies that the number is prime? Again there are Carmichael numbers which are pseudoprimes in any base.
Thus this algorithm is obliterated.
Let's try to improve this one.
Algorithm- Take a positive integer
. Take another positive integer
. Compute
. If the remnant value is not
we declare that its composite. Otherwise run the loop for all
. If all these bases say that they are prime. Then it is!
Indeed if its not a prime then there is a factor of it. This factor exponentiated would render it a composite. But this test is a sheer waste of time.
Let's turn our heads towards Miller-Rabin randomized primality test.
Take an odd positive integer
and a base
.
Definition- The integer
is called "witness" if
either there exists
such that
but 
or
.
Now the test.
Algorithm- Take an integer
as usual. Take a random number
. If
is a witness, terminate the process and print that
is a composite number. Otherwise iterate the loop
times.
It is easy to note that if we get by the test the number is composite then it is surely a composite. But unfortunately this code sometimes says a number is prime but it is not so!
It is claimed that there are atleast
integers
such that
is a witness. Hence the probability that the test prints a prime is atmost
.
It can be shown by probabilistic arguments that the odds that the tests returns a prime and it indeed is a prime is atleast
.
Hence iterating the loop
times and for any integer
in
-bits, is the test is positive then we may be
% sure that it is indeed a prime. And so we establish an efficient algorithm.
I shall elaborate on the efficacy of the code in a later blog post, and shall prove that the well above
.
For the C++ implementation of the algorithm. Check this one. http://paste.ubuntu.com/6760800/
(It works for numbers less that
)
References- Algorithms by Cormen, Leiserson, Rivest and Stein
Algorithm- Take a positive integer




Take an integer





Bazinga! It turns that it fails for

Let's take another


These numbers like




Now the question arises, are there finitely many of them? Unfortunately there are infinitely many so for any base




If we see the pseudoprime tables then we would notice that there are not much intersection in between. So can we check for a large number of bases and if that they turn out to be "prime" for each case then that implies that the number is prime? Again there are Carmichael numbers which are pseudoprimes in any base.
Thus this algorithm is obliterated.
Let's try to improve this one.
Algorithm- Take a positive integer





Indeed if its not a prime then there is a factor of it. This factor exponentiated would render it a composite. But this test is a sheer waste of time.
Let's turn our heads towards Miller-Rabin randomized primality test.
Take an odd positive integer


Definition- The integer







Now the test.
Algorithm- Take an integer





It is easy to note that if we get by the test the number is composite then it is surely a composite. But unfortunately this code sometimes says a number is prime but it is not so!
It is claimed that there are atleast




It can be shown by probabilistic arguments that the odds that the tests returns a prime and it indeed is a prime is atleast

Hence iterating the loop




I shall elaborate on the efficacy of the code in a later blog post, and shall prove that the well above

For the C++ implementation of the algorithm. Check this one. http://paste.ubuntu.com/6760800/
(It works for numbers less that

References- Algorithms by Cormen, Leiserson, Rivest and Stein
This post has been edited 4 times. Last edited by iarnab_kundu, Jan 16, 2014, 7:47 AM