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