#include #include #include using namespace std; int main() { int start, end; cin >> start; cin >> end; // const int start = 1; // const int end = 1000000; list primes; primes.push_back(1); primes.push_back(2); primes.push_back(3); primes.push_back(5); // Fill all numbers, excluding most obvious non-primes numbers for (int i = 6; i <= end; i++) { if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0) { continue; } primes.push_back(i); } auto it = primes.begin(); advance(it, 4); int stop = sqrt(end); for (it; *it <= stop; it++) { /* Go through all numbers in the list that are lower than sqrt(end) The list is modified in the loop, allowing to skip processing of some numbers */ int factor = *it; auto it2 = it; it2++; auto stop2 = primes.end(); for (it2; it2 != stop2; it2++) { /* Go through all numbers that are greater than the *it, removing them if they can be divided by *it */ if (*it2 % factor == 0) { auto tmp = next(it2); primes.erase(it2); it2 = prev(tmp); } } } // A dumb way to handle end < 5 if (end < primes.back()) { auto trueEnd = primes.begin(); auto stop = primes.end(); while (trueEnd != stop && *trueEnd <= end) { trueEnd++; } primes.erase(trueEnd, stop); } auto startIt = primes.end(); auto primesEnd = primes.end(); it = primes.begin(); for (it; it != primesEnd; it++) { if (*it >= start) { startIt = it; break; } } if (start == end && *startIt != start) { // Range contains only one number and it's not prime cout << "Absent" << endl; } else if (it == primesEnd) { cout << "Absent" << endl; } else { it = startIt; for (it; it != primesEnd; it++) { cout << *it << " "; } cout << endl; } return 0; }