Problem 1003
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 320 Solved: 37
[Submit][Status][Web Board]
Description
用筛法求之N内的素数。(N < 1000000)
Input
N (多个测试案例)
Output
0~N的素数
Sample Input
100
Sample Output
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 320 Solved: 37
[Submit][Status][Web Board]
Description
用筛法求之N内的素数。(N < 1000000)
Input
N (多个测试案例)
Output
0~N的素数
Sample Input
100
Sample Output
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
标程:
#include <stdio.h>
#include <math.h>
#define MAX 10000009
int num[MAX];
int main ()
{
//freopen("out.txt","w",stdout);
int i, j;
double n;
while(scanf ("%lf",&n)!=EOF)
{
for (i=2; i<=n; i++)
{
if (i % 2 == 0)
num[i] = 0;
else
num[i] = 1;
}
for (i=3; i<=sqrt(n); i+=2)
{
if (num[i])
for (j=i+i; j<=n; j+=i)
num[j] = 0;
}
printf ("2\n");
for (i=2; i<=n; i++)
if (num[i] == 1)
printf ("%d\n",i);
}
return 0;
}