I have a little tool called Prime Grid Explorer at <a href="https://susam.net/primegrid.html" rel="nofollow">https://susam.net/primegrid.html</a> that I wrote for my own amusement. It can display all primes below 3317044064679887385961981 (an 82-bit integer).<p>The largest three primes it can show are<p><pre><code> 3317044064679887385961783
3317044064679887385961801
3317044064679887385961813
</code></pre>
Visit <a href="https://susam.net/primegrid.html#3317044064679887385961781-20-10" rel="nofollow">https://susam.net/primegrid.html#3317044064679887385961781-2...</a> to see them plotted. Click the buttons labelled '·' and 't' to enable the grid and tooltips, then hover over each circle to see its value.<p>So essentially it can test all 81-bit integers and <i>some</i> 82-bit integers for primality. It does so using the Miller-Rabin primality test with prime bases derived from <a href="https://oeis.org/A014233" rel="nofollow">https://oeis.org/A014233</a> (OEIS A014233). The algorithm is implemented in about 80 lines of plain JavaScript. If you view the source, look for the function <i>isPrimeByMR</i>.<p>The Miller-Rabin test is inherently probabilistic. It tests whether a number is a probable prime by checking whether certain number theoretic congruence relations hold for a given base a. The test can yield false positives, that is, a composite number may pass the test. But it cannot have false negatives, so a number that fails the test is definitely composite. The more bases for which the test holds, the more likely it is that the tested number is prime. It has been computationally verified that there are no false positives below 3317044064679887385961981 when tested with prime bases 2, 3, 5, ..., 41. So although the algorithm is probabilistic, it functions as a deterministic test for all numbers below this bound when tested with these 13 bases.
There is also the segmented Sieve of Eratosthenes. It has a simlar performance but uses much less memory: the number of prime numbers from 2 to sqrt(n). For example, for n = 1000000, the RAM has to store only 168 additional numbers.<p>I use this algorithm here <a href="https://surenenfiajyan.github.io/prime-explorer/" rel="nofollow">https://surenenfiajyan.github.io/prime-explorer/</a>
The Pseudosquares Sieve will drop the memory requirements much further from sqrt(n) to log^2(n). <a href="https://link.springer.com/chapter/10.1007/11792086_15" rel="nofollow">https://link.springer.com/chapter/10.1007/11792086_15</a>
Yep, this is the natural way to go, especially considering the possibility of parallel computing and the importance of cache locality, etc.
Do you know the <a href="https://en.wikipedia.org/wiki/Sieve_of_Atkin" rel="nofollow">https://en.wikipedia.org/wiki/Sieve_of_Atkin</a>? It's mind-blowing.
If you take all 53 8 bit primes, you can use modular arithmetic with a residue base to work with numbers up to<p>64266330917908644872330635228106713310880186591609208114244758680898150367880703152525200743234420230<p>This would require 334 bits.
You can combine the Sieve and Wheel techniques to reduce the memory requirements dramatically. There's no need to use a bit for numbers that you already know can't be prime. You can find a Python implementation at <a href="https://stackoverflow.com/a/62919243/5987" rel="nofollow">https://stackoverflow.com/a/62919243/5987</a>
This got me through many of the first 100 problems on Project Euler:<p><pre><code> n = 1000000 # must be even
sieve = [True] * (n/2)
for i in range(3,int(n**0.5)+1,2):
if sieve[i/2]: sieve[i*i/2::i] = [False] * ((n-i*i-1)/(2*i)+1)
…
# x is prime if x%2 and sieve[x/2]
</code></pre>
Edit: I guess I irked someone. :/ Yes this is a memory hog, but to me beautiful because it’s so tiny and simple. I never tried very hard, but I wonder if it could be made a real one-liner.
I always like seeing implementations that start from trial division and gradually introduce optimizations like wheel factorization.<p>It makes the trade-offs much clearer than jumping straight to a complex sieve.
Very well written
> There is a long way to go from here. Kim Walisch's primesieve can generate all 32-bit primes in 0.061s (though this is without writing them to a file)<p>Oh, come on, just use a bash indirection and be done with it. It takes 1 minute and you had another result for comparison
Why include writing the primes to a file instead of, say, standard output? That increases the optimization space drastically and the IO will eclipse all the careful bitwise math<p>Does having the primes in a file even allow faster is-prime lookup of a number?
there are also very fast primality tests that work statistically. It's called Miller-Rabin, I tested in the browser here[1] and it can do them all in about three minutes on my phone.<p>[1] <a href="https://claude.ai/public/artifacts/baa198ed-5a17-4d04-8cef-72d527f7dfb1" rel="nofollow">https://claude.ai/public/artifacts/baa198ed-5a17-4d04-8cef-7...</a>
[dead]