Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple deterministic primality testing for small numbers

I am aware that there are a number of primality testing algorithms used in practice (Sieve of Eratosthenes, Fermat's test, Miller-Rabin, AKS, etc). However, they are either slow (e.g. sieve), probabalistic (Fermat and Miller-Rabin), or relatively difficult to implement (AKS).

What is the best deterministic solution to determine whether or not a number is prime?

Note that I am primarily (pun intended) interested in testing against numbers on the order of 32 (and maybe 64) bits. So a robust solution (applicable to larger numbers) is not necessary.

like image 223
tskuzzy Avatar asked Sep 29 '11 08:09

tskuzzy


People also ask

What is a deterministic primality test?

A primality test is deterministic if it outputs True when the number is a prime and False when the input is composite with probability 1. Otherwise, the primality test is probabilistic. A probabilistic primality test is often called a pseudoprimality test.

What is the best primality test?

For large integers, the most efficient primality tests are pro- babilistic. However, for integers with a small fixed number of bits the best tests in practice are deterministic. Currently the best known tests of this type involve 3 rounds of the Miller-Rabin test for 32-bit integers and 7 rounds for 64-bit integers.

Which algorithm used for primality testing?

The Wolfram Language implements the multiple Rabin-Miller test in bases 2 and 3 combined with a Lucas pseudoprime test as the primality test used by the function PrimeQ[n]. Like many such algorithms, it is a probabilistic test using pseudoprimes.

What is the fastest known deterministic method in the world for primality testing?

The fastest proof methods for this size would be APR-CL (e.g. mpz_aprcl) and ECPP (e.g. Primo or ecpp-dj).


1 Answers

Up to ~2^30 you could brute force with trial-division.

Up to 3.4*10^14, Rabin-Miller with the first 7 primes has been proven to be deterministic.

Above that, you're on your own. There's no known sub-cubic deterministic algorithm.

EDIT : I remembered this, but I didn't find the reference until now:

http://reference.wolfram.com/legacy/v5_2/book/section-A.9.4

PrimeQ first tests for divisibility using small primes, then uses the Miller-Rabin strong pseudoprime test base 2 and base 3, and then uses a Lucas test.

As of 1997, this procedure is known to be correct only for n < 10^16, and it is conceivable that for larger n it could claim a composite number to be prime.

So if you implement Rabin-Miller and Lucas, you're good up to 10^16.

like image 153
Mysticial Avatar answered Oct 07 '22 03:10

Mysticial