微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从数字列表中打印素数,例如1到1000

#include <stdio.h>
#include <math.h>
#define SIZE 100

int status[SIZE];

void sieve()
{
     int i, j, sq;
     for(i = 0; i < SIZE; i++) {
         status[i] = 0;
     }

     sq = sqrt(SIZE);

     for(i=4;i<=SIZE;i+=2) {
        status[i] = 1;
     }

     for(i = 3; i <= sq; i += 2)
     {
         if(status[i] == 0){
              for(j = 2*i; j <= SIZE; j += i)
                 status[j] = 1;
         }
     }
     status[1] = 1;
}

int main(){
     int i, counter = 100;
     sieve();
     printf(\nFollowing numbers are prime in the range: 1 to %d :\n, counter);
     for (i = 1; i < counter; i++){
       if(status[i]==0) {
          printf(%d\t, i);
       }
     }

     return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