fork(10) download
  1. # include <iostream>
  2. # include <vector>
  3. # include <cmath>
  4. # include <ctime>
  5. using namespace std;
  6. vector <unsigned int> prime_data;
  7. bool is_Prime (unsigned int num)
  8. {
  9. if (num == 1) return false;
  10. unsigned int j = sqrt ((long double)num) + 1;
  11. unsigned int x = prime_data.size();
  12. unsigned int i = 0;
  13. while (prime_data[i] <= j && i < x)
  14. {
  15. if ((num % prime_data[i]) == 0 && prime_data[i] != num)
  16. {
  17. return false;
  18. }
  19. ++i;
  20. }
  21. return true;
  22. }
  23.  
  24. /*void generate_prime_numbers (unsigned int num)
  25. {
  26. unsigned int maxi = sqrt ((long double)num) + 1;
  27. prime_data.push_back (2);
  28. prime_data.push_back (3);
  29. for (unsigned int i = 5; i <= maxi; i+=2)
  30. {
  31. if (is_Prime (i))
  32. {
  33. prime_data.push_back (i);
  34. cout << "Prime " << i << endl;
  35. }
  36. }
  37. }*/
  38.  
  39. void generate_prime_numbers (unsigned int num)
  40. {
  41. unsigned int sqmaxi = sqrt ((long double)num) + 1;
  42. prime_data.push_back (2);
  43. prime_data.push_back (3);
  44. int mini, maxi;
  45. for (unsigned int i = 1, mini = (i*6)-1; mini <= sqmaxi; i++, mini = (i*6)-1)
  46. {
  47. maxi = (i*6)+1;
  48. if (is_Prime (mini))
  49. {
  50. prime_data.push_back (mini);
  51. //cout << "Prime " << mini << endl;
  52. }
  53. if (is_Prime (maxi))
  54. {
  55. prime_data.push_back (maxi);
  56. //cout << "Prime " << maxi << endl;
  57. }
  58. }
  59. }
  60.  
  61. int main ()
  62. {
  63. int total = 0;
  64. cin >> total;
  65. generate_prime_numbers (1000000000);
  66. for (int i = 0; i < total; i++)
  67. {
  68. unsigned int istart, iend;
  69. cin >> istart >> iend;
  70. for (unsigned int j = istart; j <= iend; j++)
  71. {
  72. if (is_Prime (j))
  73. {
  74. cout << j << endl;
  75. }
  76. }
  77. cout << endl;
  78. }
  79. return 0;
  80. }
  81.  
Success #stdin #stdout 0.02s 2816KB
stdin
1
1 100
stdout
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97