判断素数,有一处不是很明白
#include <stdio.h>#include <math.h>
#define YES 1
#define NO 0
int isprime(int num)
{
int i, j;
if (num == 2)
return YES;
else if (num < 2 || num % 2 == 0)
return NO;
else
{
j = (int)sqrt(num + 1);
for (i = 3; i <= j; i = i + 2)/*这句。。。如果我输入的数据是4.求个解释,这个程序的步骤怎么运行。*/
if (num % i == 0)
return NO;
}
return YES;
}
int main(int argc, char *argv[])
{
int N;
scanf("%d", &N);
if (isprime(N) == YES)
printf("yes\n");
else
printf("no\n");
return 0;
}