fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <vector>
  4. #include <time.h>
  5.  
  6. using namespace std;
  7.  
  8. int N,cnt = 0;
  9. vector<int> primes;
  10.  
  11. int is_prime(int p)
  12. {
  13. int sq = sqrt(p);
  14. for (int ps : primes) {
  15. if(sq < ps)
  16. return 1;
  17. if(p % ps == 0)
  18. return 0;
  19. }
  20. return 1;
  21. }
  22.  
  23. void prime(int N)
  24. {
  25. int k = 0;
  26. cnt += 2;
  27. primes.push_back(2);
  28. primes.push_back(3);
  29. while (cnt < N) {
  30. k++;
  31. if(is_prime(6*k-1) == 1){
  32. cnt ++;
  33. primes.push_back(6*k-1);
  34. }
  35. if(is_prime(6*k+1) == 1){
  36. cnt ++;
  37. primes.push_back(6*k+1);
  38. }
  39. }
  40. return;
  41. }
  42.  
  43. int main() {
  44. int a;
  45. clock_t start,end;
  46. double result;
  47. scanf("%d",&a);
  48. scanf("%d", &N);
  49. start = clock();
  50. prime(N);
  51. if(a == 1) printf("%d",primes[N-1]);
  52. if(a == 0) for(int x : primes)
  53. printf("%d ",x);
  54. end = clock();
  55. result = (double)(end - start);
  56. printf("%f",result);
  57. return 0;
  58. }
Success #stdin #stdout 2.97s 12460KB
stdin
1 1500000
stdout
238795192963485.000000