Advertisement
HasanRasulov

prime1.cpp

Dec 22nd, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4.  
  5. void SieveOfEratosthenes(int n, int m)
  6. {
  7.  
  8. int size = (m + 1) / 8 + 1;
  9.  
  10. unsigned char mark[size];
  11.  
  12. memset(mark, 255, sizeof(mark));
  13.  
  14. for (int p = 2; p * p <= m; p++)
  15. {
  16.  
  17. if ((1 << (8 - p % 8)) & mark[p / 8])
  18. {
  19.  
  20. for (int i = p * p; i <= m; i += p)
  21. mark[i / 8] = ((~(1 << (8 - i % 8))) & mark[i / 8]);
  22. }
  23. }
  24.  
  25. for (int p = n; p <= m; p++)
  26. if ((1 << (8 - p % 8)) & mark[p / 8])
  27. std::cout << p << '\n';
  28. }
  29.  
  30. int main()
  31. {
  32.  
  33. int t;
  34. std::cin >> t;
  35.  
  36. while (t--)
  37. {
  38.  
  39. int n, m;
  40. std::cin >> n >> m;
  41.  
  42. SieveOfEratosthenes(n==1 ? ++n : n, m);
  43. }
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement