【发布时间】:2012-08-01 06:41:01
【问题描述】:
我在处理这个 C++ 代码时遇到了问题。整数num 是一个我想检查它是否为素数的数字。然而,这个程序总是返回 false。这可能很简单,但我找不到任何东西。
for(int i=2;i<num;i++){ //primes are allowed to be divided by 1 so we start at 2
if(num % i == 0){ //can be divided by a number other than itself or 1 so we trip out
return false;
} else if(i == num){ //if we've already done checks as high as possible and not tripped out yet then report success
return true;
}
}
【问题讨论】: