Skip to main content

Primality Tests

Naive

Given an integer , we can check numbers up to to find if a number divides . If there is such a number, is composite. If not, is a prime. This gives the solution in .

Miller Rabin Test (deterministic ver.)

Now we know how to generate prime numbers fast. How about primality testing?

Naive solutions first. Given an integer , we can check numbers up to to find if a number divides . If there is such a number, is composite. If not, is a prime. This gives the solution in .

Here's a "solution" in using the fast exponentiation we will talk about in the next section. It's called Miller-Rabin Primality Test. I'll introduce the deterministic version. We probably (hopefully) won't see stuff like in contests.

Here's the sketch of the algorithm. Choose some set of . We will run the algorithm with different s, and the more s we run this algorithm with, the more accurate this primality test is going to be.

Decompose as . Then check if the following holds. If there is an that satisfies this, is composite. If not, is a prime.

List for sufficient checks (deterministic)