Given a random number, what would be the quickest possible way of finding out whether it was prime?
Obviously, one could just iterate through the number in order to see if it was divisible by anything, but can you implement quicker ways of finding a given number?
The answer depends on the size.
For numbers less than $2^{64}$ what I've found best is:
I suggest doing a verification using both small inputs and the Feitsma SPRP-2 pseudoprime database to make sure your code works. There have been some unfortunate incidents where primality tests in oft-used libraries had errors.
Ideally you will want optimized code for the tests. E.g. Izykowski's x86-64 M-R code or my examples (the latter uses Izykowski's x86-64 Montgomery routines if possible). My code also shows use of the pre-test, trial division with mod-30 wheel, hashed single-base M-R for 32-bit inputs, and AES BPSW for 64-bit.
For numbers larger than $2^{64}$ you're presumably using something like GMP. The exact set of operations that give the best result will depend heavily on your bigint library and your platform. What I do using GMP, which is all heuristics to start:
mpz_even_pfollowed bympz_gcd_uiwith one or two ui-sized primorials.mpz_gcdwith a calculated-once primorial. GMP's gcd operation is very well optimized for large inputs, and works significantly faster than doingmpz_divisible_pon each number. Benchmark and adjust as desired. My solution is a compromise since I don't want the computation or memory use given that people may not call it with large inputs, so I do a gcd with primes less than 1009 first, then if more than 700 bits I'll do primes less than 40000, and if more than 300 bits primes less than 10000. Again these were just examples that worked for my application, but whatever actual numbers you use, this is a fast pre-test.mpz_divisible_ui_p, over that I do a simple treesieve. You could obviously skip this (Pari does, for instance), but it does make things a lot faster for inputs of 10k+ digits.For proofs, here are a few points to consider:
Some available proof software:
There are research groups with private code that also do primality proofs, but that doesn't help the rest of us. On the other hand, these groups sometimes publish papers describing their methods, which is of immense value. Atkin and Morain's papers are great stuff and allowed others to write independent software.