1013. 数素数 (20)
令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
//104729是第10000个素数
#include <iostream>
//#include <vector>
#include <math.h>
using namespace std;
int main()
{
int Odd[10000] = {0};
int index = 0;
for(int i = 2; i != 104730; i++)
{
bool flag = true;
for(int j = 2; j <= int(sqrt(i*1.0)); j++) //"j != i",程序运行超时(0分)
{
if(0 == i%j)
{
flag = false;
break; //没有"break",程序运行超时(0分)
}
}
if(flag)
{
Odd[index] = i;
index++;
}
}
int n,m;
cin >> m >> n;
int count = 0;
for(int i = m-1; i != n; i++)
{
count++;
if(1 == count)
cout << Odd[i];
else
cout << ' ' << Odd[i];
if(10 == count)
{
cout << endl;
count = 0;
}
}
if(0 != count)
cout << endl;
system("pause");
return 0;
}