<?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: Category Math</title>
    <link>http://www.alicebobandmallory.com/articles/category/math</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>metasyntactics</description>
    <item>
      <title>Inspired by randomness</title>
      <description>&lt;p&gt;Inspired by &lt;a href="http://vanillajava.blogspot.com/2011/10/randomly-no-so-random.html"&gt;Randomly not so random&lt;/a&gt; I decided to play around with the subject.&lt;/p&gt;

&lt;p&gt;Both Java and C# uses a &lt;a href="http://en.wikipedia.org/wiki/Linear_congruential_generator"&gt;linear congruential generator&lt;/a&gt; as their &lt;a href="http://en.wikipedia.org/wiki/Pseudorandom_number_generator"&gt;pseudorandom number generators&lt;/a&gt;. I found &lt;a href="http://msdn.microsoft.com/en-us/library/system.random.aspx"&gt;this&lt;/a&gt;
 at MSDN &lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;The current implementation of the Random class is based on a modified version of Donald E. Knuth's subtractive random number generator algorithm."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;but I couldn't find any information on what values Microsoft uses for the &lt;em&gt;m&lt;/em&gt;, &lt;em&gt;a&lt;/em&gt;, and &lt;em&gt;c&lt;/em&gt; parameters in their &lt;a href="http://en.wikipedia.org/wiki/Linear_congruential_generator"&gt;LGC&lt;/a&gt; implementation.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;The German Federal Office for Information Security (BSI) has established four criteria for quality of deterministic random number generators. They are summarized here:
    K1 — A sequence of random numbers with a low probability of containing identical consecutive elements.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From the &lt;a href="http://en.wikipedia.org/wiki/Pseudorandom_number_generator"&gt;pseudorandom number generator&lt;/a&gt; article on Wikipedia. Let's see what we can do about that.&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;&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; random = new Random(&lt;span style="color:#00D;font-weight:bold"&gt;116793166&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; 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;9&lt;/span&gt;; i++)&lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;    Console.Write(random.Next(&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;) + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;&lt;/p&gt;

&lt;p&gt;Outputs:&lt;/p&gt;

&lt;pre&gt;
1 1 1 1 1 1 1 1 1
&lt;/pre&gt;

&lt;p&gt;Did I just break the K1 criteria above? You know I didn't but you might be interested in how I found the seed number. It's easy but also quite computational intensive, that's why I used &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.for.aspx"&gt;Parallel.For&lt;/a&gt;.&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;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;Parallel.For(&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;, &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;.MaxValue, i =&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; rnd = new Random(i);&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;bool&lt;/span&gt; allSame = &lt;span style="color:#038;font-weight:bold"&gt;true&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; j = &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;; j &amp;lt; &lt;span style="color:#00D;font-weight:bold"&gt;9&lt;/span&gt;; j++)&lt;tt&gt;
&lt;/tt&gt;    {&lt;tt&gt;
&lt;/tt&gt;        allSame = allSame &amp;amp;&amp;amp; rnd.Next(&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;) == &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (!allSame) &lt;span style="color:#080;font-weight:bold"&gt;break&lt;/span&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; (allSame)&lt;tt&gt;
&lt;/tt&gt;        Console.WriteLine(i); &lt;tt&gt;
&lt;/tt&gt;});&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
It took a couple of minutes for the number 116793166 to show up in my console.&lt;/p&gt;

&lt;p&gt;To keep on playing I needed a random string generator. Mine looks like this.&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;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;private &lt;span style="color:#080;font-weight:bold"&gt;const&lt;/span&gt; string _chars = &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;abcdefghijklmnopqrstuvwxyz&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;private &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; string RandomString(&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; size, Random rng)&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; buffer = new &lt;span style="color:#339;font-weight:bold"&gt;char&lt;/span&gt;[size];&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; size; i++)&lt;tt&gt;
&lt;/tt&gt;        buffer[i] = _chars[rng.Next(_chars.Length)];&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; new string(buffer);&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
If the random generators were the same it looks like it should give the same result as &lt;a href="http://vanillajava.blogspot.com/2011/10/randomly-no-so-random.html"&gt;the Java one&lt;/a&gt; but &lt;code&gt;RandomString(5, new Random(-229985452))&lt;/code&gt; returns &lt;code&gt;tfsld&lt;/code&gt;. Either my RandomString method works different than the Java one or it's the case that Java and .NET has slightly different settings of their LGCs.&lt;/p&gt;

