令 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
/*
* @Author: Spring Breeze
* @Date: 2021-06-29 19:48:16
* @FilePath: \algorithm\test.c
* @LastEditTime: 2022-03-29 18:47:42
*/
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
bool prime(int n)
{
if (n == 2)
return true;
for (int i = 2; i < sqrt(n) + 1; i++)
if (n % i == 0)
return false;
return true;
}
int main()
{
long M, N, max = 10e4;
while (scanf("%ld %ld", &M, &N) != EOF)
{
bool odds;
long len = 0, start = 2, putCount = 0;
while (len <= N)
{
if (prime(start))
{
len++;
if (len >= M && len <= N)
{
printf("%d", start);
putCount++;
if (putCount % 10 == 0)
printf("\n");
else if (len != N)
printf(" ");
}
}
start++;
}
}
return 0;
}