题目描写叙述
令Pi表示第i个素数。
现任给两个正整数M <= N <= 10000,请输出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 <iostream>
using namespace std;
int main()
{
int M;
int N;
cin >> M >> N;
int *Snum = new int[11000];
int k = 0;
int i;
int j;
Snum[k++] = 2;
for (i = 3; i <= 110000; i++)
{
for (j = 0; j*j < k; j++)
{
if (i%Snum[j] == 0)break;
}
if (j*j >= k)
{
Snum[k++] = i;
}
}
int count = 0;
for (i = M-1; i < N; i++)
{
count++;
if (count % 10 == 0)
{
cout <<Snum[i]<<endl;
continue;
}
if (i == N - 1)
{
cout << Snum[i] << endl;
break;
}
cout << Snum[i] << " ";
}
return 0;
}