&lt;p&gt;Mathematically there's an infinite amount of inputs that results in a returned &lt;code&gt;hello&lt;/code&gt;, for instance 1382472294, but here we are limited by the size of our integers.&lt;/p&gt;

&lt;p&gt;I found the seed 1382472294 with the following little method and loop&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;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;private &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; CompareToRandomString(string str, Random rng) &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; str.Length; 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; (_chars[rng.Next(_chars.Length)] != str[i])&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; &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;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; &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;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;const&lt;/span&gt; string lookingFor = &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;hello&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;Parallel.For(&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;, &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;.MaxValue, i =&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; rnd = new Random(i);&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (CompareToRandomString(lookingFor, rnd))&lt;tt&gt;
&lt;/tt&gt;        Console.WriteLine(i);&lt;tt&gt;
&lt;/tt&gt;});&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
I leave it up to you to implement a &lt;a href="http://msdn.microsoft.com/en-us/library/dd460721.aspx"&gt;break&lt;/a&gt; out of that loop.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.ruby-doc.org/core-1.9.2/Random.html"&gt;Ruby&lt;/a&gt; does not use a LGC but a &lt;a href="http://en.wikipedia.org/wiki/Mersenne_twister"&gt;Mersenne twister&lt;/a&gt; instead. It's still only pseudorandom so there's no problem finding patters you like and being able to repeat them.&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;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;srand(&lt;span style="color:#00D;font-weight:bold"&gt;2570940381&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#00D;font-weight:bold"&gt;9&lt;/span&gt;.times { print &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;%d &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; % rand(&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;) }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Outputs:&lt;br/&gt;
&lt;code&gt;1 2 3 4 5 6 7 8 9&lt;/code&gt;&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;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;charset=(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;a&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;..&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;z&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;).to_a&lt;tt&gt;
&lt;/tt&gt;srand(&lt;span style="color:#00D;font-weight:bold"&gt;124931&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;puts (&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;...&lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt;).map{ charset.to_a[rand(charset.size)] }.join&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Outputs:&lt;br/&gt;
&lt;code&gt;ruby&lt;/code&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 27 Oct 2011 23:19:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:f596c241-0004-4935-ba79-64693f890cb2</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2011/10/27/inspired-by-randomness</link>
      <category>Ruby</category>
      <category>C#</category>
      <category>Math</category>
      <category>Java</category>
    </item>
    <item>
      <title>A strict directed graph</title>
      <description>&lt;p&gt;Recently N. asked a question on a list that made my geek-senses tingle. He had a test with 12 levels of questions. The testee should answer 10 questions. If a question was answered correctly the level would go up and if it was not correct it would go down. If a level 12 was answered correctly, another level 12 would be asked and vice versa. The first question would be a level 6. N. wanted to know how many questions of each level he needed.&lt;/p&gt;

&lt;p&gt;Pretty soon he got a correct answer and several wrong. One of the incorrect answers was, and I don't like to admit this, provided by me. I took the recursive route (which I very seldom do) and then tried to present the result in ASCII. I failed. The result from the algorithm in itself was correct but the presentation was simplified so much it failed to deliver the correct answer. Here it is:&lt;pre&gt;
             06
            05 07
          04 06 08
         03 05 07 09
       02 04 06 08 10
      01 03 05 07 09 11
    01 02 04 06 08 10 12
   01 02 03 05 07 09 11 12
 01 02 03 04 06 08 10 11 12
01 02 03 04 05 07 09 10 11 12
&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;The problem is that this result can be read as if the testee could get two level 2 questions in a row but the rules forbids that.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://plea.se/g.png" target="_blank"&gt;&lt;img title="Click to show full size." src="http://plea.se/me/pics/RootedBinaryDirectedAcyclicGraph.png" style="display: inline-block; float:right" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then I remembered reading about &lt;a href="http://www.graphviz.org/"&gt;Graphviz&lt;/a&gt; and especially &lt;a href="http://github.com/glejeune/Ruby-Graphviz/"&gt;Ruby-Graphviz&lt;/a&gt;. That's what I used to generate the graph to the right. The graph itself is nothing but a visualization of all the possible question sequences. To find the answer that N. was looking for you can follow a level from top to bottom and count the number of times it can occur. I have to admit that it's far from the most convenient way but it works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The result&lt;/strong&gt;&lt;br/&gt;
5 of level 1, 5, 6 &amp;amp; 7.&lt;br/&gt;
4 of level 3, 4, 8, 9  &amp;amp; 12.&lt;br/&gt;
3 of level 2, 10 &amp;amp; 11.&lt;/p&gt;

&lt;p&gt;I had to jump a few hurdles to get there. First my old Ubuntu-install, for unknown reasons, had some ancient version of &lt;code&gt;libfreetype&lt;/code&gt; in &lt;code&gt;/usr/local/lib/&lt;/code&gt; that I had to remove. In the end an &lt;code&gt;rm /usr/local/lib/libfreetype*&lt;/code&gt; would have been good. I also did a &lt;code&gt;sudo ldconfig&lt;/code&gt; but I'm not sure that was necessary. Let's just hope you're on a modern system where a &lt;code&gt;sudo apt-get install graphviz&lt;/code&gt; followed by &lt;code&gt;sudo gem install ruby-graphviz&lt;/code&gt; will be enough.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://gist.github.com/639589"&gt;recursive code&lt;/a&gt; I wrote produces every single path the testee can take. A graph of that would be quite big since it's 1023 nodes. I then &lt;a href="http://www.graphviz.org/Documentation.php"&gt;found out&lt;/a&gt; about &lt;em&gt;strict digraph&lt;/em&gt; in the Graphviz &lt;a href="http://en.wikipedia.org/wiki/DOT_language"&gt;DOT language&lt;/a&gt;. It sounded like a perfect fit and it was! One problem though, Ruby-Graphviz didn't seem to know about it. The power of open source software let me patch &lt;code&gt;ruby-graphviz-0.9.18/lib/graphviz/constants.rb&lt;/code&gt; so that &lt;code&gt;GRAPHTYPE&lt;/code&gt; included it&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;&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:#666"&gt;## Const: graphs type&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt; &lt;span style="color:#036;font-weight:bold"&gt;GRAPHTYPE&lt;/span&gt; = [&lt;tt&gt;
&lt;/tt&gt;   &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;digraph&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;graph&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;strict digraph&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt; ]&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
Another awesome power of open source is &lt;a href="http://algorithmique.net/about.html"&gt;Grégoire Lejeune&lt;/a&gt; because only hours after me contacting him he &lt;a href="http://github.com/glejeune/Ruby-Graphviz/commit/b8d10d28e0c2a58e57bab4fe9c859b114e02e8a8"&gt;added it&lt;/a&gt; to the GitHub repository.&lt;/p&gt;

