What is the most speed efficient primality test available

771 Views Asked by At

I am trying to find out if a number is prime on a computer but I am only try a very few and far apart numbers so don't want to use a sieve. I am quite good at maths but am only doing my GCSEs so can you please explain the answers clearly. I am looking for the most time efficient primality test algorithm as my numbers quickly get to sizes like 2^2^20 which needs a very efficient primality test. I am really looking for a polynomial time algorithm.

1

There are 1 best solutions below

0
On

For numbers of ~315k digits, they either need to be of a special form (e.g. Proth, LLR, Mersenne, etc.) that allows a fast proof, or you will have to settle for a PRP test. The record for general form proofs is about 35k digits, so 315k digits is far beyond today's practical limit. ECPP is polynomial time in practice, and we don't know anything faster for general-form proofs.

Re PRP tests, your choices include Fermat, Euler (Jacobi), Euler (Plumb base 2), Miller-Rabin, BPSW, Frobenius and its variants, and more. All polynomial in the size of the input. Of course for speed you want some simple pre-tests such as trial division for small factors (perhaps implemented as a gcd with a large primorial, but it's the same thing), but after that you really need to run one of the tests.

Also critical at this size is the implementation. In a test I did a few years ago with a 140k digit PRP, PFGW's Fermat test took under 6 minutes, GMP took 30 minutes, and Pari/GP took 63 minutes. PFGW uses gwnum which is highly optimized for this operation on large numbers, while the GMP library is massively more portable, much easier to use in implementations, and is faster for smaller inputs (under ~2000 digits). Using something other than these is probably going to be really, really slow.

The good news (maybe) is that it took only 6 minutes to run a Fermat test on a single 140k number. 315k digits should be reasonably practical. You could also run a few numbers in parallel if desired. Once you find something that passes, at this size it's very likely to be prime, and running a more stringent test like BPSW for more certainty should finish under a day.

Realistically at this size, just use PFGW. Once you found a candidate, you can try more bases or apply more stringent tests using various other tools. If you have a range you're looking in, you want to use a sieve of some sort to efficiently remove small divisors.