判断一个数是否是素数(质数)

素数这个概念不用强调了吧;直接上代码吧

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <math.h>
 4 //判断是否为素数
 5 void judge(){
 6     int a;
 7     while(scanf("%d",&a)!=EOF){
 8         bool tag=true;
 9         if (a<=1) {
10             tag = false;
11         }else{
12         int bound = (int)sqrt(a)+1;//求根后取整再加1,多枚举一个数
13         for(int i = 2;i<bound;i++){
14             if (a%i==0) {
15                 tag=false;
16                 break;
17             }
18         }
19         }
20         puts(tag?"yes":"no");
21     }
22 }
23 int main() {
24  
25     judge();
26     return 0;
27 }

 

posted @ 2017-03-03 16:51  小小范同学  阅读(385)  评论(0编辑  收藏  举报