&lt;p&gt;Here's the code that produced the Rooted Binary &lt;a href="http://en.wikipedia.org/wiki/Directed_acyclic_graph"&gt;Directed Acyclic Graph&lt;/a&gt; (or is it Rooted Directed Binary Acyclic Graph?).&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;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;require &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;rubygems&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;require &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;graphviz&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@min_level&lt;/span&gt;=&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@max_level&lt;/span&gt;=&lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@max_depth&lt;/span&gt;=&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;start_level=&lt;span style="color:#00D;font-weight:bold"&gt;6&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@g&lt;/span&gt; = &lt;span style="color:#036;font-weight:bold"&gt;GraphViz&lt;/span&gt;.new(&lt;span style="color:#A60"&gt;:G&lt;/span&gt;, &lt;span style="color:#A60"&gt;:type&lt;/span&gt; =&amp;gt; &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;strict digraph&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;span style="color:#080;font-weight:bold"&gt;def&lt;/span&gt; &lt;span style="color:#06B;font-weight:bold"&gt;add_node&lt;/span&gt;(level, depth, parent)&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; depth&amp;lt;&lt;span style="color:#33B"&gt;@max_depth&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    current=[level, depth].join(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;,&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;    sub=level&amp;lt;=&amp;gt;&lt;span style="color:#33B"&gt;@min_level&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    add=&lt;span style="color:#33B"&gt;@max_level&lt;/span&gt;&amp;lt;=&amp;gt;level&lt;tt&gt;
&lt;/tt&gt;    add_node(level-sub, depth+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, current)&lt;tt&gt;
&lt;/tt&gt;    add_node(level+add, depth+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, current)&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#33B"&gt;@g&lt;/span&gt;.add_node(current).label=level.to_s&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#33B"&gt;@g&lt;/span&gt;.add_edge(parent, current) &lt;span style="color:#080;font-weight:bold"&gt;unless&lt;/span&gt; parent==&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;00&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;add_node(start_level, &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;, &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;00&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;span style="color:#33B"&gt;@g&lt;/span&gt;.output( &lt;span style="color:#A60"&gt;:png&lt;/span&gt; =&amp;gt; &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;graph.png&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; )&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br&gt;
With the current parameters this problem might not be the most exciting but I believe that by just modifying them slightly we would, again, reach a wall of complexity.&lt;/p&gt;</description>
      <pubDate>Sun, 24 Oct 2010 00:39:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:c534b2b5-4895-4979-af10-6673ee6810b9</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2010/10/24/a-strict-directed-graph</link>
      <category>Ruby</category>
      <category>Math</category>
    </item>
    <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>Infinite ranges in C#</title>
      <description>&lt;p&gt;I &lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/10/15/as-timeless-as-infinity.aspx"&gt;recently learned&lt;/a&gt; that C# is compliant with the &lt;a href="http://en.wikipedia.org/wiki/IEEE_754-1985"&gt;IEEE 754-1985&lt;/a&gt; for floating point arithmetics. That wasn't a big surprise but that &lt;a href="http://en.wikipedia.org/wiki/Division_by_zero#In_computer_arithmetic"&gt;division by zero&lt;/a&gt; is defined as &lt;code&gt;Infinity&lt;/code&gt; in it was! It actually kind of bothers me that I didn't know this.&lt;/p&gt;

