输入样例:
5 27
输出样例:
11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101 103
Code
先打好素数表,因为题中要求M,N小于10^4,这里的M,N是指素数表中第M个,第N个素数,所以需要筛到10^5
以上,但刚好10^5
是过不了的,我筛到10^6
了,素数个数达到7万多,足够了。
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int Max = 1e6;
bool book[Max] = {false};
int num[Max];
void Init() {
int k = 0;
for(int i = 2; i < Max; i++) {
if(book[i] == false) {
for(int j = 2; i*j < Max; j++) {
book[i*j] = true;
}
}
}
for(int i = 2; i < Max; i++) {
if(book[i] == false) {
num[k++] = i;
}
}
}
int main() {
Init();
int m, n;
cin >> m >> n;
int k = 0;
for(int i = m - 1; i < n; i++) {
k++;
cout << num[i] << " \n"[i == n-1 || k%10 == 0]; // 行末换行,没有空格
}
return 0;
}