令 P?i?? 表示第 i 个素数。现任给两个正整数 M≤N≤10?4??,请输出 P?M?? 到 P?N?? 的所有素数。
输入格式:
输入在一行中给出 M 和 N,其间以空格分隔。
输出格式:
输出从 P?M?? 到 P?N?? 的所有素数,每 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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int p[100009];
int ind=1;
int isp(int x) //判断是否为素数
{for(int j=2;j*j<=x;j++){if(x%j==0){return 0;}}return x;}
int main()
{int n,m; scanf("%d%d",&m,&n);int i,count=0;for(i=2;;i++){if(isp(i)!=0){count++;if(m<=count&&n>=count){if((count-m+1)%10==0)printf("%d\n",i);else if(count!=n)printf("%d ",i);else printf("%d",i);}}if(count>n)break;}return 0;
}