<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0" xml:base="http://arancaytar.ermarian.net/frontpage">
  <channel>
    <title>Aranfoolcaytar - ar•an•fool•cay•tar, n: a programmer perennially comporting himself like a fool</title>
    <link>http://arancaytar.ermarian.net/frontpage</link>
    <description />
    <language>en</language>
          <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/aranfoolcaytar" /><feedburner:info uri="aranfoolcaytar" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
    <title>Extensible BBCode for Drupal 7</title>
    <link>http://feedproxy.google.com/~r/aranfoolcaytar/~3/eoeSmq-uFYM/extensible-bbcode-drupal-7</link>
    <description>&lt;!-- google_ad_section_start --&gt; &lt;p&gt;I&amp;#039;m happy to announce that my extensible BBCode module &lt;a href="http://ermarian.net/downloads/software/drupal/xbbcode/" title="http://ermarian.net/downloads/software/drupal/xbbcode/"&gt;XBBCode&lt;/a&gt; is now ported to Drupal 7. I last worked on (and wrote about) this module in April while writing &lt;a href="/2010/04/16/building-better-tag-parser-part-2"&gt;a new parsing engine&lt;/a&gt; for it.&lt;/p&gt;
&lt;p&gt;Although the filter was working well, there were many small flaws in the surrounding management code that dealt with storing and editing custom tags and settings, interfaced with Drupal&amp;#039;s filter API and handled the installation.&lt;/p&gt;
&lt;p&gt;A recent core update to the filter module &lt;a href="http://drupal.org/node/934050" title="http://drupal.org/node/934050"&gt;changed the numerical format IDs to textual names&lt;/a&gt;, which makes things lots more easier for automatically created text formats (namespaces, etc), so the required compatibility fix left the installation process far cleaner than it had been before.&lt;/p&gt;
&lt;p&gt;Also, the actual tag packages, xbbcode_basic, xbbcode_list and xbbcode_table have been ported, so the module can actually be used beyond custom tags.&lt;/p&gt;
&lt;p&gt;Unfortunately, while the Syntax Highlighter tie-in has been ported to Drupal 7, it is useless until the &lt;a href="http://drupal.org/project/highlighter" title="http://drupal.org/project/highlighter"&gt;Highlighter module&lt;/a&gt; itself is ported. I cleaned it up for Drupal&amp;#039;s CVS a few months ago, but it is still only compatible with Drupal 6.&lt;/p&gt;
&lt;p&gt;Note that while the module can be installed and used without causing immediate major errors on a clean Drupal 7 install, it has not been exhaustively tested, and small bugs can still remain. It should not be used in production yet.&lt;/p&gt;
&lt;p&gt;The release is available at &lt;a href="http://ermarian.net/downloads/software/drupal/xbbcode/xbbcode-7.x-2.0.4-r496.tar.gz" title="http://ermarian.net/downloads/software/drupal/xbbcode/xbbcode-7.x-2.0.4-r496.tar.gz"&gt;http://ermarian.net/downloads/software/drupal/xbbcode/xbbcode-7.x-2.0.4-...&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It can be checked out from SVN at &lt;a href="http://svn.ermarian.net/drupal/modules/xbbcode/trunk/xbbcode" title="http://svn.ermarian.net/drupal/modules/xbbcode/trunk/xbbcode"&gt;http://svn.ermarian.net/drupal/modules/xbbcode/trunk/xbbcode&lt;/a&gt;.&lt;/p&gt;
 &lt;!-- google_ad_section_end --&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/2WqTyzrXV-8THDQgremv_k0MOi8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/2WqTyzrXV-8THDQgremv_k0MOi8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/2WqTyzrXV-8THDQgremv_k0MOi8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/2WqTyzrXV-8THDQgremv_k0MOi8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/aranfoolcaytar/~4/eoeSmq-uFYM" height="1" width="1"/&gt;</description>
     <comments>http://arancaytar.ermarian.net/2010/10/26/extensible-bbcode-drupal-7#comments</comments>
 <category domain="http://arancaytar.ermarian.net/keyword/bbcode">bbCode</category>
 <category domain="http://arancaytar.ermarian.net/news/technology/web/drupal">Drupal</category>
 <category domain="http://arancaytar.ermarian.net/keyword/php">php</category>
 <category domain="http://arancaytar.ermarian.net/keyword/web">web</category>
 <category domain="http://arancaytar.ermarian.net/keyword/xbbcode">xbbcode</category>
 <wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://arancaytar.ermarian.net/crss/node/218</wfw:commentRss>
 <pubDate>Tue, 26 Oct 2010 21:43:48 +0000</pubDate>
 <dc:creator>Arancaytar</dc:creator>
 <guid isPermaLink="false">218 at http://arancaytar.ermarian.net</guid>
  <feedburner:origLink>http://arancaytar.ermarian.net/2010/10/26/extensible-bbcode-drupal-7</feedburner:origLink></item>
  <item>
    <title>Paper-based computing</title>
    <link>http://feedproxy.google.com/~r/aranfoolcaytar/~3/X3bbXyRBLAc/paper-based-computing</link>
    <description>&lt;!-- google_ad_section_start --&gt; &lt;p&gt;While traveling to a workshop with a friend and family this morning, I had the rare occasion to be not only awed by a piece of technology, but completely flabbergasted at its existence. This friend has a liking for cool gadgets, and what he showed me today had me first suspecting some sleight of hand, then shortly later positively &lt;em&gt;drooling&lt;/em&gt;. The best part? It&amp;#039;s a fraction of the price of a netbook.&lt;/p&gt;
&lt;p&gt;He took out a pen that looked like a fairly ordinary if slightly bulky fountain pen at first glance, but obviously had some technomancy inside as shown by the LED display on the side and some kind of metal charger contacts. He then opened an even more ordinary notebook of plain paper and thumbed through it to some lecture notes he had taken earlier.&lt;/p&gt;
&lt;p&gt;Then he uncapped the pen, activated it and tapped a word on the page with the tip.&lt;/p&gt;
&lt;p&gt;Instantly, the sounds of the lecture came from the pen&amp;#039;s built-in speakers, apparently exactly at the point in the recording that matched the place in the notes.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;What the fuck?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Okay, so putting a voice recorder and flash memory in a pen is simplicity itself. Putting sensors into the tip that can record what you write is less easy, though possible when you think about it. But how the hell did the pen know exactly where on the page you tapped it, and what to play from the recording? Looking more closely at the page, I now saw it had some unusual stuff at the bottom - buttons (printed on) that were labeled &amp;quot;Play&amp;quot;, &amp;quot;Stop&amp;quot;, etc. However, no barcode or anything was visible that would have allowed the pen to pinpoint even its page, leave alone its location.&lt;/p&gt;
&lt;p&gt;So how does it work? There has to be something special in the paper. I asked if it was some expensive &amp;quot;smart&amp;quot; paper with electronic gadgetry, but it turned out to be quite as normal as it looked: Some microscopic dots of ink were printed on it (printable with any laser or offset printer), and the camera built into the tip scanned and recognized the unique dot pattern to be able to identify its location.&lt;/p&gt;
&lt;p&gt;I had one of those &amp;quot;Holy crap, we&amp;#039;re living in the twenty-first century&amp;quot; moments.&lt;/p&gt;
&lt;p&gt;And then one of those &amp;quot;WANNA&amp;quot; moments. &lt;/p&gt;
&lt;p&gt;Pity the thing is made by only a single producer unwilling to support Linux. While the pen itself is a closed system that works well with loudspeakers or earphones, archiving the notes to a computer requires some kind of proprietary Windows application that is reportedly impossible to run on Wine. Maybe if the market were a bit larger, a reliable open-source driver would be developed, but as this thing has been around since 2008, it probably won&amp;#039;t happen so soon. Still, the technology used to accomplish this is awesome.&lt;/p&gt;
 &lt;!-- google_ad_section_end --&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/lpjz0riKrCrSBMDI_HeUUnuDtkc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lpjz0riKrCrSBMDI_HeUUnuDtkc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/lpjz0riKrCrSBMDI_HeUUnuDtkc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lpjz0riKrCrSBMDI_HeUUnuDtkc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/aranfoolcaytar/~4/X3bbXyRBLAc" height="1" width="1"/&gt;</description>
     <comments>http://arancaytar.ermarian.net/2010/10/23/paper-based-computing#comments</comments>
 <category domain="http://arancaytar.ermarian.net/news/technology">Technology</category>
 <wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://arancaytar.ermarian.net/crss/node/217</wfw:commentRss>
 <pubDate>Sat, 23 Oct 2010 19:24:29 +0000</pubDate>
 <dc:creator>Arancaytar</dc:creator>
 <guid isPermaLink="false">217 at http://arancaytar.ermarian.net</guid>
  <feedburner:origLink>http://arancaytar.ermarian.net/2010/10/23/paper-based-computing</feedburner:origLink></item>
  <item>
    <title>Server moved, PHP rebuilt</title>
    <link>http://feedproxy.google.com/~r/aranfoolcaytar/~3/_2W1R2TGNT4/server-moved-php-rebuilt</link>
    <description>&lt;!-- google_ad_section_start --&gt; &lt;p&gt;Yesterday, the entire Ermarian Network was migrated to a different server. After spending the past four years on &lt;em&gt;millhouse&lt;/em&gt;, an AMD Opteron machine with two 2GHz cores and 2GB RAM, it is now hosted on &lt;em&gt;carvin&lt;/em&gt; a brand new Intel Xeon with four 2.5GHz cores (hyperthreaded to eight virtual cores) and 16GB RAM. These impressive numbers are of course slightly misleading, since I&amp;#039;m one of a great number of users sharing the server.&lt;/p&gt;
