View Single Post
  #4 (permalink)  
Old August 27th, 2004, 12:00 PM
janq janq is offline
Registered User
 
Join Date: Aug 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to janq
Default

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

Reply With Quote