令Pi表示第i个素数。现任给两个正整数M <= N <= 10000,请输出PM到PN的所有素数。
代码直接实现:
#include<iostream> #include<cmath> using namespace std; int main() { int n, m, count = 0; cin >> n >> m; for (int i = 2; count <= m; i++) { int temp = 0; for (int j = i; j<sqrt(i); j++) { if (i % j == 0) { temp++; } } if (temp == 0) { count++; } if (count >= n && count <= m && temp == 0) { cout << i; if ((count - n) % 10 == 9) { cout << endl; } else if (count != m) { cout << " "; } } } cout << endl; return 0; }