筛选法求质数

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <cmath>
 4 #include <string.h>
 5 using namespace std;
 6 bool f[1000005];
 7 int main()
 8 {
 9   int n;
10   scanf("%d",&n);
11   f[1]=false;
12   for(int i=2;i<=n;i++)
13   f[i]=true;
14   for(int i=2;i<=n;i++)
15   {
16     if(f[i]==true)
17     {
18       for(int j=2;j*i<=n;j++)
19       f[i*j]=false;
20     }
21   }
22   for(int i=2;i<=n;i++)
23   if(f[i])
24   printf("%d ",i);
25 }
原文地址:https://www.cnblogs.com/hi3254014978/p/10843475.html