&lt;p&gt;Naturally, the very first thing that happened after the server move was that all my sites stopped functioning. This was to be expected and warned about, since I was using a PHP interpreter I had compiled myself, dynamically linking against multiple system libraries. With a new system and different version numbers, there was bound to be a mismatch (in this case, &lt;code&gt;libssl.so.0.9.7&lt;/code&gt; being replaced by a newer version). I had to recompile PHP. While doing so, I also decided to switch to the new PHP 5.3.x release (I had been using 5.2.x before).&lt;/p&gt;
&lt;p&gt;If you have ever installed software without a package manager, using only &lt;code&gt;./configure --prefix=$HOME &amp;amp;&amp;amp; make &amp;amp;&amp;amp; make install&lt;/code&gt;, you know what a royal pain this can be. But if you have never done this with PHP, you have &lt;em&gt;no idea&lt;/em&gt;. Compiling only the barest essential extensions, those that are packaged with the PHP source, is not that bad. Even linking to mysql is easy. But I use a fairly wide mix of extensions that include cURL, libtidy, mcrypt and PEAR.&lt;/p&gt;
&lt;p&gt;Installing this software and then linking with it is an intricate arcane ritual. For example, mcrypt requires both libmcrypt and mhash to be installed, but its installer offers no easy way of looking for those libraries in a custom folder. Since I have to do everything in my &lt;code&gt;$HOME&lt;/code&gt;, and no access to &lt;code&gt;/usr/local&lt;/code&gt;, that&amp;#039;s tricky.&lt;/p&gt;
&lt;p&gt;I solved this using the following command:&lt;/p&gt;

&lt;object&gt;&lt;div class="codeblock"&gt;env LD_LIBRARY_PATH=$HOME/lib LD_FLAGS=&amp;quot;-L$HOME/lib -I$HOME/include&amp;quot; ./configure --prefix=$HOME&lt;/div&gt;&lt;/object&gt;
&lt;p&gt;The &lt;code&gt;-L&lt;/code&gt; and &lt;code&gt;-I&lt;/code&gt; linking flags tell the compiler to look for libraries and header files in these custom directories.&lt;/p&gt;
&lt;p&gt;The second issue was far harder to identify: While running the PHP &lt;code&gt;./configure&lt;/code&gt; script, it cancelled mysteriously and warned that there was an error with the mysql library. Confusingly, it &lt;em&gt;found&lt;/em&gt; the mysql library (changing to another path resulted in a different error), but the sample C++ code it tried to use to test the library failed.&lt;/p&gt;
&lt;p&gt;Eventually, I found out this was not because of MySQL at all, but because the libltdl library it was trying to include was missing. This was an extra component of libmcrypt, and I had to install it separately using&lt;/p&gt;

&lt;object&gt;&lt;div class="codeblock"&gt;cd ~/files/installs/libmcrypt/
./configure --prefix=$HOME --enable-ltdl-install &amp;amp;&amp;amp; make &amp;amp;&amp;amp; make install&lt;/div&gt;&lt;/object&gt;
&lt;p&gt;After doing this, the configure script ran through, as did the compilation and installation.&lt;/p&gt;
&lt;p&gt;I then had to update a few symbolic links, rename the php-cgi file to php.cgi (to this day I have no idea why this is required), and add the old php.ini, and the Ermarian Network was once again functioning, now on PHP 5.3.3! &lt;/p&gt;
&lt;p&gt;Note that Drupal 5 uses many deprecated functions, and its error handler does not recognize the new DEPRECATED bitmasks (8192 and 16384) of PHP 5.3.3, leading to a lot of &amp;quot;undefined offset&amp;quot; notices. That was also &lt;a href="http://drupal.org/node/524664" title="http://drupal.org/node/524664"&gt;easy enough to fix&lt;/a&gt;, however.&lt;/p&gt;
 &lt;!-- google_ad_section_end --&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/xAb5vGz3hcA2JF9tPnz9wtmLaus/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xAb5vGz3hcA2JF9tPnz9wtmLaus/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/xAb5vGz3hcA2JF9tPnz9wtmLaus/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xAb5vGz3hcA2JF9tPnz9wtmLaus/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/aranfoolcaytar/~4/_2W1R2TGNT4" height="1" width="1"/&gt;</description>
     <comments>http://arancaytar.ermarian.net/2010/08/23/server-moved-php-rebuilt#comments</comments>
 <category domain="http://arancaytar.ermarian.net/keyword/compiling">compiling</category>
 <category domain="http://arancaytar.ermarian.net/keyword/dreamhost">Dreamhost</category>
 <category domain="http://arancaytar.ermarian.net/keyword/drupal">drupal</category>
 <category domain="http://arancaytar.ermarian.net/keyword/php-53">PHP 5.3</category>
 <category domain="http://arancaytar.ermarian.net/news/technology/web/php">PHP</category>
 <wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://arancaytar.ermarian.net/crss/node/216</wfw:commentRss>
 <pubDate>Mon, 23 Aug 2010 14:28:46 +0000</pubDate>
 <dc:creator>Arancaytar</dc:creator>
 <guid isPermaLink="false">216 at http://arancaytar.ermarian.net</guid>
  <feedburner:origLink>http://arancaytar.ermarian.net/2010/08/23/server-moved-php-rebuilt</feedburner:origLink></item>
  <item>
    <title>Code Jam 2010</title>
    <link>http://feedproxy.google.com/~r/aranfoolcaytar/~3/5zwaXeupp4Q/code-jam-2010</link>
    <description>&lt;!-- google_ad_section_start --&gt; &lt;p&gt;The first qualification round of the &lt;a href="http://code.google.com/codejam" title="http://code.google.com/codejam"&gt;Google Code Jam&lt;/a&gt; just ended, and I managed to scrape through with two of the three problems solved in time. On the third, unfortunately, I failed to take into account the orders of magnitude of complexity, and my algorithm was not fast enough to solve the larger input case.&lt;/p&gt;
