Pregunta

I am learning PLINQ. I tried on example given on a site. However I can see that the result is wrong and also varies when I re-run the program many times. First it does not gives all prime numbers and second it gives only 9591 random prime numbers.

IEnumerable<int> numbers = Enumerable.Range (3, 100000-3);

var parallelQuery = 
  from n in numbers.AsParallel()
  where Enumerable.Range (2, (int) Math.Sqrt (n)).All (i => n % i > 0)
  select n;

int[] primes = parallelQuery.ToArray();

Appreciate any help!

¿Fue útil?

Solución

There are 9592 prime numbers below 100,000, and your range does not include 2, which is a prime number.

The order of the prime numbers will vary, because you do not sort them, and have no control over how PLINQ splits the work into parallel chunks.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top