Skip to content

Finding Primes

One of the most efficient ways to find smaller primes is to use a sieve. There are several different sieves but one of the easiest to code is the sieve of Eratosthenes.

Here is a simple example:

This starts with primes less than 10 and then finds primes between 10 and 100 by finding odd numbers that are not multiples of 2, 3, 5 or 7.

In order to test whether a number is prime, we only need to test whether it is a multiple of any prime up to the square root of the number since, if it has any integer factors, at least one of them must be at or below this limit.  We could hence extend the above approach by using each new list to find primes in a larger interval – primes up to 10 allow the list to be extended to 100, primes up to 100 provide for extension up to 10,000 and so on.

But we really need a more general solution, which the following subroutine provides:

This is quite fast for an interpreted language such as Python but it uses a lot of memory when large primes are needed.An alternative that consumes less memory uses a Python generator:

We can time these two approaches using the following example which counts the number of primes below 1,000,000:

This gives the output:

which shows that the prime list function is over ten times faster than the generator solution.

Nevertheless, the generator uses less memory and offers a lot of flexibility as can be seen in its use with a filter function such as:

Here a list of digits in the input number is created and the function f is applied to this list.  We can now define a property we want for the digits of a prime using the function f (which can be a lambda function, an ordinary function written in a compact form).

For example, suppose we want to list the primes below 1000 with repeated digits. This does the job in a single line of code:

In a similar way we can easily find primes whose digits sum to 32:

So far we have considered only primes where it is feasible to hold lists of primes in memory. But when numbers are very large, with hundreds or thousands of digits, it becomes impractical to find primes in this way.   Although there are ways of determining whether a very large number is prime, in practice they are very slow. Fortunately, however, there are tests that can determine whether a given number is almost certain to be prime.  Such tests are often known as probable prime tests but they would be more accurately called composite tests because they can determine with certainty that a number is composite (i.e. it is not a prime).

One such test is known as the Miller-Rabin test, which is as follows in Python:

The first parameter is the number to test, the second parameter being the number of times the test is to be repeated (with a default of 10). Each time the test is run, the value returned will be False if the number is composite and True if the number is possibly prime.

For the Miller-Rabin test it is known that the probability of a test falsely indicating a prime is not higher than 1/4. Moreover, tests are independent of each other when run with different internal random numbers, which means that running N tests reduces the chance of finding a false prime to 1 in 4^N.  So, if 10 tests all indicate that a value is prime, the probability that it is actually composite will be lower than 1 in 1,000,000. So, for example, if we want to find the largest primes below the powers of 10 between 10 and 30, we can use:

In practice this test is almost always far better than its theoretical probability suggests and running it 20 times will make the probability of falsely reporting a composite as a prime far lower than the probability that a random machine memory error will cause such a false report.