4
$\begingroup$

Is there any function to evaluate the number of prime numbers between [2, n]?

For example, consider the following range: [2, 20]. In this case the number of prime numbers between 2 and 20 is 8: 2, 3, 5, 7, 11, 13, 17, 19. Therefore the function I'm looking for would return 8.

Also, is there any function to evaluate the number of prime numbers between [x, y]?

$\endgroup$

3 Answers 3

3
$\begingroup$

There are some formulas but the best we have so far is only asymptotic estimates.

It is shown that if we denote with $\pi(n)$ the number of primes that do not exceed $n$ then the fraction
$$\frac{\pi(n)lnn}{n}$$ can be arbitrarily close to $1$.
This is the famous prime number theorem.

$\endgroup$
2
  • 1
    $\begingroup$ Not true. There are lots of formulas which give better errors than that of the original log-version of PNT as well as some formulas giving exact values of $\pi(n)$. $\endgroup$ Commented Jan 21, 2014 at 14:07
  • $\begingroup$ not "can be arbitrarily close to 1", but "the limit for n->infinity is 1". $\endgroup$ Commented May 3, 2014 at 19:05
2
$\begingroup$

Number of primes between $[1,n]$ can be evaluate with $\frac{n}{\ln n}$

Number of primes between $[x,y]$ can be evaluate with $\frac{y}{\ln y}-\frac{x}{\ln x}$

$\endgroup$
1
$\begingroup$

For a reasonably fast method (substantially faster than finding all the primes) thanks to Meissel, Lehmer, Lagarias, Miller (who I saw posting on math.stackexchange recently) and Odlyzko, see

http://www.ams.org/journals/mcom/1996-65-213/S0025-5718-96-00674-6/S0025-5718-96-00674-6.pdf

For a not quite so fast but easier method by Legendre see

http://programmingpraxis.com/2011/07/22/counting-primes-using-legendres-formula/

Usually the function denoting the number of primes <= n is denoted as π (n). The number of primes in the interval [x, y] with x <= y is obviously π (y) - π (x - 1).

$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.