title: .Net Parallel.For strange behavior
body: <p>I brute-forced summing of all primes under 2000000. After that, just for fun I tried to parallel my for, but I was a little bit surprised when I saw that <code>Parallel.For</code> gives me an <strong>incorrect</strong> sum!</p>
  
  <p>Here's my code : (C#)</p>
  
  <pre><code>static class Problem
  {
      public static long Solution()
      {
          long sum = 0;
          //Correct result is 142913828922
          //Parallel.For(2, 2000000, i =&gt;
          //                             {
          //                                 if (IsPrime(i)) sum += i;
          //                             });
          for (int i = 2; i &lt; 2000000; i++)
          {
              if (IsPrime(i)) sum += i;
          }
          return sum;
      }
      private static bool IsPrime(int value)
      {
          for (int i = 2; i &lt;= (int)Math.Sqrt(value); i++)
          {
              if (value % i == 0) return false;
          }
          return true;
      }
  }
  </code></pre>
  
  <p>I know that brute-force is pretty bad solution here but that is not a question about that. I think I've made some very stupid mistake, but I just can't locate it. So, <code>for</code> is calculating correctly, but <code>Parallel.For</code> is not.</p>