# 线性筛法求质数

 #include #include #define MAX_COUNT 10000 #define NEXT(x) x = next[x] #define PREV(x) x = prev[x] #define REMOVE(x) { next[prev[x]] = next[x]; \                              prev[next[x]] = prev[x];} #define INIT(n) { unsigned long i; \                         for(i = 2; i <= n; i++){ \                              next[i] = i + 1; \                              prev[i] = i - 1; \                       } \                        prev[2] = next[n] = NULL; \                       }   int main(int argc, char** argv) {     unsigned long num, count;     unsigned long prime, factor, multi;      unsigned long next[MAX_COUNT + 1];     unsigned long prev[MAX_COUNT - 1];         printf("%s", "please input the upper num\n");     scanf("%d", &num);       INIT(num);         for(prime = 2; prime * prime <= num; NEXT(prime))     {         for(factor = prime; prime * factor <= num; NEXT(factor))         {             for(multi = prime * factor; multi <= num; multi *= prime)             {                 REMOVE(multi);             }         }     }     for(prime = 2, count = 0; prime != NULL; NEXT(prime))     {          if(count++ % 8 == 0)          {               printf("\n");          }          printf("%8ld", prime);      }      return 0; }
posted @ 2012-12-08 10:18  行者酱油君  阅读(653)  评论(0编辑  收藏  举报