<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Alice, Bob, and Mallory: Finding primes in parallel</title>
    <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>metasyntactics</description>
    <item>
      <title>Finding primes in parallel</title>
      <description>&lt;p&gt;&lt;a href="http://www.codethinked.com/page/About-Me.aspx"&gt;Justin Etheredge&lt;/a&gt; has been blogging about his &lt;a href="http://www.codethinked.com/post/2010/01/08/TekPubs-Mastering-LINQ-Challenge.aspx"&gt;challenge&lt;/a&gt; to find prime numbers with LINQ. He &lt;a href="http://www.codethinked.com/post/2010/01/10/The-TekPub-LINQ-Challenge-Part-2-Faster-Algorithms.aspx"&gt;later&lt;/a&gt; used &lt;code&gt;AsParallel()&lt;/code&gt; (coming in .NET 4) to speed things up and then followed that up with &lt;a href="http://www.codethinked.com/post/2010/01/12/The-TekPub-LINQ-Challenge-And-The-Sieve-Of-Eratosthenes.aspx"&gt;a post&lt;/a&gt; about using &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes"&gt;The Sieve Of Eratosthenes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As you can see in the comments of those posts I tried to speed the Sieve of Eratosthenes up by using &lt;code&gt;Parallel.For&lt;/code&gt; in the inner loop. I also tried AsParallel() in the LINQ expression but it made no difference in either case. At most it got 5% faster. I'm not sure but it could be that because SoE is very memory intense we could have a scaling issue and maybe also memory bandwidth exhaustion. This is mere speculation.&lt;/p&gt;

&lt;p&gt;I then searched for other algorithms and found &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Atkin"&gt;The Sieve of Atkin&lt;/a&gt;. It uses less memory than SoE so I thought I'd give it a try.&lt;/p&gt;

