Advertisement
uzimane_

найти все простые числа от 1 до n

Jul 10th, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. freopen("input.txt", "r", stdin);
  8. freopen("output.txt", "w", stdout);
  9.  
  10. int a;
  11. int b = 0;
  12. cin >> a;
  13.  
  14.  
  15. if (a == 1)
  16. {
  17. cout << "";
  18. }
  19. if (a == 2)
  20. {
  21. cout << "2";
  22. }
  23. if (a > 2)
  24. {
  25. cout << "2" << " " << "3" << " ";
  26. for (int i = 5; i <= a; i += 2)
  27. {
  28. for (int j = 2; j <= sqrt(i); )
  29. {
  30. j++;
  31. if ((i % j) == 0)
  32. {
  33. break;
  34. }
  35. else
  36. {
  37. if (j > sqrt(i))
  38. {
  39. cout << i << " ";
  40. }
  41. }
  42. }
  43. }
  44. }
  45.  
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement