This function is supposed to take in three parameters and find the prime numbers less than the NUM inputed using trial division
if &&7 = '-'
update MIN_ESSENTIAL_COV_IN with its value ( so, do not update)
else
update MIN_ESSENTIAL_COV_IN to &&7
Then do trial division to find prime numbers.
I am having a problem figuring out how to put numbers less than sqrt(NUM) into a function.
Thanks
答案 0 :(得分:0)
First, you're going to want to create a loop that goes from Total = Decimal.Round(z.Sum(l => l.Total), 0);
to i=2
. This is to check every positive integer greater than 1 up to NUM-1 for primeness. It would look something like this:
i<NUM
Now for the actual prime checking. Let's put this in a function. This is where you would use for (int i=2;i<NUM;i++)
{
// prime checking
}
to define the upper bound of the loop. Once again we start our loop at sqrt()
because all numbers are divisible by 1, so there's no need to include that.
j=2
Basically, if the loop terminates, then we have not found any factors of bool isPrime(int i)
{
for (int j=2;j<sqrt(i);j++)
{
if (i%j==0)
{
return false;
}
}
return true;
}
between i
and 2
, so it is prime.
Now simply call this function in your first loop.
sqrt(i)
If you wanted just a list of primes, then for (int i=2;i<NUM;i++)
{
if (isPrime(i))
{
prime[i] = 1; // or whatever you want to use to represent a prime. If you use 1, it's best if you initiate the array with 0's
}
}
would be a better choice for storing your results.