fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. int main() {
  7. int T;
  8. int m, n;
  9. int const MAX = 1000000000;
  10.  
  11. std::vector<bool> flags((MAX + 1)/2, true);
  12.  
  13. double halfMax = std::ceil(std::sqrt(MAX));
  14. for (int i = 3; i <= halfMax; i+=2) {
  15. if (flags[i/2]) {
  16. for (int j = i * i; j <= MAX; j += i*2) {
  17. flags[j/2] = false;
  18. }
  19. }
  20. }
  21.  
  22. std::cin >> T;
  23. while (T--) {
  24. std::cin >> m >> n;
  25. if (m < 3) {
  26. std::cout << "2 ";
  27. }
  28. for (int k = std::max(m&1, 3); k <= n; k+=2) {
  29. if (flags[k/2]) {
  30. std::cout << k << ' ';
  31. }
  32. }
  33. std::cout << '\n';
  34. }
  35. }
Success #stdin #stdout 4.19s 15240KB
stdin
2
1 10
3 5
stdout
2 3 5 7 
3 5