&lt;p&gt;I set the limit to 20,000,000 and then benchmarked it. It timed in on 2.48s so actually worse than the 2.2s that SoE took. Not good!
Then I added &lt;code&gt;Parallel.For&lt;/code&gt; in the loop that did most of the work and  lo and behold, it scaled! I have two cores in my machine (T7200@2.0GHz) and the average runtime went down to 1.26s. That's almost linear and surprisingly good! If you happen have a quad core (or more) and feel like trying it out then please contact me. It would be interesting to see if it scales further. &lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;28&lt;tt&gt;
&lt;/tt&gt;29&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;31&lt;tt&gt;
&lt;/tt&gt;32&lt;tt&gt;
&lt;/tt&gt;33&lt;tt&gt;
&lt;/tt&gt;34&lt;tt&gt;
&lt;/tt&gt;35&lt;tt&gt;
&lt;/tt&gt;36&lt;tt&gt;
&lt;/tt&gt;37&lt;tt&gt;
&lt;/tt&gt;38&lt;tt&gt;
&lt;/tt&gt;39&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;40&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;41&lt;tt&gt;
&lt;/tt&gt;42&lt;tt&gt;
&lt;/tt&gt;43&lt;tt&gt;
&lt;/tt&gt;44&lt;tt&gt;
&lt;/tt&gt;45&lt;tt&gt;
&lt;/tt&gt;46&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; List&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt; FindPrimesBySieveOfAtkins(&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; max)&lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;   &lt;span style="color:#666"&gt;//  var isPrime = new BitArray((int)max+1, false); &lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;   &lt;span style="color:#666"&gt;//  Can't use BitArray because of threading issues.&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; isPrime = new &lt;span style="color:#339;font-weight:bold"&gt;bool&lt;/span&gt;[max + &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;];&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; sqrt = (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)Math.Sqrt(max);&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    Parallel.For(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, sqrt, x =&amp;gt;&lt;tt&gt;
&lt;/tt&gt;    {&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; xx = x * x;&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; y = &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;; y &amp;lt;= sqrt; y++)&lt;tt&gt;
&lt;/tt&gt;        {&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; yy = y * y;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; n = &lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt; * xx + yy;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (n &amp;lt;= max &amp;amp;&amp;amp; (n % &lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt; == &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt; || n % &lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt; == &lt;span style="color:#00D;font-weight:bold"&gt;5&lt;/span&gt;))&lt;tt&gt;
&lt;/tt&gt;                isPrime[n] ^= &lt;span style="color:#038;font-weight:bold"&gt;true&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            n = &lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt; * xx + yy;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (n &amp;lt;= max &amp;amp;&amp;amp; n % &lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt; == &lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;                isPrime[n] ^= &lt;span style="color:#038;font-weight:bold"&gt;true&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            n = &lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt; * xx - yy;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (x &amp;gt; y &amp;amp;&amp;amp; n &amp;lt;= max &amp;amp;&amp;amp; n % &lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt; == &lt;span style="color:#00D;font-weight:bold"&gt;11&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;                isPrime[n] ^= &lt;span style="color:#038;font-weight:bold"&gt;true&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;        }&lt;tt&gt;
&lt;/tt&gt;    });&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; primes = new List&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt;() { &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt; };&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; n = &lt;span style="color:#00D;font-weight:bold"&gt;5&lt;/span&gt;; n &amp;lt;= sqrt; n++)&lt;tt&gt;
&lt;/tt&gt;    {&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (isPrime[n])&lt;tt&gt;
&lt;/tt&gt;        {&lt;tt&gt;
&lt;/tt&gt;            primes.Add(n);&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; nn = n * n;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; k = nn; k &amp;lt;= max; k += nn)&lt;tt&gt;
&lt;/tt&gt;                isPrime[k] = &lt;span style="color:#038;font-weight:bold"&gt;false&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;        }&lt;tt&gt;
&lt;/tt&gt;    }&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; n = sqrt + &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;; n &amp;lt;= max; n++)&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (isPrime[n])&lt;tt&gt;
&lt;/tt&gt;            primes.Add(n);&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; primes;&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;This code needs C# 4.0 to compile.
&lt;br&gt;&lt;br&gt;
&lt;font style="color:red;font-weight:bold"&gt;Edit 2010-12-14&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Dommer &lt;a href="http://stackoverflow.com/questions/1569127/c-implementation-of-the-sieve-of-atkin/2070579#2070579"&gt;found out&lt;/a&gt; that the BitArray implementation had some serious threading issues.
I had my worries about the non thread safe characteristics of BitArray but I thought that the isPrime[n] ^= true; was an atomic operation and that it didn't matter in what order bit bits was flipped would make it possible to use anyway. Not so. Changed it to a boolean array and that seems to rock the boat but of course at a much higher memory cost.
&lt;br&gt;&lt;br&gt;
&lt;font style="color:red;font-weight:bold"&gt;Edit 2010-01-20&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Indications are that this does in fact not scale very good on a quad core. It's even worse, it seems it scales good on my old T7200 but not on a dual core E6320. I don't know why but of course the shared state of the &lt;strong&gt;isPrime&lt;/strong&gt; &lt;code&gt;BitArray&lt;/code&gt; is a huge problem and maybe it could be that differences in CPU architecture (FSB speed, caches and so on) in the E6320 is an explanation. Average execution time on the E6320 was 1290ms in a single thread and 1064ms in two.&lt;/p&gt;

