6
$\begingroup$

Are there any good algorithms which can be used to construct a prime greater than $n$, for arbitrary $n$?

There are some brute force approaches: for example, factoring $n!+1$. However, I'm looking for something significantly more efficient; preferably, polynomial in $n$.

Also, if anyone knows of a proof showing a bound on the speed of such an algorithm (say, $n^4$), I would like to hear about it.

EDIT: It appears that polynomial in $\ln(n)$ makes more sense.

  • 0
    I have an algorithm that runs in poly time, but requires that $n$ is given in unary (smirk!).2011-09-15

4 Answers 4

0

Polynomial in $n$ is not efficient because the input length (measured in bits) is $\log_2 n$ and so $n$ is exponentially large in relation to your input length.

So it is easy to find a prime number greater than $n$ in time polynomial in $n$ (e.g. with the answer from Moron).

If you are interested in a deterministic algorithm than the polymath project gives you the current known results.

But there is a randomized algorithm with time polynomial in $\log_2 n$ that gives you a prime greater than $n$. You pick a random number from the set $n, \ldots, 2n$ and check this number with AKS or Miller Rabin primality test. With the prime number theorem you can proof that there are enough primes in the set (for large $n$) and if you repeat this algorithm $O(\log_2 n)$ times you find a prime with high probability.

At the moment I cannot find a reference for a detailed proof for the randomized algorithm. But if you are interested I can look for it when I have more time.

6

Well theoretically speaking, there is a deterministic $\mathcal{O}(n \log ^{12} n)$ time algorithm.

By Bertrand's postulate, there is a prime $p$ guaranteed to satisfy $n \le p \lt2n$. (In practice, you should hit one much earlier than $2n$, owing to the prime number theorem).

Use the AKS primality testing program to test each of the numbers in the range $[n, 2n)$ and you will find one.

You can also try some sieving etc as suggested in the answers to this other question here: The least prime greater than 2000

  • 0
    The time can be decreased to $O(n^{0.525+\varepsilon})$ due to Baker-Harman-Pintz 2001.2011-09-15
3

Terence Tao has set up a "polymath project" which addresses exactly your question. Here is the relevant page:

http://michaelnielsen.org/polymath1/index.php?title=Finding_primes

It contains a list of conjectures, partial results and references. To sum it all up: At the moment the answer to your question is No.

1

There is an algorithm of complexity $O((\ln n) \cdot P(n))$ where $P(n)$ is the complexity of computing the prime counting function on n (usually denoted $\pi(n)$).

By Bertrand's postulate there is a prime in the interval $[n,2n]$ so you can do a binary search on it:

If $\pi\left(n+\frac{n}{2}\right)-\pi(n)$ > 1 there is a prime in the interval $\left[n, n+\frac{n}{2}\right]$, otherwise there is a prime in the interval $\left[n+\frac{n}{2}+1,2n\right]$ and so you obtain a smaller interval that contains a prime. Repeat the calculation on the new interval. The calculation for which of the 2 intervals to use needs time $O(P(n))$. Since each interval is half the size of the previous one the total number of calculations required is O(ln n).

Odlyzko's algorithm for $\pi(n)$ has complexity $O(n^{\frac{1}{2}})$. There is a link to a short description of it on the polymath page. So the overall complexity of this algorithm is $O((\ln n)n^{\frac{1}{2}})$.

  • 1
    Isn't it $O(n^{0.5+\varepsilon})$? In that case the $\varepsilon$ subsumes the $\log n$ term entirely.2011-09-15