令 Pi 表示第 i 个素数。现任给两个正整数 M≤N≤104,请输出 PM 到 PN 的所有素数。
输入格式:
输入在一行中给出 M 和 N,其间以空格分隔。
输出格式:
输出从 PM 到 PN 的所有素数,每 10 个数字占 1 行,其间以空格分隔,但行末不得有多余空格。
输入样例:
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
#include <bits/stdc++.h>
using namespace std;
int a[150001],p[100001];
int main()
{
int n,m,t=2,len=0;
cin>>m>>n;
p[1]=2;
for(int i=2; i<=150001; i++){
if(i%2)a[i]=true;
else a[i]=false;
}
for(int i=3; i<=sqrt(150001); i++){
if(a[i]){
for(int j=i+i; j<150001; j=j+i){
a[j]=false;
}
}
}
for(int i=2; i<=150001; i++){
if(a[i]){
p[t++]=i;
}
}
for(int i=m;i<=n;i++){
if(len!=0)cout<<" ";
cout<<p[i];
len++;
if(len==10){
len=0;
cout<<endl;
}
}
}