我已经给出了下面的代码,它传递一个数字来检查它是否是素数。我不明白什么floor(sqrt(toCheck)) +1
做什么或prb
变量做什么。我认为它运行while循环,而bool noFactorFound
为true,prb小于searchLimit
。如果我知道searchLimit是如何初始化的,那么一切都会落空。寻找一个素数的代码C++
#include<cmath> //for floor() & sqrt()
#include "prime.h"
bool isPrime(unsigned toCheck)
{
if (toCheck== 2)
return true;
if ((toCheck % 2) == 0)
return false;
unsigned prb = 3;
unsigned searchLimit = floor(sqrt(toCheck)) + 1;
bool noFactorFound = true;
while (noFactorFound && (prb<searchLimit))
{
if ((toCheck % prb) == 0)
noFactorFound = false;
else
prb += 2;
}
return (noFactorFound);
}
[sqrt()](http://en.cppreference.com/w/cpp/numeric/math/sqrt),[floor()](http://en.cppreference.com/w/cpp/)数字/数学/楼) –