I

Give you a lot of positive integers, just to find out how many prime numbers there are.

Input  There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be less than 2.Output  For each case, print the number of prime numbers you have found out.Sample Input

3
2 3 4

Sample Output

2


这是一道素数题。最关键得是我们要通过这道题学会筛选素数的算法。(这提别重要)

毫无疑问这是一道打表题,别问我为什么?这是经验。其实这是有规律的
我把实现筛选素数的算法给出来

#include <stdio.h>
#include <math.h>
#define N 10000001
int prime[N];
int main()
{
   int i, j;
   for(i=2; i<N; i++){
       if(i%2) prime[i]=1;
       else prime[i]=0;
   }

   for(i=3; i<=sqrt(N); i++)
   {   if(prime[i]==1)
       for(j=i+i; j<N; j+=i) prime[j]=0;
   }

   for(i=2; i<100; i++)//只输出2-100内的素数
    if( prime[i]==1 )printf("%d ",i);
   printf(" ");

   return 0;
}




原文地址:https://www.cnblogs.com/damaoranran/p/8748350.html