Skip navigation

Another simple brute force. Took 9 seconds.

#include <iostream>
#include <vector>

int main (int argc, char * const argv[]) {

    unsigned long long cur = 3;
    int limit = 10001;
    int found = 2; //ones already accounted for;
    int i;
    bool isPrime;
    std::vector<unsigned long long> sieve;

    sieve.push_back(2);
    sieve.push_back(3);
    while(found < limit){
      isPrime = true;
      for(i=0; i < sieve.size(); i++){
        if(!(cur % sieve[i])){
          isPrime = false;
          break;
        }
      }
      if(isPrime){  
        sieve.push_back(cur);
        found++;
      } 
      cur += 2;
    }
    std::printf("The %dth prime number is: %llu \n", found, cur - 2 );
    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: