The (asymptotically) most efficient deterministic primality testing algorithm is due to Lenstra and Pomerance, running in time $\tilde{O}(\log^6 n)$. If you believe the Extended Riemann Hypothesis, then Miller's algorithm runs in time $\tilde{O}(\log^4 n)$. There are many other deterministic primality testing algorithms, for example Miller's paper has an $\tilde{O}(n^{1/7})$ algorithm, and another well-known algorithm is Adleman–Pomerance–Rumley, running in time $O(\log n^{O(\log\log\log n)})$.
In reality, no one uses these algorithms, since they are too slow. Instead, probabilistic primality testing algorithms are used, mainly Miller–Rabin, which is a modification of Miller's algorithm mentioned above (another important algorithm is Solovay–Strassen). Each iteration of Miller–Rabin runs in time $\tilde{O}(\log^2 n)$, and so for a constant error probability (say $2^{-80}$) the entire algorithm runs in time $\tilde{O}(\log^2 n)$, which is much faster than Lenstra–Pomerance.
In all of these tests, memory is not an issue.
In their comment, jbapple raises the issue of deciding which primality test to use in practice. This is a question of implementation and benchmarking: implement and optimize a few algorithms, and experimentally determine which is fastest in which range. For the curious, the coders of PARI did just that, and they came up with a deterministic function isprime
and a probabilistic function ispseudoprime
, both of which can be found here. The probabilistic test used is Miller–Rabin. The deterministic one is BPSW.
Here is more information from Dana Jacobsen:
Pari since version 2.3 uses an APR-CL primality proof for isprime(x)
, and BPSW probable prime test (with "almost extra strong" Lucas test) for ispseudoprime(x)
.
They do take arguments which change the behavior:
isprime(x,0)
(default.) Uses combination (BPSW, quick Pocklington or BLS75 theorem 5, APR-CL).
isprime(x,1)
Uses Pocklington–Lehmer test (simple $n-1$).
isprime(x,2)
Uses APR-CL.
ispseudoprime(x,0)
(default.) Uses BPSW (M-R with base 2, "almost extra strong" Lucas).
ispseudoprime(x,k)
(for $k\geq 1$.) Does $k$ M-R tests with random bases. The RNG is seeded identically in each Pari run (so the sequence is deterministic) but is not reseeded between calls like GMP does (GMP's random bases are in fact the same bases every call so if mpz_is_probab_prime_p(x,k)
is wrong once it will always be wrong).
Pari 2.1.7 used a much worse setup. isprime(x)
was just M-R tests (default 10), which led to fun things like isprime(9)
returning true quite often. Using isprime(x,1)
would do a Pocklington proof, which was fine for about 80 digits and then became too slow to be generally useful.
You also write In reality, no one uses these algorithms, since they are too slow. I believe I know what you mean, but I think this is too strong depending on your audience. AKS in of course, stupendously slow, but APR-CL and ECPP are fast enough that some people use them. They are useful for paranoid crypto, and useful for people doing things like primegaps
or factordb
where one has enough time to want proven primes.
[My comment on that: when looking for a prime number in a specific range, we use some sieving approach followed by some relatively quick probabilistic tests. Only then, if at all, we run a deterministic test.]
In all of these tests, memory is not an issue. It is an issue for AKS. See, for instance, this eprint. Some of this depends on the implementation. If one implements what numberphile's video calls AKS (which is actually a generalization of Fermat's Little Theorem), memory use will be extremely high. Using an NTL implementation of the v1 or v6 algorithm like the referenced paper will result in stupid large amounts of memory. A good v6 GMP implementation will still use ~2GB for a 1024-bit prime, which is a lot of memory for such a small number. Using some of the Bernstein improvements and GMP binary segmentation leads to much better growth (e.g. ~120MB for 1024-bits). This is still much larger than other methods need, and no surprise, will be millions of times slower than APR-CL or ECPP.