TITLE: How many prime numbers in a given interval?
QUESTION [6 upvotes]: Is there any algorithm or a technique to calculate how many prime numbers lie in a given closed interval [a1, an], knowing the values of a1 and an, with a1,an ∈ ℕ?
Example: 
[2, 10] --> 4 prime numbers {2, 3, 5, 7}
[4, 12] --> 3 prime numbers {5, 7, 11}

REPLY [2 votes]: Use the prime-counting function and get $\pi(a_n)-\pi(a_1-1)$

REPLY [2 votes]: The prime number theorem gives an approximation to $\pi(n),$ the number of primes less than $n$.  You can subtract the values at the start and end of an interval.  Mathematica and other software can compute exact values for small values of $n$ and approximate values for larger values of $n$.  The first approximation is $\pi(n) \sim \frac n{\log n}$ but a much better one is $$\pi(n)\sim \int_2^n \frac {dt}{\log t}$$