Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <array>
  4. #include <vector>
  5. #include <math.h>
  6.  
  7. const int _max = 1000000000;
  8.  
  9. using namespace std;
  10.  
  11. bool IsPrime(const int& n, std::vector<int>& v) {
  12.  double root = sqrt(n);
  13.  auto it = v.begin();
  14.  while (it != v.end() && *it <= root) {
  15.   if(! (n % *it)) return false;
  16.   it++;
  17.  }
  18.  return true;
  19. }
  20. void Primes(int min, int finish) {
  21.  static std::vector<int> prime_array;
  22.  static bool z = true;
  23.  if (z) {
  24.   z = false;
  25.   for (int i = 2; i <= sqrt(_max); ++i) {
  26.    if(IsPrime(i, prime_array)) {
  27.     prime_array.push_back(i);
  28.    }
  29.   }
  30.  }
  31.  
  32.  for (int i = min; i <= finish; ++i) {
  33.   if (IsPrime(i, prime_array)) {
  34.    std::cout << i << std::endl;
  35.   }
  36.  }
  37. }
  38. int main() {
  39.  int t = 0;
  40.  std::cin >> t;
  41.  for(int i = 0; i < t; ++i) {
  42.   int m,n = 0;
  43.   std::cin >> m;
  44.   std::cin >> n;
  45.   Primes(m,n);
  46.  }
  47.  
  48.  return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement