10001st prime

Project Euler problem #7 solution in C++ (Eratosphene’s sieve used):

#include <iostream>
#include <vector>
using namespace std;
int main(){
  vector<unsigned long long> num(10001);
  num[0]=2; num[1]=3; num[2]=5; num[3]=7; num[4]=11; num[5]=13;
  unsigned long long j=17;
  int i=5;
  do{
    for(int k=0;k<i;k++){
      if(j%num[k]==0){
        j+=2;
        break;
      }
      if(k==(i-1)){
        i++;
        num[i]=j;
        j+=2;
        break;
      }
    }
  }while(i<10001);
  cout << num[10000] << '\n';
  system(“pause”);
  return 0;
}


Leave a Reply

Name *
Email *
Website