int main () { limit = 1<<16; primeTable(limit); while (scanf("%lld%lld", &L, &R) == 2) { solve(); if (cnt <= 1) printf("There are no adjacent primes.\n"); else { int minRec, maxRec, minDis, maxDis; maxDis = 0; minDis = 1e7; for (int i = 1; i < cnt; i++) { int tmp = set[i] - set[i-1]; if (tmp > maxDis) { maxDis = tmp; maxRec = i; } if (tmp < minDis) { minDis = tmp; minRec = i; } } printf("%lld,%lld are closest, %lld,%lld are most distant.\n", set[minRec-1]+L, set[minRec]+L, set[maxRec-1]+L, set[maxRec]+L); } } return 0; }
//time complexity: O(nloglogn) //space complexity: O(n) int countPrimes(int n) { vector<bool> primeTable(n, true); int count = 0; for (int i = 2; i * i < n; i++) { if (!primeTable[i]) { continue; } for (int j = i * i; j < n; j += i) { primeTable[j] = false; } } for (int i = 2; i < n; i++) { count += primeTable[i] ? 1 : 0; } return count; }