1

I would like to detect prime numbers within an interval $\mathcal{I}:[a,b]$. The only problem is that the interval contains very large numbers - in excess of 10000 digits each and $\# \mathcal{I}$ is also very large.

One of my tasks involves answering the following question:

"Given a positive integer $i\in\mathcal I$, is $i$ prime?"

Testing for primality using PrimeQ[] takes far too long for this many large numbers. So, I thought about first gathering a list of probable primes, before engaging in any rigorous prime-proving process.

So basically what I'm asking is whether there exists a function like ProbablePrimeQ[] that can return very fast and give a first indication whether the input is prime. Does such a function exist in Mathematica?

Klangen
  • 1,009
  • 5
  • 14
  • 3
    PrimeQ is already the fast probabilistic primality test. It combines small prime testing, Miller-Rabin testing in base 2 & 3 and a Lucas Test. This procedure is known to produce no false positive for $<2^{64}$. Mathematica features a provable prime test ProvablePrimeQ (in the primality proving package <<PrimalityProving) which is much slower than PrimeQ. If you need to generate a list of good candidates, just do your own small prime tests beforehand with the first few thousand primes or so. This should be reasonable fast. – Julien Kluge Jan 11 '23 at 11:31
  • 1
    Timings for numbers with 10K digits are not too bad. Around 9 seconds per number in many of the slow cases, below milliseconds on most cases, TableForm @ Map[ AbsoluteTiming[PrimeQ[#]]& , (Round[Pi*10^11111, 2] + 1 + 2*Range[25] ) ] – rhermans Jan 11 '23 at 12:03
  • @JulienKluge Thanks. Do you know some "standard" way of doing that in Mathematica? Do you perhaps have a link to an existing SE post? Or do you just mean "divide by the first thousand primes"? – Klangen Jan 11 '23 at 12:26
  • @rhermans If we take 4.5 seconds per call to PrimeQ[], and assuming I have to check $10^9$ numbers in the interval, I would require 142 years to complete the task. If I go through $10^{10}$ numbers, then that changes to 1425 years. I don't think I can wait that long. – Klangen Jan 11 '23 at 12:29
  • @Klangen it is not about your expectations, but about what Mathematica can do. You were already told that ProvablePrimeQ is slower than PrimeQ. Unless you present a more constrained case with a clever idea to be implemented, this is as far one can go with your general question. – rhermans Jan 11 '23 at 13:51
  • 1
    If I recall correctly the slower step in PrimeQ, when assessing numbers that are in fact prime, is the use of the Lucas test. So you might just implement Miller-Rabin for a couple of bases. But there might then be a handful of composites that are claimed as prime. – Daniel Lichtblau Jan 11 '23 at 17:16

0 Answers0