I've got a static List<long> primes
of all known primes up to a certain point, and a function like this:
static bool isPrime(long p)
{
double rootP = Math.Sqrt(p);
foreach (long q in primes)
{
if (p % q == 0)
return false;
if (q >= rootP)
return true;
}
return true;
}
which could be parallelised like this:
static bool isPrime(long p)
{
double rootP = Math.Sqrt(p);
primes.AsParallel().ForAll(q =>
{
if (p % q == 0)
return false;
if (q > rootP)
break;
});
return true;
}
However, this gives a compile-time error saying some return types in my block aren't implicitly convertible to the delegate return type.
I'm somewhat new to LINQ, especially PLINQ. This, to me, seems like a good candidate for parallelism, since the checking of each known prime against the candidate prime is an independent process.
Is there an easy way to fix my block so that it works, or do I need to attack this problem in a completely different way?