素数判断改进(三种)P36

*11111*

#include

int determine(int number) { int result = 1; int control; if (number == 1 || (number != 2 && number%2 == 0)) { result = 0; } else { for (control = 3;control<number;control+=2) { if (number %control == 0) { result = 0; break; } } }

return result;

}

int main (void) { int number; scanf("%d",&number); if (determine(number)) { printf("是素数"); } else { printf("不是素数"); }

return 0;

}

*22222222**

#include

int determine(int number) { int result = 1; int control; if (number == 1) result = 0; for (control =2;control <number;control++) { if (number %control == 0) { result = 0; break; } }

return result;

}

int main (void) { int number; scanf("%d",&number); if (determine(number)) { printf("是素数"); } else { printf("不是素数"); }

return 0;

} *333333**

#include

int main(void) { int number; scanf("%d",&number); int shuzu[number]; //数组初始为1,即都是素数 for (int control = 2;control < number;control++) { shuzu[control] = 1; } shuzu[1] = 0;//确定1为非素数 //找出非素数 int x = 2; while (x < number/2 ) { int a = 2; while (ax <number) { shuzu[ax] = 0; a++; } do { x++; } while ( shuzu[x] == 0); }

int count = 0;
for (int control = 1; control < number;control++) {
    if (shuzu[control]) {
        printf("%d",control);
        count++;
        if (count % 5 == 0) {
        printf("\r");
        } else {
        printf("\t");
        }
    }
}

return 0;

}

JSRUN前端笔记, 是针对前端工程师开放的一个笔记分享平台,是前端工程师记录重点、分享经验的一个笔记本。JSRUN前端采用的 MarkDown 语法 (极客专用语法), 这里属于IT工程师。