&lt;p&gt;Since the round is now closed, I can publish the solutions I used without unfairly influencing the competition. (Don&amp;#039;t read on if you want to figure it out for yourself, though. It&amp;#039;s really fun to solve on your own.)&lt;/p&gt;
&lt;h3&gt;Problem 1: Snappers&lt;/h3&gt;
&lt;blockquote&gt;&lt;p&gt;The Snapper is a clever little device that, on one side, plugs its input plug into an output socket, and, on the other side, exposes an output socket for plugging in a light or other device.&lt;/p&gt;
&lt;p&gt;When a Snapper is in the ON state and is receiving power from its input plug, then the device connected to its output socket is receiving power as well. When you snap your fingers -- making a clicking sound -- any Snapper receiving power at the time of the snap toggles between the ON and OFF states. [&lt;a href="http://code.google.com/codejam/contest/dashboard?c=433101#" title="http://code.google.com/codejam/contest/dashboard?c=433101#"&gt;read on&lt;/a&gt;]&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The beautiful simplicity of this problem is obvious as soon as you consider the pattern of switches on each clap:&lt;/p&gt;

&lt;object&gt;&lt;div class="codeblock"&gt;00000
00001
00010
00011
00100&lt;/div&gt;&lt;/object&gt;
&lt;p&gt;Having established that the switches follow the progression of binary numbers, it is also clear what property the binary number must have for the light to be on: All switches must be switched on; the binary number (from the nth digit to the right) has to completely consist of ones. That means if you add one to the number, that pattern must consist of zeros.&lt;/p&gt;
&lt;p&gt;It is possible to do all this using modulo arithmetic, but I thought using actual bitwise operators might be somewhat faster.&lt;/p&gt;
&lt;div class="hl-main"&gt;
&lt;pre&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;snapper&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;k&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-comment"&gt;# Add one to the number, AND it with the number &amp;quot;2^n-1&amp;quot;, check for 0.&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-identifier"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;k&lt;/span&gt;&lt;span class="hl-code"&gt;+&lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt; &amp;amp; &lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt; - &lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt; == &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;
 
&lt;/span&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;main&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-identifier"&gt;labels&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;OFF&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;ON&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;try&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;cases&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-builtin"&gt;xrange&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-builtin"&gt;int&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-builtin"&gt;raw_input&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;+&lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;for&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;case&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;in&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;cases&lt;/span&gt;&lt;span class="hl-code"&gt;:
            &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;k&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-builtin"&gt;map&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;int&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-builtin"&gt;raw_input&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;.&lt;/span&gt;&lt;span class="hl-identifier"&gt;split&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
            &lt;/span&gt;&lt;span class="hl-reserved"&gt;print&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;Case #%d: %s&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt; % &lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;case&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;labels&lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-identifier"&gt;snapper&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;k&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;except&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;ValueError&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;print&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;INVALID INPUT&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;
 
&lt;/span&gt;&lt;span class="hl-identifier"&gt;main&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;hr /&gt;
&lt;h3&gt;Problem 2: Doomsday&lt;/h3&gt;
&lt;blockquote&gt;On our planet, Jamcode IX, three Great Events occurred. They happened 26000, 11000 and 6000 slarboseconds ago. In 4000 slarboseconds, the amount of time since all of those events will be multiples of 5000 slarboseconds, the largest possible amount... and the apocalypse will come.&lt;/blockquote&gt;
&lt;p&gt;This one was a bit trickier. Obviously, you will immediately be thinking along the right lines of what is required here: Modulo arithmetic and greatest common divisors. Hope you have the Euclidean algorithm fresh in your mind (&lt;code&gt;gcd a b = b ? (gcd b a%b) : a&lt;/code&gt;). However, it is not immediately clear what you need to do with those numbers. Clearly, 26000, 11000 and 6000 have nothing in common, divisor-wise, with 5000.&lt;/p&gt;
&lt;p&gt;Naturally, the differences between these numbers &lt;em&gt;do&lt;/em&gt;. You will find that regardless of the order of the numbers, the intervals between them have the same greatest common divisor - 5000. Even more, it can be shown that any group of numbers shares a remainder class for the greatest common divisor of their differences - 1000 in this case. The complement of this remainder class (4000) is the doomsday time we are looking for.&lt;/p&gt;
&lt;p&gt;So the algorithm looks like this:&lt;/p&gt;
&lt;div class="hl-main"&gt;
&lt;pre&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;doomsday&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;events&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-identifier"&gt;T&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-identifier"&gt;gcd_n&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;intervals&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;events&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; -&lt;/span&gt;&lt;span class="hl-identifier"&gt;events&lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt; % &lt;/span&gt;&lt;span class="hl-identifier"&gt;T&lt;/span&gt;&lt;span class="hl-code"&gt;
 
&lt;/span&gt;&lt;span class="hl-comment"&gt;# Absolute values of the intervals between N numbers.&lt;/span&gt;&lt;span class="hl-code"&gt;
&lt;/span&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;intervals&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;z&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-builtin"&gt;abs&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;a&lt;/span&gt;&lt;span class="hl-code"&gt;-&lt;/span&gt;&lt;span class="hl-identifier"&gt;b&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;for&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;a&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;b&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;in&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-builtin"&gt;zip&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;z&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;z&lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt;
 
&lt;/span&gt;&lt;span class="hl-comment"&gt;# Greatest Common Divisor of N numbers.&lt;/span&gt;&lt;span class="hl-code"&gt;
&lt;/span&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;gcd_n&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;z&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-comment"&gt;# gcd(a, b, c) = gcd(gcd(a,b), c)&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-builtin"&gt;reduce&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;gcd_2&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;z&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
 
&lt;/span&gt;&lt;span class="hl-comment"&gt;# Greatest Common Divisor of 2 numbers.&lt;/span&gt;&lt;span class="hl-code"&gt;
&lt;/span&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;gcd_2&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;a&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;b&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;while&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;b&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;a&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;b&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-identifier"&gt;b&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;a&lt;/span&gt;&lt;span class="hl-code"&gt; % &lt;/span&gt;&lt;span class="hl-identifier"&gt;b&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;a&lt;/span&gt;&lt;span class="hl-code"&gt;
 
&lt;/span&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;main&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;try&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;cases&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-builtin"&gt;xrange&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-builtin"&gt;int&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-builtin"&gt;raw_input&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;+&lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;for&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;case&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;in&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;cases&lt;/span&gt;&lt;span class="hl-code"&gt;:
            &lt;/span&gt;&lt;span class="hl-identifier"&gt;line&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-builtin"&gt;map&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;int&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-builtin"&gt;raw_input&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;.&lt;/span&gt;&lt;span class="hl-identifier"&gt;split&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
            &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;events&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-identifier"&gt;line&lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;line&lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt;
            &lt;/span&gt;&lt;span class="hl-reserved"&gt;assert&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt; == &lt;/span&gt;&lt;span class="hl-builtin"&gt;len&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;events&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
            &lt;/span&gt;&lt;span class="hl-reserved"&gt;print&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;Case #%d: %d&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt; % &lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;case&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;doomsday&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;events&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;except&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;print&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;INVALID INPUT&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;
            
&lt;/span&gt;&lt;span class="hl-identifier"&gt;main&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;hr /&gt;
&lt;h3&gt;Problem 3: Theme Park&lt;/h3&gt;
&lt;blockquote&gt;The roller coaster can hold k people at once. People queue for it in groups. Groups board the roller coaster, one at a time, until there are no more groups left or there is no room for the next group; then the roller coaster goes, whether it&amp;#039;s full or not. Once the ride is over, all of its passengers re-queue in the same order. The roller coaster will run R times in a day.&lt;/blockquote&gt;
&lt;p&gt;This is the problem I didn&amp;#039;t manage to solve in time. I had two implementations using the same approach, one in Haskell and one in Python. Both did not scale well, as the algorithm ran in linear time and the final input differed by orders of magnitude from what I had tested it with.&lt;/p&gt;

&lt;object&gt;&lt;div class="codeblock"&gt;-- Does the next group fit aboard?
  if (length queue &amp;gt; 0) &amp;amp;&amp;amp; (head queue &amp;lt;= size - sum aboard)
  -- Then let them pay, leave the queue and step aboard.
  then advance rides size (tail queue) (aboard++[head queue]) (profit + (head queue))
  else
    -- Otherwise, are there rides left?
    if rides &amp;gt; 1
    -- Then let everyone disembark and requeue for the next ride.
    then advance (rides - 1) size (queue++aboard) [] profit
    else profit
    
-- Start out with an empty ride and no profit.
start rides size queue = advance rides size queue [] 0&lt;/div&gt;&lt;/object&gt;
&lt;div class="hl-main"&gt;
&lt;pre&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;rollercoaster&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;rides&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;size&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;queue&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-identifier"&gt;profit&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;aboard&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;while&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;rides&lt;/span&gt;&lt;span class="hl-code"&gt; &amp;gt; &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;aboard&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;for&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;x&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;in&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;queue&lt;/span&gt;&lt;span class="hl-code"&gt;:
            &lt;/span&gt;&lt;span class="hl-reserved"&gt;if&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;aboard&lt;/span&gt;&lt;span class="hl-code"&gt; + &lt;/span&gt;&lt;span class="hl-identifier"&gt;x&lt;/span&gt;&lt;span class="hl-code"&gt; &amp;gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;size&lt;/span&gt;&lt;span class="hl-code"&gt;:
                &lt;/span&gt;&lt;span class="hl-reserved"&gt;break&lt;/span&gt;&lt;span class="hl-code"&gt;
            &lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;aboard&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt; + &lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;aboard&lt;/span&gt;&lt;span class="hl-code"&gt; + &lt;/span&gt;&lt;span class="hl-identifier"&gt;x&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;profit&lt;/span&gt;&lt;span class="hl-code"&gt; += &lt;/span&gt;&lt;span class="hl-identifier"&gt;aboard&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;queue&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-identifier"&gt;queue&lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt; + &lt;/span&gt;&lt;span class="hl-identifier"&gt;queue&lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;rides&lt;/span&gt;&lt;span class="hl-code"&gt; -= &lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;profit&lt;/span&gt;&lt;span class="hl-code"&gt;
 
&lt;/span&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;main&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-identifier"&gt;cases&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-builtin"&gt;xrange&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-builtin"&gt;int&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-builtin"&gt;raw_input&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;+&lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;for&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;case&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;in&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;cases&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;rides&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;size&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;queue_len&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-builtin"&gt;map&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;int&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-builtin"&gt;raw_input&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;.&lt;/span&gt;&lt;span class="hl-identifier"&gt;split&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;queue&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-builtin"&gt;map&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;int&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-builtin"&gt;raw_input&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;.&lt;/span&gt;&lt;span class="hl-identifier"&gt;split&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;assert&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;queue_len&lt;/span&gt;&lt;span class="hl-code"&gt; == &lt;/span&gt;&lt;span class="hl-builtin"&gt;len&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;queue&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;print&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;Case #%d: %d&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt; % &lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;case&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;rollercoaster&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;rides&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;size&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;queue&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
&lt;/span&gt;&lt;span class="hl-identifier"&gt;main&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
 &lt;!-- google_ad_section_end --&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/JOo8nonzt6-LaSycn4kUiyBWKhQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/JOo8nonzt6-LaSycn4kUiyBWKhQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/JOo8nonzt6-LaSycn4kUiyBWKhQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/JOo8nonzt6-LaSycn4kUiyBWKhQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/aranfoolcaytar/~4/5zwaXeupp4Q" height="1" width="1"/&gt;</description>
     <comments>http://arancaytar.ermarian.net/2010/05/09/code-jam-2010#comments</comments>
 <category domain="http://arancaytar.ermarian.net/keyword/codejam">codejam</category>
 <category domain="http://arancaytar.ermarian.net/news/technology/google">Google</category>
 <wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://arancaytar.ermarian.net/crss/node/215</wfw:commentRss>
 <pubDate>Sat, 08 May 2010 23:27:22 +0000</pubDate>
 <dc:creator>Arancaytar</dc:creator>
 <guid isPermaLink="false">215 at http://arancaytar.ermarian.net</guid>
  <feedburner:origLink>http://arancaytar.ermarian.net/2010/05/09/code-jam-2010</feedburner:origLink></item>
  <item>
    <title>Hard drives and stupidity do not mix</title>
    <link>http://feedproxy.google.com/~r/aranfoolcaytar/~3/MpVseG1OfFU/hard-drives-and-stupidity-do-not-mix</link>
    <description>&lt;!-- google_ad_section_start --&gt; &lt;p&gt;At 0400 on Wednesday (yesterday), my home partition suddenly disappeared. I didn&amp;#039;t have a clue what had happened, but the problem persisted on reboot.&lt;/p&gt;
&lt;p&gt;Now, Enki has two hard drives - one contains /home, and one contains my backups. I was able to mount the backup drive manually, but the one containing /home appeared to be unresponsive.&lt;/p&gt;
&lt;p&gt;So I took out what I assumed to be the defective drive. I then connected it to a different computer and was instantly smelling magic smoke.&lt;/p&gt;
&lt;p&gt;After buying a new drive, I built it in and launched Puppy Linux to partition it and set up a new /home. My first priority, though, was to copy all files off the backup partition in case that failed too. It&amp;#039;s amazing how diligently you make backups on the day after losing data (... and a dollar short.)&lt;/p&gt;
&lt;p&gt;So I mounted what I presumed to be the backup drive, and saw... /HOME.&lt;/p&gt;
&lt;p&gt;First thought: Oh wow. It&amp;#039;s all still here. I&amp;#039;m saved. There&amp;#039;s some stuff on the backup drive too that I&amp;#039;ll miss, but not really a lot.&lt;br /&gt;Second thought: This means I not only built out the wrong drive, it also means I killed it through mishandling (since it was working a moment earlier).&lt;br /&gt;Third thought: Actually, I killed it for absolutely no reason, because even what seemed broken was in fact okay.&lt;/p&gt;
&lt;p&gt;Final thought, just now: Actually, the last one makes it hurt most, but is also the best. Imagine if my /home had died, and in trying to fix it I&amp;#039;d destroyed the only backup of it. Now THAT would suck.&lt;/p&gt;
 &lt;!-- google_ad_section_end --&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/yu21Gklp5zTfJKWG8q-atTheKXQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/yu21Gklp5zTfJKWG8q-atTheKXQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/yu21Gklp5zTfJKWG8q-atTheKXQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/yu21Gklp5zTfJKWG8q-atTheKXQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/aranfoolcaytar/~4/MpVseG1OfFU" height="1" width="1"/&gt;</description>
     <comments>http://arancaytar.ermarian.net/2010/04/29/hard-drives-and-stupidity-do-not-mix#comments</comments>
 <category domain="http://arancaytar.ermarian.net/news/personal/aranfoolcaytar">Aranfoolcaytar</category>
 <category domain="http://arancaytar.ermarian.net/keyword/enki">Enki</category>
 <category domain="http://arancaytar.ermarian.net/keyword/fool">fool</category>
 <wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://arancaytar.ermarian.net/crss/node/214</wfw:commentRss>
 <pubDate>Thu, 29 Apr 2010 07:39:15 +0000</pubDate>
 <dc:creator>Arancaytar</dc:creator>
 <guid isPermaLink="false">214 at http://arancaytar.ermarian.net</guid>
  <feedburner:origLink>http://arancaytar.ermarian.net/2010/04/29/hard-drives-and-stupidity-do-not-mix</feedburner:origLink></item>
  <item>
    <title>Building a better Tag-Parser, part 2</title>
    <link>http://feedproxy.google.com/~r/aranfoolcaytar/~3/_slD1XcaXYc/building-better-tag-parser-part-2</link>
    <description>&lt;!-- google_ad_section_start --&gt; &lt;p&gt;As I concluded in the &lt;a href="http://arancaytar.ermarian.net/2010/04/15/building-better-tag-parser" title="http://arancaytar.ermarian.net/2010/04/15/building-better-tag-parser"&gt;first part&lt;/a&gt;, one problem remains with the strict &amp;quot;inner-tag-first&amp;quot; evaluation of code (which roughly equates to a Left-Right-Root traversal of the code tree). Namely, some tags, like &lt;code&gt;[nocode]&lt;/code&gt;, require that further code inside them is not parsed. &lt;/p&gt;
&lt;p&gt;At first glance, this looks easy: Just determine if the currently open tag allows any code inside it, and ignore any opening tags otherwise until it closes (other closing tags must still be examined in case the nocode tag is dangling). There are multiple conditions where this fails:&lt;/p&gt;
&lt;ol class="numeric"&gt;&lt;li&gt;The &lt;code&gt;[nocode]&lt;/code&gt; tag is not closed. Even though it is ultimately discarded, all tags following it will remain unrendered. &amp;quot;Discarded&amp;quot; tags should not influence the rendering.&lt;/li&gt;&lt;li&gt;The tag is discarded because its parent tag is closed before it itself is. This causes the same problem as condition 1, up to the closing parent tag.&lt;/li&gt;&lt;li&gt;Even with valid code, if the nocode tag contains a valid pair of tags, and the opening one is ignored, then the closing one may be misinterpreted as closing a parent tag above nocode, resulting in condition 2.&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;From number 3 it is clear that even without rendering them, tags still have to be tracked inside the nocode block. But how to ensure that they will be retroactively rendered in case the nocode tag ends up discarded?&lt;/p&gt;
&lt;p&gt;In this case, it is possible to recursively reapply the filter to the text block that hasn&amp;#039;t yet been rendered. In pseudocode (some context added in parentheses; refer to the code of the first part), the additions and changes are:&lt;/p&gt;

&lt;object&gt;&lt;div class="codeblock"&gt;1.  Let `nocode` be 0.

2.  (Inside the iteration over `tags`:)
        (If `T` is an opening tag:)

            If `T` is a nocode tag:
                Increment `nocode` by 1.

        (If `T` is a closing tag:)
            (While popping dangling tags off `open_tags`:)

                If `Current` is a nocode tag:
                    Decrement `nocode` by 1.
                    If `nocode` is 0:
                        Run myself on the input `Current.content` and store it in `Current.content`

            
            (Instead of always rendering the tag when it is closed)
            If `nocode` is greater than 0:
                Append `Current.element` to `Parent.content`
                Append `Current.content` to `Parent.content`
                Append [/`Current.name`] to `Parent.content`
            Else:
                Append the rendered form of `Current` to `Parent.content`&lt;/div&gt;&lt;/object&gt;
&lt;p&gt;I am not yet sure whether the recursion/backtracking can be maliciously exploited, in the sense that certain crafted input might take a lot of server resources to process. Even if that is the case though, it would be simple to put in some safeguards.&lt;/p&gt;
&lt;p&gt;The upside of this change is that the &amp;quot;weight&amp;quot; of tags becomes completely obsolete. Instead of having a specified order for every single tag, every single tag merely specifies whether it wants to be rendered &lt;em&gt;before&lt;/em&gt; its content is, or &lt;em&gt;after&lt;/em&gt;.&lt;/p&gt;
 &lt;!-- google_ad_section_end --&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/4LXr17L6d-FqMV6oCzm4xJDFb_M/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4LXr17L6d-FqMV6oCzm4xJDFb_M/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/4LXr17L6d-FqMV6oCzm4xJDFb_M/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4LXr17L6d-FqMV6oCzm4xJDFb_M/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/aranfoolcaytar/~4/_slD1XcaXYc" height="1" width="1"/&gt;</description>
     <comments>http://arancaytar.ermarian.net/2010/04/16/building-better-tag-parser-part-2#comments</comments>
 <category domain="http://arancaytar.ermarian.net/keyword/bbcode">bbCode</category>
 <category domain="http://arancaytar.ermarian.net/news/technology/web/drupal">Drupal</category>
 <category domain="http://arancaytar.ermarian.net/keyword/xbbcode">xbbcode</category>
 <wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://arancaytar.ermarian.net/crss/node/213</wfw:commentRss>
 <pubDate>Fri, 16 Apr 2010 12:11:34 +0000</pubDate>
 <dc:creator>Arancaytar</dc:creator>
 <guid isPermaLink="false">213 at http://arancaytar.ermarian.net</guid>
  <feedburner:origLink>http://arancaytar.ermarian.net/2010/04/16/building-better-tag-parser-part-2</feedburner:origLink></item>
  <item>
    <title>Building a better Tag-Parser</title>
    <link>http://feedproxy.google.com/~r/aranfoolcaytar/~3/zzphiBEVYUM/building-better-tag-parser</link>
    <description>&lt;!-- google_ad_section_start --&gt; &lt;h3&gt;The Problem&lt;/h3&gt;
&lt;p&gt;If you read the code of my &lt;a href="http://ermarian.net/xbbcode" title="http://ermarian.net/xbbcode"&gt;Extensible BBCode&lt;/a&gt; module for &lt;a href="http://en.wikipedia.org/wiki/Drupal" title="reference on Drupal"&gt;Drupal&lt;/a&gt;, you&amp;#039;ll notice that my tag parsing algorithm is kind of complex. In two steps, the tags are first &amp;quot;paired&amp;quot; (inserting a matching ID into the opening and closing tag) and then &amp;quot;rendered&amp;quot;, all by an evaluated regular expression that calls another function. The process certainly ensures that all tags are balanced - and it even allows some tags to be rendered before others are, regardless of nesting. &lt;/p&gt;
&lt;p&gt;But for all its voodoo magic and messing about, it is vulnerable to the same bug as most primitive BBCode parsers. Namely, allowing input that will result in this output:&lt;/p&gt;

&lt;object&gt;&lt;div class="codeblock"&gt;&amp;lt;strong&amp;gt;This sentence is &amp;lt;em&amp;gt;not&amp;lt;/strong&amp;gt; valid HTML code.&amp;lt;/em&amp;gt;&lt;/div&gt;&lt;/object&gt;
&lt;p&gt;Now, the &lt;a href="http://en.wikipedia.org/wiki/quirks_mode" title="reference on quirks mode"&gt;quirks mode&lt;/a&gt; of all &lt;a href="http://en.wikipedia.org/wiki/layout_engines" title="reference on layout engines"&gt;layout engines&lt;/a&gt; are very good at dealing with simple cases like that. Inline markup will be rendered correctly even with overlapping elements. &lt;/p&gt;
&lt;p&gt;But what if it includes block elements? In &lt;a href="http://en.wikipedia.org/wiki/phpBB" title="reference on phpBB"&gt;phpBB&lt;/a&gt;, I recently noticed that it is extremely easy to disrupt the page layout using just two simple tags; &lt;code&gt;[spoiler]&lt;/code&gt; and &lt;code&gt;[quote]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The following overlapping input will be accepted:&lt;/p&gt;

&lt;object&gt;&lt;div class="codeblock"&gt;[quote]
  [spoiler]
[/quote]
  [/spoiler]&lt;/div&gt;&lt;/object&gt;
&lt;p&gt;The following output will (roughly) be generated:&lt;/p&gt;

&lt;object&gt;&lt;div class="codeblock"&gt;&amp;lt;blockquote&amp;gt;
  &amp;lt;div&amp;gt;&amp;lt;div&amp;gt;&amp;lt;div&amp;gt;
&amp;lt;/blockquote&amp;gt;
  &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&lt;/div&gt;&lt;/object&gt;
&lt;p&gt;Unlike the inline markup, several major layout engines (&lt;a href="http://en.wikipedia.org/wiki/Gecko_(layout_engine)" title="reference on Gecko (layout engine)|"&gt;Gecko (layout engine)|&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/WebKit" title="reference on WebKit"&gt;WebKit&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Presto_(layout_engine)" title="reference on Presto (layout engine)|"&gt;Presto (layout engine)|&lt;/a&gt; are the ones tested) take issue with this. The quirk mode behavior in all three drops the three unmatched div tags inside the blockquote element, &lt;em&gt;and then applies the three closing div tags to parent div elements higher up in the tree&lt;/em&gt;. This naturally wreaks havoc on the page. (I&amp;#039;d almost consider this a mild denial-of-service vulnerability because any malicious user can break the page layout.)&lt;/p&gt;
&lt;p&gt;XBBCode is vulnerable to the same, because in spite of all its careful &amp;quot;pair matching&amp;quot;, it never actually checks that the tag tree is nested correctly.&lt;/p&gt;
&lt;h3&gt;The Algorithm&lt;/h3&gt;
&lt;p&gt;So now I&amp;#039;m working on a much simpler and in my opinion more stable approach. I can only suppose it hadn&amp;#039;t occurred to me earlier because I was thinking that BBCode had to be rendered &amp;quot;in-place&amp;quot;, replacing each tag in the string where it was found. It seems that a better approach is to first find all tags, and then gradually build the output from substrings of the original text.&lt;/p&gt;
&lt;p&gt;In Pseudocode (the actual PHP code is not as clean, alas):&lt;/p&gt;

&lt;object&gt;&lt;div class="codeblock"&gt;1. Let `tags` be a list of the opening and closing tags in the input.
        Every element has the properties .name, .is-opening, .args, .index, .length
            .index is the string offset of the first character of the tag - &amp;quot;[&amp;quot;.
            .element is the whole tag&amp;#039;s string, from &amp;quot;[&amp;quot; to &amp;quot;]&amp;quot;.
   Let `open_tags` be a stack of tags.
        Every element has the properties .name, .args, .content, .element and .offset
   Let `open_tags_by_name` be a dictionary of String -&amp;gt; Int.

2. For every tag `T` in `tags`:
       Is `T.is-opening` true?

       Then:
           Let `Parent` be the top element of `open_tags` (leave it on the stack).
           Append the substring between `Parent.offset` and `T.index` to `Parent.content`.
           Let `Parent.offset` be `T.index`.
           Let T2 be:
               .name : `T.name`, 
               .args : `T.args`, 
               .content : &amp;quot;&amp;quot;, 
               .index : `T.index` + `T.length`
               .element : `T.element`
           Push T2 onto `open_tags`.
           Increment `open_tags_by_name[T.name]` by 1.

       Else:
           If `open_tags_by_name[T.name] greater than 0?
           Then:
               Pop an element off `open_tags` and call it `Current`.
               While `T.name` is not equal to `Current.name`:
                   Decrement `open_tags[Current.name]` by 1.
                   Let `content` be `Current.element` concatenated with `Current.content`
                   Let `offset` be `Current.offset`
                   Pop an element off `open_tags` and call it `Current`.
                   Append `content` to `Current.content`
                   Let `Current.offset` be `offset`

               Let `Parent` be the top element of `open_tags` (leave it on the stack).
               Append the substring between `Current.offset` and `T.index` to `Current.content`
               Let `output` be the output of rendering `Current`
               Append `output` to `Parent.content`
               Decrement `open_tags[Current.name]` by 1.

3. Let `Current` be the top element of `open_tags` (leave it on the stack).
   Append the substring between `Current.offset` and the end of input to `Current.content`

4. While `open_tags` has more than 1 element:
       Pop an element off `open_tags` and call it `Current`
       Let `Parent` be the top element of `open_tags` (leave it on the stack).
       Append `Current.element` to `Parent.content`
       Append `Current.content` to `Parent.content`

5. Let `Root` be the only element of `open_tags`.
   Return `Root.content`&lt;/div&gt;&lt;/object&gt;
&lt;p&gt;You can see that even though the algorithm and the data it operates on is iterative, the text is rendered recursively from the inside out: Whenever a tag is closed, it is rendered and the output appended to the parent element&amp;#039;s content. &lt;/p&gt;
&lt;p&gt;If the tag being closed lies further down the stack, then all intermediate unclosed tags are discarded (though any &lt;em&gt;complete&lt;/em&gt; tags inside them are kept). In the end, the same is considered to happen to the virtual &amp;quot;root&amp;quot; tag, that encloses the entire input - unclosed tags are discarded. (Note that &amp;quot;discarded&amp;quot; means &amp;quot;displayed in the output as unrendered BBCode&amp;quot;, as is customary.)&lt;/p&gt;
&lt;h3&gt;The Costs&lt;/h3&gt;
&lt;p&gt;The first and greatest downside to this method is that tags cannot be weighted anymore. Every tag renderer will receive its content with all nested BBCode tags already rendered. Deferring the rendering of a tag is just not feasible in this algorithm.&lt;/p&gt;
&lt;p&gt;However, this can be trivially circumvented by using a feature that is a lot easier to implement. Tags can be given a &amp;quot;nocode&amp;quot; property, which will tell this algorithm to ignore any tags nested inside it. The tag renderer could then independently run the BBCode filter on its content to render the tags that were left unrendered earlier.&lt;/p&gt;
&lt;p&gt;(I&amp;#039;m not yet at the point where a dangling nocode tag will not stop enclosed tags from being rendered. It&amp;#039;s not trivial, alas.)&lt;/p&gt;
 &lt;!-- google_ad_section_end --&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/lhrvf09dEcGxOcPZsJtlkgBF9A8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lhrvf09dEcGxOcPZsJtlkgBF9A8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/lhrvf09dEcGxOcPZsJtlkgBF9A8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lhrvf09dEcGxOcPZsJtlkgBF9A8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/aranfoolcaytar/~4/zzphiBEVYUM" height="1" width="1"/&gt;</description>
     <comments>http://arancaytar.ermarian.net/2010/04/15/building-better-tag-parser#comments</comments>
 <category domain="http://arancaytar.ermarian.net/news/technology/web/drupal">Drupal</category>
 <category domain="http://arancaytar.ermarian.net/keyword/xbbcode">xbbcode</category>
 <wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://arancaytar.ermarian.net/crss/node/212</wfw:commentRss>
 <pubDate>Thu, 15 Apr 2010 22:15:02 +0000</pubDate>
 <dc:creator>Arancaytar</dc:creator>
 <guid isPermaLink="false">212 at http://arancaytar.ermarian.net</guid>
  <feedburner:origLink>http://arancaytar.ermarian.net/2010/04/15/building-better-tag-parser</feedburner:origLink></item>
  <item>
    <title>I am crazy</title>
    <link>http://feedproxy.google.com/~r/aranfoolcaytar/~3/1z1o4VW7jOg/i-am-crazy</link>
    <description>&lt;!-- google_ad_section_start --&gt; &lt;p&gt;During my first semester here in Frankfurt, I took five subjects and was unable to sleep more than six nights per week (that&amp;#039;s one too few, if you want to keep count). Surprisingly, I was still able to score a &lt;a href="http://en.wikipedia.org/wiki/Academic_grading_in_Germany#Universities" title="http://en.wikipedia.org/wiki/Academic_grading_in_Germany#Universities"&gt;1.0&lt;/a&gt; in three and a 1.7 in one of the courses (deferred testing for the fifth, Logic).&lt;/p&gt;
&lt;p&gt;So naturally, after barely surviving that, this semester I am taking &lt;em&gt;six&lt;/em&gt; subjects. Since two of these courses will be tested both with a written and an oral exam, and I&amp;#039;m planning to take the test for Logic this semester, that means that I&amp;#039;ll be facing nine tests instead of just four this time.&lt;/p&gt;
&lt;p&gt;There&amp;#039;s no way that is going to work.&lt;/p&gt;
&lt;p&gt;(Sounds like a challenge.)&lt;/p&gt;
 &lt;!-- google_ad_section_end --&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Wf9ImbhznyWsotPUu2LdpLwPEwg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Wf9ImbhznyWsotPUu2LdpLwPEwg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Wf9ImbhznyWsotPUu2LdpLwPEwg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Wf9ImbhznyWsotPUu2LdpLwPEwg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/aranfoolcaytar/~4/1z1o4VW7jOg" height="1" width="1"/&gt;</description>
     <comments>http://arancaytar.ermarian.net/2010/04/15/i-am-crazy#comments</comments>
 <category domain="http://arancaytar.ermarian.net/news/personal">Personal</category>
 <wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://arancaytar.ermarian.net/crss/node/211</wfw:commentRss>
 <pubDate>Thu, 15 Apr 2010 21:03:39 +0000</pubDate>
 <dc:creator>Arancaytar</dc:creator>
 <guid isPermaLink="false">211 at http://arancaytar.ermarian.net</guid>
  <feedburner:origLink>http://arancaytar.ermarian.net/2010/04/15/i-am-crazy</feedburner:origLink></item>
  <item>
    <title>Factorizing a large set of numbers fast</title>
    <link>http://feedproxy.google.com/~r/aranfoolcaytar/~3/UC9m-ToszS0/factorizing-large-set-numbers-fast</link>
    <description>&lt;!-- google_ad_section_start --&gt; &lt;p&gt;Factorizing a number can be done by several methods. The first is a naive way, which iterates over every natural number between 2 and the square root of n, then dividing by it as long as the number is divisible by it.&lt;/p&gt;
&lt;div class="hl-main"&gt;
&lt;pre&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;factor&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-identifier"&gt;factors&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-number"&gt;2&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;while&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt;*&lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt; &amp;lt;= &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt;:   &lt;/span&gt;&lt;span class="hl-comment"&gt;# Iterate up to the square root of n.&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;exp&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;       &lt;/span&gt;&lt;span class="hl-comment"&gt;# Start with exponent 0&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;while&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt; % &lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;is&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;:  &lt;/span&gt;&lt;span class="hl-comment"&gt;# Increment exponent and divide while divisible.&lt;/span&gt;&lt;span class="hl-code"&gt;
            &lt;/span&gt;&lt;span class="hl-identifier"&gt;exp&lt;/span&gt;&lt;span class="hl-code"&gt; += &lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;
            &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt; /= &lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;if&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;exp&lt;/span&gt;&lt;span class="hl-code"&gt; &amp;gt; &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;:
            &lt;/span&gt;&lt;span class="hl-comment"&gt;# Note: All prime factors of n that are less than i are&lt;/span&gt;&lt;span class="hl-code"&gt;
            &lt;/span&gt;&lt;span class="hl-comment"&gt;# already gone, therefore i itself is definitely prime if we're here.&lt;/span&gt;&lt;span class="hl-code"&gt;
            &lt;/span&gt;&lt;span class="hl-identifier"&gt;factors&lt;/span&gt;&lt;span class="hl-code"&gt;.&lt;/span&gt;&lt;span class="hl-identifier"&gt;append&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;exp&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-comment"&gt;# Append the prime factor.&lt;/span&gt;&lt;span class="hl-code"&gt;
            &lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt; += &lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-comment"&gt;# If n is not yet 1, then it is now prime.&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;if&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt; &amp;gt; &lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;factors&lt;/span&gt;&lt;span class="hl-code"&gt;.&lt;/span&gt;&lt;span class="hl-identifier"&gt;append&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
&lt;/span&gt;&lt;span class="hl-reserved"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;factors&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This requires us to iterate over n^0.5 numbers in the worst case (which is when n is prime). An upper bound for factorizing all numbers 1&amp;lt;x&amp;lt;n is O(n^1.5) (though admittedly that is a loose upper bound since 1&amp;lt;n&amp;lt;10^&lt;sup&gt;4&lt;/sup&gt; contains 12.3% prime numbers, 1&amp;lt;n&amp;lt;10^&lt;sup&gt;5&lt;/sup&gt; has 9.6% and 1&amp;lt;n&amp;lt;10^&lt;sup&gt;6&lt;/sup&gt; has only 7.8%. Still, the ratio changes slowly enough to be considered constant.&lt;/p&gt;
&lt;p&gt;A slight performance improvement can be had by pre-calculating the set of prime numbers between i and n (which can be done in O(n*log(n*log(n)*loglog(n)) time with the &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Erastothenes" title="reference on Sieve of Erastothenes"&gt;Sieve of Erastothenes&lt;/a&gt;) and then &lt;em&gt;reusing&lt;/em&gt; it for every number to be factored. After incurring an initial linear-logarithmic cost, we then do the following:&lt;/p&gt;
&lt;div class="hl-main"&gt;
&lt;pre&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;factor&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;global&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;primes&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-identifier"&gt;factors&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;for&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;p&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;in&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;primes&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;exp&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;while&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt; % &lt;/span&gt;&lt;span class="hl-identifier"&gt;p&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;is&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;:
            &lt;/span&gt;&lt;span class="hl-identifier"&gt;exp&lt;/span&gt;&lt;span class="hl-code"&gt; += &lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;
            &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt; /= &lt;/span&gt;&lt;span class="hl-identifier"&gt;p&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;if&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;exp&lt;/span&gt;&lt;span class="hl-code"&gt; &amp;gt; &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;:
            &lt;/span&gt;&lt;span class="hl-identifier"&gt;factors&lt;/span&gt;&lt;span class="hl-code"&gt;.&lt;/span&gt;&lt;span class="hl-identifier"&gt;append&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;p&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;exp&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;if&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;p&lt;/span&gt;&lt;span class="hl-code"&gt;*&lt;/span&gt;&lt;span class="hl-identifier"&gt;p&lt;/span&gt;&lt;span class="hl-code"&gt; &amp;gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt;:
            &lt;/span&gt;&lt;span class="hl-reserved"&gt;break&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;if&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt; &amp;gt; &lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;factors&lt;/span&gt;&lt;span class="hl-code"&gt;.&lt;/span&gt;&lt;span class="hl-identifier"&gt;append&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;factors&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It is apparent, however, that this produces merely the practically constant improvement of only checking prime numbers instead of all numbers. It&amp;#039;s down by a factor 10 if n is small (up to maybe 20 when n is 10^8), but it&amp;#039;s still in the O(n^1.5) upper bound.&lt;/p&gt;
&lt;p&gt;Can we improve on this?&lt;/p&gt;
&lt;p&gt;Yes.&lt;/p&gt;
&lt;p&gt;The Sieve of Erastothenes can be slightly modified without changing its O(n*log(n)*loglog(n)) complexity, and the modified sieve will let us factor primes in constant and any number in logarithmic time.&lt;/p&gt;
&lt;p&gt;First, the modified sieve:&lt;/p&gt;
&lt;div class="hl-main"&gt;
&lt;pre&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;sieve&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-comment"&gt;# We need to fill the first 2 spots with 0's to cleanly ignore the case i &amp;lt; 2.&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-identifier"&gt;table&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt;*&lt;/span&gt;&lt;span class="hl-number"&gt;2&lt;/span&gt;&lt;span class="hl-code"&gt; + &lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt; * &lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt;-&lt;/span&gt;&lt;span class="hl-number"&gt;2&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-comment"&gt;# A list filled with 2 0's and n-1 1's.&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;for&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;in&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-builtin"&gt;xrange&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;if&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;table&lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt;-&lt;/span&gt;&lt;span class="hl-number"&gt;7&lt;/span&gt;&lt;span class="hl-code"&gt;-&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;is&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;:
            &lt;/span&gt;&lt;span class="hl-reserved"&gt;for&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;j&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;in&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-builtin"&gt;xrange&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-number"&gt;2&lt;/span&gt;&lt;span class="hl-code"&gt;*&lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;: &lt;/span&gt;&lt;span class="hl-comment"&gt;# Iterate over all multiples of i&lt;/span&gt;&lt;span class="hl-code"&gt;
                &lt;/span&gt;&lt;span class="hl-identifier"&gt;table&lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-identifier"&gt;j&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-identifier"&gt;i&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-comment"&gt;# This is where we'd normally mark j as not prime.&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;table&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The output of this algorithm is a list that assigns each non-prime number its largest prime factor, and each prime number the number 1.&lt;/p&gt;
&lt;p&gt;For example, the output for 0&amp;lt;=n&amp;lt;=10 is:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;[0, 0, 1, 1, 2, 1, 3, 1, 2, 3, 5]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;When factorizing now, we just need to look into this table to find the next prime factor, instead of finding it by iteration:&lt;/p&gt;
&lt;div class="hl-main"&gt;
&lt;pre&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;factor&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;global&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;sieve&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;if&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;sieve&lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt; &amp;gt; &lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-comment"&gt;# Divide n by the prime factor and recurse.&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-identifier"&gt;sieve&lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt; + &lt;/span&gt;&lt;span class="hl-identifier"&gt;factor&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt; / &lt;/span&gt;&lt;span class="hl-identifier"&gt;sieve&lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-comment"&gt;# n is prime, stop recursion.&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;(Note that the output of this algorithm is not identical to the previous. Instead of [(2, 5), (5, 5)] for 10^5, it will produce [5, 5, 5, 5, 5, 2, 2, 2, 2, 2].)&lt;/p&gt;
&lt;p&gt;What was our worst case earlier is now the best: If a number is prime, the algorithm will immediately halt. The best case from earlier (a small prime to a high power, like 2^20) is now the worst case - but &lt;em&gt;it will still be done in logarithmic time&lt;/em&gt;. 2^20 needs 20 recursions and so on. (If the recursion depth bothers you, the algorithm can also be formulated iteratively.)&lt;/p&gt;
&lt;p&gt;Since our worst case for a single number is now logarithmic, the whole problem has gone from O(n^1.5) to O(n*log(n)), which is about the complexity we spent building the sieve in the first place. From linear-root to linear-logarithmic is quite an improvement for big numbers.&lt;/p&gt;
 &lt;!-- google_ad_section_end --&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/G5qj7acwn1yUnNIuoH2FRuj-seI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/G5qj7acwn1yUnNIuoH2FRuj-seI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/G5qj7acwn1yUnNIuoH2FRuj-seI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/G5qj7acwn1yUnNIuoH2FRuj-seI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/aranfoolcaytar/~4/UC9m-ToszS0" height="1" width="1"/&gt;</description>
     <comments>http://arancaytar.ermarian.net/2010/03/03/factorizing-large-set-numbers-fast#comments</comments>
 <category domain="http://arancaytar.ermarian.net/news/technology/programming/algorithms">Algorithms</category>
 <category domain="http://arancaytar.ermarian.net/keyword/factorization">factorization</category>
 <category domain="http://arancaytar.ermarian.net/keyword/mathematics">mathematics</category>
 <category domain="http://arancaytar.ermarian.net/keyword/numbers">numbers</category>
 <wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://arancaytar.ermarian.net/crss/node/210</wfw:commentRss>
 <pubDate>Wed, 03 Mar 2010 09:32:48 +0000</pubDate>
 <dc:creator>Arancaytar</dc:creator>
 <guid isPermaLink="false">210 at http://arancaytar.ermarian.net</guid>
  <feedburner:origLink>http://arancaytar.ermarian.net/2010/03/03/factorizing-large-set-numbers-fast</feedburner:origLink></item>
  <item>
    <title>Goedelization of first-order logic</title>
    <link>http://feedproxy.google.com/~r/aranfoolcaytar/~3/1yUPEj7cS9M/goedelization-first-order-logic</link>
    <description>&lt;!-- google_ad_section_start --&gt; &lt;p&gt;In our Logic class, we just started on Goedelization of logical formulae - that is, the encoding of the language of first-order (arithmetic) logic into numbers, such that every formula gets a unique natural number. (For an analogy, take the English language and consider every word as a number in base 27, where &amp;quot;a&amp;quot; is 1 and &amp;quot;z&amp;quot; is 26. Something similar works for logic.)&lt;/p&gt;
&lt;p&gt;Needless to say, these numbers are unbelievably large for even very short formulae.&lt;/p&gt;
&lt;p&gt;For example, the first four natural numbers themselves are (of course) terms in arithmetical logic. However, the language does not actually allow writing these numbers out in their literal form: Rather, a term must be constructed only of the basic operators (&amp;quot;+&amp;quot; and &amp;quot;*&amp;quot;) and the basic constants (&amp;quot;0&amp;quot; and &amp;quot;1&amp;quot;), and every term must be enclosed in parentheses.&lt;/p&gt;

&lt;object&gt;&lt;div class="codeblock"&gt;0 and 1 remain 0 and 1.
2 becomes (1+1)
3 becomes ((1+1)+1)
4 becomes (((1+1)+1)+1)
...&lt;/div&gt;&lt;/object&gt;
&lt;p&gt;Our encoding specifies 0xd for 0, 0xe for 1, 0x7 and 0x8 for parentheses, and 0xc for +.&lt;/p&gt;
&lt;p&gt;So the hexadecimal code for the term equivalent to 3 would read:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;0x77ece8ce8&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The decimal form of this number is 32191187944. That&amp;#039;s our Goedel number of the natural number 3.&lt;/p&gt;
&lt;p&gt;(You don&amp;#039;t want to know what the Goedel number for 32191187944 is. I did, but my inefficient recursive function crapped out.)&lt;/p&gt;
&lt;p&gt;Here&amp;#039;s an algorithm for generating the representation and then converting it to decimal:&lt;/p&gt;
&lt;div class="hl-main"&gt;
&lt;pre&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;basic_num&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;if&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt; &amp;lt; &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;raise&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;ValueError&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;elif&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt; &amp;lt;= &lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-builtin"&gt;str&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;else&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-reserved"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;(&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt; + &lt;/span&gt;&lt;span class="hl-identifier"&gt;basic_num&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;n&lt;/span&gt;&lt;span class="hl-code"&gt;-&lt;/span&gt;&lt;span class="hl-number"&gt;1&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt; + &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;+1)&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;
 
&lt;/span&gt;&lt;span class="hl-reserved"&gt;def&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;goedel&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;s&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;if&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-builtin"&gt;type&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;s&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;is&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;int&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;s&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-identifier"&gt;basic_num&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;s&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-identifier"&gt;code&lt;/span&gt;&lt;span class="hl-code"&gt; = {
        &lt;/span&gt;&lt;span class="hl-comment"&gt;# Logical operators&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;~&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-identifier"&gt;x2&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-comment"&gt;# NOT&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;&amp;amp;&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-identifier"&gt;x3&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-comment"&gt;# AND&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;|&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-identifier"&gt;x4&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-comment"&gt;# OR&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;E&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-identifier"&gt;x5&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-comment"&gt;# EXISTS&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;A&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-identifier"&gt;x6&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-comment"&gt;# ALL&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;(&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-identifier"&gt;x7&lt;/span&gt;&lt;span class="hl-code"&gt;,
        &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;)&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-identifier"&gt;x8&lt;/span&gt;&lt;span class="hl-code"&gt;,
        &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;=&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-identifier"&gt;x9&lt;/span&gt;&lt;span class="hl-code"&gt;,
 
        &lt;/span&gt;&lt;span class="hl-comment"&gt;# Arithmetic operators&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;&amp;lt;&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-identifier"&gt;xa&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-comment"&gt;# &amp;lt;=&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;+&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-identifier"&gt;xb&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-comment"&gt;# ADD&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;*&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-identifier"&gt;xc&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-comment"&gt;# MULT&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;0&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-identifier"&gt;xd&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-comment"&gt;# 0&lt;/span&gt;&lt;span class="hl-code"&gt;
        &lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-string"&gt;1&lt;/span&gt;&lt;span class="hl-quotes"&gt;&amp;quot;&lt;/span&gt;&lt;span class="hl-code"&gt;:&lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-identifier"&gt;xe&lt;/span&gt;&lt;span class="hl-code"&gt;, &lt;/span&gt;&lt;span class="hl-comment"&gt;# 1&lt;/span&gt;&lt;span class="hl-code"&gt;
    }
 
    &lt;/span&gt;&lt;span class="hl-identifier"&gt;g&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-number"&gt;0&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;for&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;char&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-reserved"&gt;in&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-builtin"&gt;str&lt;/span&gt;&lt;span class="hl-brackets"&gt;(&lt;/span&gt;&lt;span class="hl-identifier"&gt;s&lt;/span&gt;&lt;span class="hl-brackets"&gt;)&lt;/span&gt;&lt;span class="hl-code"&gt;:
        &lt;/span&gt;&lt;span class="hl-identifier"&gt;g&lt;/span&gt;&lt;span class="hl-code"&gt; = &lt;/span&gt;&lt;span class="hl-identifier"&gt;g&lt;/span&gt;&lt;span class="hl-code"&gt; * &lt;/span&gt;&lt;span class="hl-number"&gt;16&lt;/span&gt;&lt;span class="hl-code"&gt; + &lt;/span&gt;&lt;span class="hl-identifier"&gt;code&lt;/span&gt;&lt;span class="hl-brackets"&gt;[&lt;/span&gt;&lt;span class="hl-identifier"&gt;char&lt;/span&gt;&lt;span class="hl-brackets"&gt;]&lt;/span&gt;&lt;span class="hl-code"&gt;
    &lt;/span&gt;&lt;span class="hl-reserved"&gt;return&lt;/span&gt;&lt;span class="hl-code"&gt; &lt;/span&gt;&lt;span class="hl-identifier"&gt;g&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
 &lt;!-- google_ad_section_end --&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/VSSF3lGj69G88udcZZTpQp5VfEQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VSSF3lGj69G88udcZZTpQp5VfEQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/VSSF3lGj69G88udcZZTpQp5VfEQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VSSF3lGj69G88udcZZTpQp5VfEQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/aranfoolcaytar/~4/1yUPEj7cS9M" height="1" width="1"/&gt;</description>
     <comments>http://arancaytar.ermarian.net/2010/01/26/goedelization-first-order-logic#comments</comments>
 <category domain="http://arancaytar.ermarian.net/news/technology/programming/algorithms">Algorithms</category>
 <category domain="http://arancaytar.ermarian.net/keyword/goedel">Goedel</category>
 <category domain="http://arancaytar.ermarian.net/keyword/logic">Logic</category>
 <category domain="http://arancaytar.ermarian.net/keyword/python">python</category>
 <wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://arancaytar.ermarian.net/crss/node/209</wfw:commentRss>
 <pubDate>Tue, 26 Jan 2010 04:53:42 +0000</pubDate>
 <dc:creator>Arancaytar</dc:creator>
 <guid isPermaLink="false">209 at http://arancaytar.ermarian.net</guid>
  <feedburner:origLink>http://arancaytar.ermarian.net/2010/01/26/goedelization-first-order-logic</feedburner:origLink></item>
  </channel>
</rss>