&lt;p&gt;If you want to try this in an older version of C# than 4.0 then check out &lt;a href="http://coding-time.blogspot.com/2008/03/implement-your-own-parallelfor-in-c.html"&gt;this post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A reader asked how I timed the executions. Here's how.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; steps = new List&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;long&lt;/span&gt;&amp;gt;();&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; watch = new Stopwatch();&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; i = &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;; i &amp;lt; &lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;; i++) &lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;    watch.Reset();&lt;tt&gt;
&lt;/tt&gt;    watch.Start();&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; primes = FindPrimesBySieveOfAtkins(&lt;span style="color:#00D;font-weight:bold"&gt;20000000&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;    watch.Stop();&lt;tt&gt;
&lt;/tt&gt;    Console.WriteLine(watch.ElapsedMilliseconds.ToString());&lt;tt&gt;
&lt;/tt&gt;    steps.Add(watch.ElapsedMilliseconds);&lt;tt&gt;
&lt;/tt&gt;}&lt;tt&gt;
&lt;/tt&gt;Console.WriteLine(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Average: &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + steps.Average().ToString());&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;font style="color:red;font-weight:bold"&gt;Edit 2010-10-24&lt;/font&gt;
&lt;br&gt;&lt;br&gt;
Tom's code from the comment below&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;28&lt;tt&gt;
&lt;/tt&gt;29&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;31&lt;tt&gt;
&lt;/tt&gt;32&lt;tt&gt;
&lt;/tt&gt;33&lt;tt&gt;
&lt;/tt&gt;34&lt;tt&gt;
&lt;/tt&gt;35&lt;tt&gt;
&lt;/tt&gt;36&lt;tt&gt;
&lt;/tt&gt;37&lt;tt&gt;
&lt;/tt&gt;38&lt;tt&gt;
&lt;/tt&gt;39&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;40&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;41&lt;tt&gt;
&lt;/tt&gt;42&lt;tt&gt;
&lt;/tt&gt;43&lt;tt&gt;
&lt;/tt&gt;44&lt;tt&gt;
&lt;/tt&gt;45&lt;tt&gt;
&lt;/tt&gt;46&lt;tt&gt;
&lt;/tt&gt;47&lt;tt&gt;
&lt;/tt&gt;48&lt;tt&gt;
&lt;/tt&gt;49&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;50&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;51&lt;tt&gt;
&lt;/tt&gt;52&lt;tt&gt;
&lt;/tt&gt;53&lt;tt&gt;
&lt;/tt&gt;54&lt;tt&gt;
&lt;/tt&gt;55&lt;tt&gt;
&lt;/tt&gt;56&lt;tt&gt;
&lt;/tt&gt;57&lt;tt&gt;
&lt;/tt&gt;58&lt;tt&gt;
&lt;/tt&gt;59&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;60&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;61&lt;tt&gt;
&lt;/tt&gt;62&lt;tt&gt;
&lt;/tt&gt;63&lt;tt&gt;
&lt;/tt&gt;64&lt;tt&gt;
&lt;/tt&gt;65&lt;tt&gt;
&lt;/tt&gt;66&lt;tt&gt;
&lt;/tt&gt;67&lt;tt&gt;
&lt;/tt&gt;68&lt;tt&gt;
&lt;/tt&gt;69&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;70&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;71&lt;tt&gt;
&lt;/tt&gt;72&lt;tt&gt;
&lt;/tt&gt;73&lt;tt&gt;
&lt;/tt&gt;74&lt;tt&gt;
&lt;/tt&gt;75&lt;tt&gt;
&lt;/tt&gt;76&lt;tt&gt;
&lt;/tt&gt;77&lt;tt&gt;
&lt;/tt&gt;78&lt;tt&gt;
&lt;/tt&gt;79&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;80&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;81&lt;tt&gt;
&lt;/tt&gt;82&lt;tt&gt;
&lt;/tt&gt;83&lt;tt&gt;
&lt;/tt&gt;84&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;using System; &lt;tt&gt;
&lt;/tt&gt;using System.Collections.Generic; &lt;tt&gt;
&lt;/tt&gt;using System.Linq; &lt;tt&gt;
&lt;/tt&gt;using System.Numerics; &lt;tt&gt;
&lt;/tt&gt;using System.Text; &lt;tt&gt;
&lt;/tt&gt;using System.Threading.Tasks;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;namespace Calculate_Primes &lt;tt&gt;
&lt;/tt&gt;{ &lt;tt&gt;
&lt;/tt&gt;    class Program &lt;tt&gt;
&lt;/tt&gt;    { &lt;tt&gt;
&lt;/tt&gt;        private &lt;span style="color:#080;font-weight:bold"&gt;const&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; _NUMBER_OF_DIGITS = &lt;span style="color:#00D;font-weight:bold"&gt;100&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;void&lt;/span&gt; Main(string[] args)&lt;tt&gt;
&lt;/tt&gt;        {&lt;tt&gt;
&lt;/tt&gt;            BigInteger floor = BigInteger.Parse(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;1&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + string.Empty.PadLeft(_NUMBER_OF_DIGITS - &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, &lt;span style="color:#04D"&gt;'0'&lt;/span&gt;));&lt;tt&gt;
&lt;/tt&gt;            BigInteger ceiling = BigInteger.Parse(string.Empty.PadLeft(_NUMBER_OF_DIGITS, &lt;span style="color:#04D"&gt;'9'&lt;/span&gt;));&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            Console.WindowWidth = &lt;span style="color:#00D;font-weight:bold"&gt;150&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#666"&gt;//var primes = Enumerable.Range(floor, ceiling).Where(n =&amp;gt; Enumerable.Range(1, n).Where(m =&amp;gt; (n / m) * m == n).Count() == 2);&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            Console.Clear();&lt;tt&gt;
&lt;/tt&gt;            _calculatePrimes(floor, ceiling, &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;C:&lt;/span&gt;&lt;span style="color:#b0b"&gt;\\&lt;/span&gt;&lt;span style=""&gt;100 digit primes.txt&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            Console.Clear();&lt;tt&gt;
&lt;/tt&gt;            _calculatePrimes(floor, ceiling, &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;C:&lt;/span&gt;&lt;span style="color:#b0b"&gt;\\&lt;/span&gt;&lt;span style=""&gt;300 digit primes.txt&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;        }&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; IEnumerable&amp;lt;BigInteger&amp;gt; Range(BigInteger fromInclusive, BigInteger toInclusive)&lt;tt&gt;
&lt;/tt&gt;        {&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (BigInteger i = fromInclusive; i &amp;lt;= toInclusive; i++)&lt;tt&gt;
&lt;/tt&gt;                yield &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; i;&lt;tt&gt;
&lt;/tt&gt;        }&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;void&lt;/span&gt; ParallelFor(BigInteger fromInclusive, BigInteger toInclusive, Action&amp;lt;BigInteger&amp;gt; body)&lt;tt&gt;
&lt;/tt&gt;        {&lt;tt&gt;
&lt;/tt&gt;            Parallel.ForEach(Range(fromInclusive, toInclusive), body);&lt;tt&gt;
&lt;/tt&gt;        } &lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;void&lt;/span&gt; _calculatePrimes(BigInteger floor, BigInteger ceiling, string resultsFileFilepath)&lt;tt&gt;
&lt;/tt&gt;        {&lt;tt&gt;
&lt;/tt&gt;            using (System.IO.FileStream fs = new System.IO.FileStream(resultsFileFilepath, System.IO.FileMode.Create)) { }&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(resultsFileFilepath))&lt;tt&gt;
&lt;/tt&gt;            {&lt;tt&gt;
&lt;/tt&gt;                ParallelFor(floor, ceiling, i =&amp;gt;&lt;tt&gt;
&lt;/tt&gt;                    {&lt;tt&gt;
&lt;/tt&gt;                        &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (_isPrime(i))&lt;tt&gt;
&lt;/tt&gt;                        {&lt;tt&gt;
&lt;/tt&gt;                            lock (sw)&lt;tt&gt;
&lt;/tt&gt;                            {&lt;tt&gt;
&lt;/tt&gt;                                sw.Write(i.ToString() + System.Environment.NewLine);&lt;tt&gt;
&lt;/tt&gt;                                sw.Flush();&lt;tt&gt;
&lt;/tt&gt;                            }&lt;tt&gt;
&lt;/tt&gt;                        }&lt;tt&gt;
&lt;/tt&gt;                    });&lt;tt&gt;
&lt;/tt&gt;            }&lt;tt&gt;
&lt;/tt&gt;        }&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;bool&lt;/span&gt; _isPrime(BigInteger number)&lt;tt&gt;
&lt;/tt&gt;        {&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#339;font-weight:bold"&gt;bool&lt;/span&gt; returnValue = &lt;span style="color:#038;font-weight:bold"&gt;true&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            Console.WriteLine(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Checking {0} for primality.&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, number.ToString());&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; ((number &amp;lt; &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;) || (number &amp;gt; &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; &amp;amp;&amp;amp; number.IsEven) || (number &amp;gt; &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; &amp;amp;&amp;amp; number.IsPowerOfTwo))&lt;tt&gt;
&lt;/tt&gt;                returnValue = &lt;span style="color:#038;font-weight:bold"&gt;false&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;else&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;                &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (BigInteger i = &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;; i * i &amp;lt;= number; i++)&lt;tt&gt;
&lt;/tt&gt;                {&lt;tt&gt;
&lt;/tt&gt;                    &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (number % i == &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;                        returnValue = &lt;span style="color:#038;font-weight:bold"&gt;false&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;                }&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt;(returnValue)&lt;tt&gt;
&lt;/tt&gt;                Console.WriteLine(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;         {0} IS prime.&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, number.ToString());&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;else&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;                Console.WriteLine(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;         {0} IS NOT prime.&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, number.ToString());&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; returnValue;&lt;tt&gt;
&lt;/tt&gt;        }&lt;tt&gt;
&lt;/tt&gt;    }&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;</description>
      <pubDate>Thu, 14 Jan 2010 22:55:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:2c932b6d-8c9c-4532-9b8f-85ea02fdd193</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel</link>
      <category>C#</category>
      <category>Math</category>
    </item>
    <item>
      <title>"Finding primes in parallel" by casque audio beats</title>
      <description>&lt;p&gt;vers le sud-ouest, aux Turones, à qui appartenaient les environs de Montrichard ; à l&amp;#8217;ouest, aux Cenomani, dont le pays a formé le département de laSarthe.&lt;/p&gt;</description>
      <pubDate>Sat, 04 Feb 2012 03:05:40 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:c3ad4ba2-2cc8-4ade-8bba-47f36896e8fe</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-7266</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Freeman</title>
      <description>&lt;p&gt;just wanted to say you can use watch.Restart() instead of     watch.Reset();
    watch.Start();&lt;/p&gt;</description>
      <pubDate>Wed, 16 Nov 2011 15:00:58 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:7984b2f2-7f37-48bd-8f99-9835699708f4</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-6723</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Dave Jellison</title>
      <description>&lt;p&gt;FWIW: 20M took 387 ms on my 6 core using the algo in this post. Rockin! Thanks for the C# implementation.&lt;/p&gt;</description>
      <pubDate>Thu, 17 Feb 2011 01:01:58 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:5c2b7d89-0c01-403e-b352-0e68129a53d6</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-5234</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Jonas Elfström</title>
      <description>&lt;p&gt;@Peter Thanks! Even though Sieve of Atkin in theory should be faster than Sieve of Eratosthenes your implementation of the latter is much faster than mine of the former. I&amp;#8217;m not surprised because I just did a naive translation from the &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Atkin"&gt;pseudo code on Wikipedia&lt;/a&gt; to C#.&lt;/p&gt;

&lt;p&gt;Also my implementation can&amp;#8217;t handle searching for primes up to 1000000000. It seems to be the line &lt;code&gt;int n = 4 * xx + yy;&lt;/code&gt; that is the problem. &lt;code&gt;4*xx+yy&lt;/code&gt; does not fit in a &lt;code&gt;Int32&lt;/code&gt; for max=1000000000.&lt;/p&gt;

&lt;pre&gt;
Peter's impl. of The Sieve Of Eratosthenes
n: 10000  primes: 1229 in 0 ms
n: 100000  primes: 9592 in 2 ms
n: 1000000  primes: 78498 in 20 ms
n: 10000000  primes: 664579 in 213 ms
n: 100000000  primes: 5761455 in 2744 ms
n: 1000000000  primes: 50847534 in 33338 ms

My impl. of The Sieve Of Atkin
n: 10000  primes: 1229 in 3 ms
n: 100000  primes: 9592 in 6 ms
n: 1000000  primes: 78498 in 64 ms
n: 10000000  primes: 664579 in 584 ms
n: 100000000  primes: 5761455 in 7612 ms
&lt;/pre&gt;</description>
      <pubDate>Wed, 22 Dec 2010 11:20:23 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:3cc82064-caa2-406a-b302-42873a922a2a</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-5199</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Peter</title>
      <description>&lt;p&gt;This implementation of the sieve of Eatosthenes should take about 500 ms on your PC finding primes upto 20*10^6&lt;/p&gt;

&lt;p&gt;Regards,&lt;/p&gt;

&lt;p&gt;Peter&lt;/p&gt;</description>
      <pubDate>Tue, 21 Dec 2010 16:47:04 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:bce812a5-ca78-4cf3-bcce-8ba5f69b368c</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-5197</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Jonas Elfström</title>
      <description>&lt;p&gt;@Tom Thanks for the code and sorry for the not so fancy commenting function on my blog. &lt;/p&gt;

&lt;p&gt;I think that you are right in that brute force prime search scales over multiple CPUs. It could be a problem that it&amp;#8217;s so terrible slow in comparance to The Sieve Of Eratosthene and The Sieve of Atkin, I just don&amp;#8217;t know. Guess I have to read up on how those gigantic primes that have been found was found.&lt;/p&gt;</description>
      <pubDate>Sun, 24 Oct 2010 00:22:29 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:bb003385-e126-4cae-a3f4-17a3923176c1</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-4403</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Jonas Elfström</title>
      <description>&lt;p&gt;@Tom&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;isPrime[n] ^= true&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An XOR toggle, nice! I haven&amp;#8217;t checked it but I would guess that both that and mine compile to something similar.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;No more atomicity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now you lost me. I believe it is and that that&amp;#8217;s a good thing.&lt;/p&gt;</description>
      <pubDate>Sun, 24 Oct 2010 00:10:50 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:d496a377-531b-4650-a55f-1d95e25d42f4</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-4402</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Tom</title>
      <description>&lt;p&gt;Here&amp;#8217;s a little something I came up with&amp;#8211;except for the commented out LINQ query&amp;#8211;using Stephen Toub&amp;#8217;s comments on Scott Hansellman&amp;#8217;s blog:&lt;/p&gt;

&lt;p&gt;using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace Calculate_Primes
{
    class Program
    {
        private const int &lt;em&gt;NUMBER&lt;/em&gt;OF_DIGITS = 100;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    static void Main(string[] args)
    {
        BigInteger floor = BigInteger.Parse("1" + string.Empty.PadLeft(_NUMBER_OF_DIGITS - 1, '0'));
        BigInteger ceiling = BigInteger.Parse(string.Empty.PadLeft(_NUMBER_OF_DIGITS, '9'));

        Console.WindowWidth = 150;

        //var primes = Enumerable.Range(floor, ceiling).Where(n =&amp;gt; Enumerable.Range(1, n).Where(m =&amp;gt; (n / m) * m == n).Count() == 2);

        Console.Clear();
        _calculatePrimes(floor, ceiling, "C:\100 digit primes.txt");

        Console.Clear();
        _calculatePrimes(floor, ceiling, "C:\300 digit primes.txt");
    }

    static IEnumerable&amp;lt;BigInteger&amp;gt; Range(BigInteger fromInclusive, BigInteger toInclusive)
    {
        for (BigInteger i = fromInclusive; i &amp;lt;= toInclusive; i++)
            yield return i;
    }

    static void ParallelFor(BigInteger fromInclusive, BigInteger toInclusive, Action&amp;lt;BigInteger&amp;gt; body)
    {
        Parallel.ForEach(Range(fromInclusive, toInclusive), body);
    } 

    static void _calculatePrimes(BigInteger floor, BigInteger ceiling, string resultsFileFilepath)
    {
        using (System.IO.FileStream fs = new System.IO.FileStream(resultsFileFilepath, System.IO.FileMode.Create)) { }

        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(resultsFileFilepath))
        {
            ParallelFor(floor, ceiling, i =&amp;gt;
                {
                    if (_isPrime(i))
                    {
                        lock (sw)
                        {
                            sw.Write(i.ToString() + System.Environment.NewLine);
                            sw.Flush();
                        }
                    }
                });
        }
    }

    static bool _isPrime(BigInteger number)
    {
        bool returnValue = true;

        Console.WriteLine("Checking {0} for primality.", number.ToString());

        if ((number &amp;lt; 2) || (number &amp;gt; 2 &amp;amp;&amp;amp; number.IsEven) || (number &amp;gt; 2 &amp;amp;&amp;amp; number.IsPowerOfTwo))
            returnValue = false;
        else
            for (BigInteger i = 2; i * i &amp;lt;= number; i++)
            {
                if (number % i == 0)
                    returnValue = false;
            }

        if(returnValue)
            Console.WriteLine("         {0} IS prime.", number.ToString());
        else
            Console.WriteLine("         {0} IS NOT prime.", number.ToString());

        return returnValue;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;}&lt;/p&gt;</description>
      <pubDate>Fri, 22 Oct 2010 18:59:47 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:de8575e4-bc06-4c84-9b30-f73c01e40bed</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-4398</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Tom</title>
      <description>&lt;p&gt;These lines:
isPrime[n] = !isPrime[n];&lt;/p&gt;

&lt;p&gt;Should be replaced with
isPrime[n] ^= true;&lt;/p&gt;

&lt;p&gt;No more atomicity.&lt;/p&gt;</description>
      <pubDate>Fri, 22 Oct 2010 18:35:54 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:79543d2a-2508-4f02-a0fc-50762ef126a8</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-4397</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Jonas Elfström</title>
      <description>&lt;p&gt;&lt;a href="http://coding-time.blogspot.com/2008/03/implement-your-own-parallelfor-in-c.html"&gt;http://coding-time.blogspot.com/2008/03/implement-your-own-parallelfor-in-c.html&lt;/a&gt; - makes it possible to run FindPrimesBySieveOfAtkins unchanged in C# 2.0-3.5.&lt;/p&gt;</description>
      <pubDate>Wed, 20 Jan 2010 09:11:22 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:d5f02f7b-3ad8-44c6-b427-17d58eec4f51</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-3939</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Svish</title>
      <description>&lt;p&gt;So should maybe use the Set method instead then? Or doesn&amp;#8217;t make much difference perhaps&amp;#8230;&lt;/p&gt;

&lt;p&gt;Thanks for the link. Will check it out :)&lt;/p&gt;</description>
      <pubDate>Tue, 19 Jan 2010 18:33:16 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:ab5f14c5-6d7f-4149-84cb-383057d2e248</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-3934</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Jonas Elfström</title>
      <description>&lt;p&gt;&lt;i&gt;&amp;#8220;I was wondering if it could be done in a nice way without using Parallel.&amp;#8221;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;Check out:  &lt;a href="http://www.codeproject.com/KB/dotnet/PoorMansParallelForEach.aspx"&gt;http://www.codeproject.com/KB/dotnet/PoorMansParallelForEach.aspx&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 16 Jan 2010 16:11:21 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:cc5562be-58a8-4d77-b9f1-bc2ff135ce61</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-3919</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Jonas Elfström</title>
      <description>&lt;p&gt;If Holterman is correct then my usage is thread safe: &lt;a href="http://stackoverflow.com/questions/1213997/is-there-a-generic-type-safe-bitarray-in-net/1214686#1214686"&gt;http://stackoverflow.com/questions/1213997/is-there-a-generic-type-safe-bitarray-in-net/1214686#1214686&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 15 Jan 2010 17:24:41 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:5c6e0ff7-7806-4f22-9995-72d7d9531954</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-3914</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Jonas Elfström</title>
      <description>&lt;p&gt;That&amp;#8217;s for &amp;#8220;long, ulong, double, and decimal&amp;#8221;.
Read/write of booleans is atomic. I&amp;#8217;m just not sure that isPrime[n] = !isPrime[n]; is the same as 
Boolean test = false;
test = !test;
which would be atomic.&lt;/p&gt;</description>
      <pubDate>Fri, 15 Jan 2010 16:23:15 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:254adce2-07ea-4bbf-adec-abf09c5dbf4c</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-3913</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Svish</title>
      <description>&lt;p&gt;From that link you posted: &amp;#8220;Aside from the library functions designed for that purpose, there is no guarantee of atomic read-modify-write, such as in the case of increment or decrement.&amp;#8221; &amp;#8211; Wouldn&amp;#8217;t that mean that it is not thread-safe? or?&lt;/p&gt;</description>
      <pubDate>Fri, 15 Jan 2010 15:50:13 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:0fbcedf0-41f7-48b3-a761-e72922250f46</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-3912</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Svish</title>
      <description>&lt;p&gt;But that would remove the parallelism :p I was wondering if it could be done in a nice way without using Parallel.For, but still have the parallelism. (So do whatever Parallel.For does yourself)&lt;/p&gt;</description>
      <pubDate>Fri, 15 Jan 2010 15:47:57 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:e3c5ceb0-c341-430d-be76-77eccded5be8</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-3911</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Jonas Elfström</title>
      <description>&lt;p&gt;Replace the Parallel.For with
for (int x = 1; x &amp;lt;= sqrt; x++)
and remove ); from row 24 and you should be good to go.&lt;/p&gt;

&lt;p&gt;If I understand 
&lt;a href="http://bit.ly/8lZagW"&gt;http://bit.ly/8lZagW&lt;/a&gt; correctly the isPrime[n] = !isPrime[n]; is an atomic operation but I have to investigate the matter of thread safety further. Thanks!&lt;/p&gt;</description>
      <pubDate>Fri, 15 Jan 2010 13:33:16 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:83d5ec5d-e28b-4bde-9d11-5c211ea3d7fb</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-3909</link>
    </item>
    <item>
      <title>"Finding primes in parallel" by Svish</title>
      <description>&lt;p&gt;Are BitArrays thread-safe, or how does that work?&lt;/p&gt;

&lt;p&gt;Is it possible to write a version of this without the Parrallel.For method? Well, I suppose it is possible of course, but would it be a big mess? :p&lt;/p&gt;</description>
      <pubDate>Fri, 15 Jan 2010 11:03:18 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:16e6e69c-cc7a-47ee-92b3-8a2a7904f05f</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel#comment-3908</link>
    </item>
  </channel>
</rss>

