For problem of finding primes, the best solution is Eratostenes Sieve.
Here is my a code for your problem:
int count_of_primes(int n){ //We are finding primes between 1..n
int i,j;
int count=0;
bool prime[1000001];
for(i=2;i<=n;i++) prime[i]=true;
for(i=2;i<=sqrt(n);i++) if (prime[i]){
for(j=i*2;j<=n;j+=i) prime[j]=false;
}
for(i=2;i<=n;i++) if (prime[i]) count++;
return(count);
}
See also
http://mathworld.wolfram.com/SieveofEratosthenes.html