&lt;p&gt;In mathematics division by zero is &lt;a href="http://en.wikipedia.org/wiki/Defined_and_undefined"&gt;undefined&lt;/a&gt; for real numbers but I guess &lt;code&gt;Infinity&lt;/code&gt; is a more pragmatic result. Or as a friend put it &lt;em&gt;"IEEE stands for Institute of Electrical and Electronics Engineers not Institute of Mathematics"&lt;/em&gt;&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;&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;double&lt;/span&gt; n = &lt;span style="color:#60E;font-weight:bold"&gt;1&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;n = n / &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (n &amp;gt; &lt;span style="color:#00D;font-weight:bold"&gt;636413622384679305&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;  System.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;Yes it certainly is!&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
This C# code does not throw an exception, it simply leaves n defined as Infinity and writes a line to the console.&lt;/p&gt;

&lt;p&gt;Ruby is &lt;a href="http://weblog.jamisbuck.org/2007/2/7/infinity"&gt;also&lt;/a&gt; IEEE 754-1985 compliant. It even lets you define &lt;a href="http://banisterfiend.wordpress.com/2009/10/02/wtf-infinite-ranges-in-ruby/"&gt;infinite ranges&lt;/a&gt;.&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;&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:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;=&lt;span style="color:#60E;font-weight:bold"&gt;1.0&lt;/span&gt;/&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;=&amp;gt;&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;..&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;).include?(&lt;span style="color:#00D;font-weight:bold"&gt;162259276829213363391578010288127&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; &lt;span style="color:#038;font-weight:bold"&gt;true&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;(&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;..&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;).step(&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;).take(&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;).inject(&amp;amp;&lt;span style="color:#A60"&gt;:+&lt;/span&gt;) &lt;span style="color:#666"&gt;# 7+14+21&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; &lt;span style="color:#00D;font-weight:bold"&gt;42&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
I can't say I see very much &lt;a href="http://www.michaelharrison.ws/weblog/?p=163"&gt;use&lt;/a&gt; of this but it brings a kind of completeness to the handling of infinities. Unfortunately it seems we don't get that in C# out of the box because &lt;code&gt;Enumerable.Range&lt;/code&gt; takes &lt;code&gt;&amp;lt;int&amp;gt;,&amp;lt;int&amp;gt;&lt;/code&gt; as parameters and there's no &lt;code&gt;Infinity&lt;/code&gt; definition for &lt;code&gt;int&lt;/code&gt;.  That's unless someone wrote a generic Range class. Turns out &lt;a href="http://stackoverflow.com/users/22656/jon-skeet"&gt;none other&lt;/a&gt; than &lt;a href="http://www.yoda.arachsys.com/csharp/"&gt;Jon Skeet&lt;/a&gt; did in his &lt;a href="http://www.yoda.arachsys.com/csharp/miscutil/"&gt;MiscUtil&lt;/a&gt;. Download MiscUtil and then by &lt;code&gt;using MiscUtil.Collections;&lt;/code&gt; you can:&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;&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;double&lt;/span&gt; n = &lt;span style="color:#60E;font-weight:bold"&gt;1&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; infinity = n / &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; r = new Range&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt;&amp;gt;(&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;, infinity);&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (r.Contains(&lt;span style="color:#00D;font-weight:bold"&gt;4711&lt;/span&gt;))&lt;tt&gt;
&lt;/tt&gt;  System.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;Yes it certainly does!&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; sum = r.Step(&lt;span style="color:#60E;font-weight:bold"&gt;7&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;).Take(&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;).Sum();&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
And guess what, it works like a charm! &lt;code&gt;4711&lt;/code&gt; is part of positive infinity and &lt;code&gt;sum&lt;/code&gt; is 42.0 and all is good.&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;There's also a couple of predefined constants. Thanks to Eric for pointing that out.&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;&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; r = new Range&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt;&amp;gt;(&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;,  System.Double.PositiveInfinity);&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; sum = r.Step(&lt;span style="color:#60E;font-weight:bold"&gt;7&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;).Take(&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;).Sum();&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 20 Oct 2009 20:41:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:1337fd12-5058-4b35-bbf4-c9d0c4e9ade8</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2009/10/20/infinite-ranges-in-c</link>
      <category>Ruby</category>
      <category>C#</category>
      <category>Math</category>
    </item>
    <item>
      <title>Sierpinski's shoes</title>
      <description>&lt;p&gt;There were no cross-platform windowing toolkits for Ruby so &lt;a href="http://en.wikipedia.org/wiki/Why_the_lucky_stiff"&gt;_why&lt;/a&gt; made one and he calls it &lt;a href="http://code.whytheluckystiff.net/shoes/"&gt;Shoes&lt;/a&gt;. Not even close to 1.0, it's already yummy in a chunky kind of way and since it came from _why I simply had to try it out. Something simple.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="constant"&gt;Shoes&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;app&lt;/span&gt; &lt;span class="symbol"&gt;:width&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;1024&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:height&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;768&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;  
  &lt;span class="ident"&gt;corners&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;[&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt;&lt;span class="symbol"&gt;:x&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;256&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:y&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;10&lt;/span&gt;&lt;span class="punct"&gt;},&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt;&lt;span class="symbol"&gt;:x&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;12&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:y&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;378&lt;/span&gt;&lt;span class="punct"&gt;},&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt;&lt;span class="symbol"&gt;:x&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;506&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:y&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;378&lt;/span&gt;&lt;span class="punct"&gt;}&lt;/span&gt; &lt;span class="punct"&gt;]&lt;/span&gt;
  &lt;span class="ident"&gt;xpos&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;&lt;span class="ident"&gt;ypos&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;&lt;span class="ident"&gt;c&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;256&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;&lt;span class="number"&gt;10&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;
  &lt;span class="ident"&gt;srand&lt;/span&gt;
  &lt;span class="number"&gt;2111&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;times&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
      &lt;span class="ident"&gt;c&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="ident"&gt;rand&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;3&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="ident"&gt;xpos&lt;/span&gt; &lt;span class="punct"&gt;+=&lt;/span&gt; &lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;corners&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="ident"&gt;c&lt;/span&gt;&lt;span class="punct"&gt;][&lt;/span&gt;&lt;span class="symbol"&gt;:x&lt;/span&gt;&lt;span class="punct"&gt;]-&lt;/span&gt;&lt;span class="ident"&gt;xpos&lt;/span&gt;&lt;span class="punct"&gt;)&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;
      &lt;span class="ident"&gt;ypos&lt;/span&gt; &lt;span class="punct"&gt;+=&lt;/span&gt; &lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;corners&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="ident"&gt;c&lt;/span&gt;&lt;span class="punct"&gt;][&lt;/span&gt;&lt;span class="symbol"&gt;:y&lt;/span&gt;&lt;span class="punct"&gt;]-&lt;/span&gt;&lt;span class="ident"&gt;ypos&lt;/span&gt;&lt;span class="punct"&gt;)&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;
      &lt;span class="ident"&gt;star&lt;/span&gt; &lt;span class="ident"&gt;xpos&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;ypos&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;5&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;10&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;a href="/sierpinskis_shoes.png"&gt;result&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Wed, 14 Nov 2007 23:50:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:2c9a77e9-a013-4c99-8562-90d8c1a6b265</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2007/11/14/sierpinskis-shoes</link>
      <category>Ruby</category>
      <category>Math</category>
    </item>
    <item>
      <title>Huge number factored into primes</title>
      <description>&lt;p&gt;Recently 2^1039-1 were &lt;a href="http://www.crypto-world.com/announcements/m1039.txt"&gt;factored&lt;/a&gt;. Is this the end of 1024 RSA encryption? Lenstra, one of the researchers, addresses this question: "Last time, it took nine years for us to generalize from a special to a non-special hard-to factor number (155 digits). I won't make predictions, but let's just say it might be a good idea to stay tuned."&lt;/p&gt;

