Description
用篩法求N(<1000)之内的素數。
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
HINT
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int m,i,k,n;
cin>>n;
for(m=2;m<=n;m++)
{
k=sqrt(m);
for(i=2;i<=k;i++)
if(m%i==0)
break;
if(i>k)
cout<<m<<endl;
}
}
OJ輸出n以内的所有素數
Description
用篩法求N(<1000)之内的素數。
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
HINT
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int m,i,k,n;
cin>>n;
for(m=2;m<=n;m++)
{
k=sqrt(m);
for(i=2;i<=k;i++)
if(m%i==0)
break;
if(i>k)
cout<<m<<endl;
}
}
