Guest User

Untitled

a guest
Mar 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. bool flag = false;
  2. for (int j = 3; j < n; j+=2)
  3. {
  4. double b = sqrt(j);
  5. for (int a = 1; a<b; a+=2)
  6. {
  7. if (j%a == 0)
  8. {
  9. flag = true;
  10. break;
  11. }
  12. }
  13. if (flag) cout << j;
  14. }
  15.  
  16. int main(int argc, const char * argv[])
  17. {
  18. int n = 100;
  19.  
  20. cout << 2 << endl;
  21.  
  22. for (int j = 3; j < n; j+=2)
  23. {
  24. bool flag = true;
  25. if (j%2 == 0) continue;
  26. for (int a = 3; a*a <= j; a+=2)
  27. {
  28. if (j%a == 0)
  29. {
  30. flag = false;
  31. break;
  32. }
  33. }
  34. if (flag) cout << j << endl;
  35. }
  36.  
  37. }
  38.  
  39. int main(int argc, const char * argv[])
  40. {
  41. int n = 100;
  42. vector<int> primes = { 2 };
  43.  
  44. cout << 2 << endl;
  45.  
  46. for (int j = 3; j < n; j+=2)
  47. {
  48. bool flag = true;
  49. for(auto i: primes)
  50. {
  51. if (j % i == 0)
  52. {
  53. flag = false;
  54. break;
  55. }
  56. if (i*i > j) break;
  57. }
  58. if (flag)
  59. {
  60. cout << j << endl;
  61. primes.push_back(j);
  62. }
  63. }
  64.  
  65. }
  66.  
  67. for (int a = 1; a<b; a+=2)
  68. ^^^^^
  69.  
  70. if (j%a == 0)
  71.  
  72. for (int j = 3; j < n; j+=2)
  73. ^^^^^
  74.  
  75. #include <iostream>
  76.  
  77. std::ostream & output_primes( unsigned int n, std::ostream &os = std::cout )
  78. {
  79. for ( unsigned int i = 2; i <= n; i++ )
  80. {
  81. bool prime = i == 2 || i % 2;
  82.  
  83. for ( unsigned int j = 3; prime && j * j <= i; j += 2 ) prime = i % j;
  84.  
  85. if ( prime ) os << i << ' ';
  86. }
  87.  
  88. return os;
  89. }
  90.  
  91. int main()
  92. {
  93. const unsigned int N = 25;
  94.  
  95. for ( unsigned int i = 1; i <= N; i++ )
  96. {
  97. std::cout << "Prime numbers less than or equal to " << i << ": ";
  98. output_primes( i ) << std::endl;
  99. }
  100.  
  101. return 0;
  102. }
  103.  
  104. Prime numbers less than or equal to 1:
  105. Prime numbers less than or equal to 2: 2
  106. Prime numbers less than or equal to 3: 2 3
  107. Prime numbers less than or equal to 4: 2 3
  108. Prime numbers less than or equal to 5: 2 3 5
  109. Prime numbers less than or equal to 6: 2 3 5
  110. Prime numbers less than or equal to 7: 2 3 5 7
  111. Prime numbers less than or equal to 8: 2 3 5 7
  112. Prime numbers less than or equal to 9: 2 3 5 7
  113. Prime numbers less than or equal to 10: 2 3 5 7
  114. Prime numbers less than or equal to 11: 2 3 5 7 11
  115. Prime numbers less than or equal to 12: 2 3 5 7 11
  116. Prime numbers less than or equal to 13: 2 3 5 7 11 13
  117. Prime numbers less than or equal to 14: 2 3 5 7 11 13
  118. Prime numbers less than or equal to 15: 2 3 5 7 11 13
  119. Prime numbers less than or equal to 16: 2 3 5 7 11 13
  120. Prime numbers less than or equal to 17: 2 3 5 7 11 13 17
  121. Prime numbers less than or equal to 18: 2 3 5 7 11 13 17
  122. Prime numbers less than or equal to 19: 2 3 5 7 11 13 17 19
  123. Prime numbers less than or equal to 20: 2 3 5 7 11 13 17 19
  124. Prime numbers less than or equal to 21: 2 3 5 7 11 13 17 19
  125. Prime numbers less than or equal to 22: 2 3 5 7 11 13 17 19
  126. Prime numbers less than or equal to 23: 2 3 5 7 11 13 17 19 23
  127. Prime numbers less than or equal to 24: 2 3 5 7 11 13 17 19 23
  128. Prime numbers less than or equal to 25: 2 3 5 7 11 13 17 19 23
Add Comment
Please, Sign In to add comment