&lt;p&gt;2^1039-1 is a &lt;a href="http://en.wikipedia.org/wiki/Mersenne_prime"&gt;special number&lt;/a&gt; that the RSA algorithm would never use so 1024 RSA might still be secure for the time being but if you want to be future safe 2048 bits or more would be the way to go.&lt;/p&gt;</description>
      <pubDate>Wed, 23 May 2007 07:12:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:04708ff7-b317-4c78-bfb4-2069f677846a</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2007/05/23/huge-prime-number-factored</link>
      <category>Security</category>
      <category>Math</category>
    </item>
    <item>
      <title>Find primes in regexp</title>
      <description>&lt;p&gt;In an earlier &lt;a href="/articles/2007/02/09/setl-ruby-and-list-comprehensions"&gt;post&lt;/a&gt; the example code did find prime numbers. Recently I &lt;a href="http://groups.google.com/group/ruby-talk-google/browse_thread/thread/3deebb518a454bc4"&gt;stumbled&lt;/a&gt; over a really cool regexp hack that also deals with primes. This is how you execute that regexp in Ruby:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;Prime&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt; &lt;span class="keyword"&gt;unless&lt;/span&gt; &lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt; &lt;span class="punct"&gt;*&lt;/span&gt; &lt;span class="number"&gt;43&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;=~&lt;/span&gt; &lt;span class="punct"&gt;/&lt;/span&gt;&lt;span class="regex"&gt;^1$|^(11+?)&lt;span class="escape"&gt;\1&lt;/span&gt;+$&lt;/span&gt;&lt;span class="punct"&gt;/&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Change 43 to whatever you like and you will get Prime as output if it's a prime number.
