TITLE: The fastest way to count prime number that smaller or equal N
QUESTION [6 upvotes]: I want to count all prime numbers that existing in N but I don't know how to count. Can any one tell me how to count prime numbers that are smaller than or equal to N in mathematics formal?

REPLY [15 votes]: Some methods:

Count via primality as mentioned by frogeyedpeas.  Use something like BPSW since we're not going to be over $2^{64}$.  Lowest memory, but crazy slow and using a segment sieve makes the memory issue pretty much moot.  An additional note since it was brought up, AKS is completely impractical to use for this -- it provides no benefit and runs extremely slow.
Use a segmented sieve.   primesieve is an excellent off-the-shelf choice, but you can find them all over.  Segmenting the SoE will result in both faster performance for large inputs as well as constraining the memory to whatever sane bound you want.  It loses steam around $10^{11}$ or so, and isn't the fastest method regardless.
Use a sparse table plus a segmented sieve.  This can be quite fast, as the sparse table holds the counts at selected intervals, meaning you only need to sieve from the closest table point.  Mathematically no more interesting than the previous option, but in practice this helps.  I use this for numbers under 60,000,000, as that was the crossover for my code when looking at optimizing performance while keeping the table space under 2k.
Combinatorial prime count methods.  These include the Legendre, Meissel, Lehmer, LMO, and LMO extensions.  See code for each at primecount or Math::Prime::Util.  These are much, much faster than sieving.  A single core can count in a second what primesieve (the current fastest sieve code) takes over 2 hours to do using 8 cores.  The growth rate is lower as well so this difference just gets bigger.  Memory growth for Legendre/Meissel/Lehmer can be problematic for very large values, but LMO and eLMO are quite good up to $2^{64}$.  References include:

Prime-counting function on Wikipedia
Hans Riesel's "Prime Numbers and Computer Methods for Factorization" book
Computing $\pi(x)$: the combinatorial method by T Oliveira e Silva
Computing $\pi(x)$: the Meissel-Lehmer method by Lagarias, Miller, and Odlyzko (1985)
Computing $\pi(x)$: the Meissel, Lehmer, Lagarias, Miller, Odlyzko method by Deleglise and Rivat (1996)
Gourdon (2001) and Gourdon & Sebah (2004) also have some later improvements.

Analytic methods, introduced in 1987 by Lagarias and Odlyzko in Computing $\pi(x)$: An Analytic Method.  See, for example, How to Not Count Primes, a little presentation by DJ Platt.  The analytic methods have been used for values up to $10^{25}$, and for years were about two orders of magnitude higher than the combinatorial methods.  I'm not aware of any open source implementations, but two research teams are actively working on running their implementations (FKBJ and Platt).

5 months after I originally wrote this, Douglas Staple published his results for $10^{26}$ from a distributed combinatorial algorithm.  Later Kim Walisch wrote a distributed version his excellent open source primecount program, which was used to validate the result.  Continuous improvements have been made to his program, and it currently holds the record for calculating $\pi(10^{27})$.  It is also the fastest program for smaller numbers as well.
Note that approximations and bounds can be done very quickly and are extremely close.  For bounds see Dusart (2010).  This is actually an area of current research, with papers showing tighter bounds coming out every year.  For approximation we can use the simple $n/(log(n)-1)$ which works but much worse than the following methods.  Averaging the Dusart bounds gets significantly closer without a lot more work.  The Logarithmic Integral is much closer yet.  We can improve on this by adding another term: $\pi(n) \approx li(n)-li(\sqrt{n})/2$.  Better yet is the full Riemann R function.  It is off by only 0.00000001% at $10^{19}$.