0

Counting up from zero, if you find a prime number, is there a certain intervall you can skip knowing for sure that sequent prime number will not be skipped?

For reference, I am writing a program which searchs for prime numbers by checking every number up from 2. The problem is that once the number is big enough it takes very much time to find the next number. For example, if the program detects a 10-digit prime number, can I skip a certain intervall knowing that the next one won't be left out?

Shaun
  • 47,747
  • 3
    Twin primes will pose a problem for this. – Randall Nov 27 '20 at 14:18
  • 2
    You can skip every even number. –  Nov 27 '20 at 14:21
  • Advances in the study of gaps between primes have shown that differences of six between primes will occur infinitely often. It remains an open problem whether differences of two occur infinitely often (the twin prime conjecture says they do). – hardmath Nov 27 '20 at 14:24
  • 2
    Either you need more efficient primality tests (there are much better tests than the cumbersome and very slow trial division) , or (even better) , you apply sieving algorithms , but in this case you might have to partition the intervals because of memeory issues) – Peter Nov 27 '20 at 14:24
  • 4
    It sounds like you're searching for primes by doing trial division on every number. That's not very efficient. Take a look at the sieve of Eratosthenes. Or wheel sieves. If you want to search for primes without specifying an upper limit (and without storing all primes found so far), there are ways to do that too, eg the segmented sieve. – PM 2Ring Nov 27 '20 at 14:26
  • 1
    @hardmath This is not quite true. The best known unconditional result is that prime gaps with difference $246$ or less occur infinite many often. For no concrete gap it is known whether it occurs infinite many often. The version with difference $6$ needs a conjecture that is not yet proven. Additionally, I do not see how this result would help the author with his program. – Peter Nov 27 '20 at 14:27
  • @Peter: You are correct, and I was a bit lazy not to give more details. However the Question asks about whether it would eventually be safe to skip over a fixed gap in checking for primes, and my Comment says no, we cannot be sure this will be safe should we skip more than two ahead. Of course sieving methods accomplish the goal of finding primes more efficiently by a somewhat related idea. – hardmath Nov 27 '20 at 14:38
  • @hardmath I know how it was meant, and it hits the nail on the head. In fact, the current number +2 could well be prime if we neither know a factor nor have applied a primality test. So, consider my comment only as a correction, not as a critique. – Peter Nov 27 '20 at 14:43
  • Perhaps, the title with the prime gaps is a bit misleading. Actually, you want to know which numbers can be "immediately" skipped. Unfortunately, only the numbers having very small prime factors ! So, the prime gap structure would not help very much, even if the twin prime conjecture would have been proven. Best is : Sieve out candidates and then test them. This will give the best overall speed. – Peter Nov 27 '20 at 14:48
  • See this 2013 Question Yitang Zhang: Prime Gaps and its Answers for links to some of the details @Peter mentions. – hardmath Nov 27 '20 at 16:25

2 Answers2

6

No, but you can do something similar. The first 3 primes are $2, 3, 5$ and $2\cdot 3\cdot 5 = 30$. If a number is greater than $30$ and prime then it must be congruent to $1, 7, 11, 13, 17, 19, 23,$ or $29$ modulo $30$.

So for every block of 30 numbers, you need only check 8 of them.

You can expand this method by using more than the first 3 primes.

B. Goddard
  • 33,728
0

Chebyshev said it, I'll say it again:
There's always a prime between $n$ and $2n$.

Shaun
  • 47,747