&lt;br&gt;
&lt;br&gt;
&lt;strong&gt;EDIT:&lt;/strong&gt; As you can see in the comments &lt;a href="http://montreal.pm.org/tech/neil_kandalgaonkar.shtml"&gt;Neil Kandalonkar&lt;/a&gt; explained how the regexp by Abigail works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;EDIT 2011-04-06:&lt;/strong&gt;
I happened to stumble upon what I believe is the first time Abigail showed this to the world. It's in &lt;a href="http://groups.google.com/group/comp.lang.perl.misc/msg/149b99453d113730"&gt;a post&lt;/a&gt; in comp.lang.perl.misc back in 1997.
I also found that at &lt;a href="http://www.cpan.org/misc/japh"&gt;http://www.cpan.org/misc/japh&lt;/a&gt; there's a couple more&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;perl&lt;/span&gt; &lt;span class="punct"&gt;-&lt;/span&gt;&lt;span class="ident"&gt;wle&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;print &amp;quot;Prime&amp;quot; if (0 x shift) !~ m 0^\0?$|^(\0\0+?)\1+$0&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;perl&lt;/span&gt; &lt;span class="punct"&gt;-&lt;/span&gt;&lt;span class="ident"&gt;wle&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;print &amp;quot;Prime&amp;quot; if (&amp;quot;m&amp;quot; x shift) !~ m m^\m?$|^(\m\m+?)\1+$mm&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Fri, 30 Mar 2007 07:33:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:511a2ff5-ab23-4fb0-bba6-7a574882563d</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2007/03/30/find-primes-in-regexp</link>
      <category>Ruby</category>
      <category>Math</category>
    </item>
  </channel>
</rss>

