I have been given the code below, it passes a number to check if it is prime. I do not understand what floor(sqrt(toCheck)) +1
does or what the prb
variable does. I think it runs the while loop while the bool noFactorFound
is true and prb is less than searchLimit
. Everything would fall into place if I knew how searchLimit was initialised.
#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);
}