<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dreamshire &#187; Perl</title>
	<atom:link href="http://blog.dreamshire.com/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dreamshire.com</link>
	<description>Showing what's behind the curtain</description>
	<lastBuildDate>Wed, 01 Sep 2010 02:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Project Euler Problem 59 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/28/project-euler-problem-59-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/28/project-euler-problem-59-solution/#comments</comments>
		<pubDate>Fri, 29 May 2009 03:04:21 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 50-59]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1482</guid>
		<description><![CDATA[Using a brute force attack, can you decrypt the cipher using XOR encryption?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Each character on a computer is assigned a unique code and the preferred standard is ASCII (American Standard Code for Information Interchange). For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107.</p>
<p>A modern encryption method is to take a text file, convert the bytes to ASCII, then XOR each byte with a given value, taken from a secret key. The advantage with the XOR function is that using the same encryption key on the cipher text, restores the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65.</p>
<p>For unbreakable encryption, the key is the same length as the plain text message, and the key is made up of random bytes. The user would keep the encrypted message and the encryption key in different locations, and without both &quot;halves&quot;, it is impossible to decrypt the message.</p>
<p>Unfortunately, this method is impractical for most users, so the modified method is to use a password as a key. If the password is shorter than the message, which is likely, the key is repeated cyclically throughout the message. The balance for this method is using a sufficiently long password key for security, but short enough to be memorable.</p>
<p>Your task has been made easy, as the encryption key consists of three lower case characters. Using <a href='http://projecteuler.net/project/cipher1.txt'>cipher1.txt</a> (right click and &#8216;Save Link/Target As&#8230;&#8217;), a file containing the encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the ASCII values in the original text.</p>
<h4><u>Analysis</u></h4>
<p>Following the instructions, we read the file and convert to ASCII.  The decryption process is simplified by limiting the key to three characters from the set {a..z} for only 17,576 possibilities.</p>
<p>Perl makes the process easier with built in support for regular expressions.  Taking the hint that the message contains common English words we look for the string &#8216; the &#8216; after each attempt at decryption.  Surrounding our word with spaces guarantees it&#8217;s not part of another word, such as &#8216;they&#8217;.</p>
<p>We have to repeat each key cyclically until it&#8217;s the same length as the message.  For example, if the message is &#8220;gduii9iu&#8221; and the password is &#8216;abc&#8217; we must decrypt with &#8216;abcabcab&#8217;.<br />
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">open</span> <span style="color: #009900;">&#40;</span>IN<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;&lt;cipher1.txt&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Can't read file: $!&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #0000ff;">$message</span> <span style="color: #339933;">=</span> <span style="color: #000066;">join</span> <span style="color: #ff0000;">''</span><span style="color: #339933;">,</span> <span style="color: #000066;">map</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span> <span style="color: #000066;">split</span> <span style="color: #009966; font-style: italic;">/,/</span><span style="color: #339933;">,</span> <span style="color: #009999;">&lt;IN&gt;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">#read file &amp; convert to ASCII</span>
&nbsp;
<span style="color: #0000ff;">$l</span> <span style="color: #339933;">=</span> <span style="color: #000066;">length</span> <span style="color: #0000ff;">$message</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key</span> <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'aaa'</span> <span style="color: #339933;">..</span> <span style="color: #ff0000;">'zzz'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$message</span> <span style="color: #339933;">^</span> <span style="color: #000066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$key</span> x <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$l</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">3</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$l</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">last</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$text</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/ the /i</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #0000ff;">$s</span> <span style="color: #339933;">+=</span> <span style="color: #000066;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">for</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span> <span style="color: #0000ff;">$text</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE59: &quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$s</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<ul>
<li>&#8216;the&#8217; was selected because it is the most common English word.</li>
<li><em>chr() </em>and <em>ord() </em>convert to/from ASCII/decimal.</li>
<li>&#8216;^&#8217; is Perl&#8217;s XOR operator.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/28/project-euler-problem-59-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 51 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/27/project-euler-problem-51-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/27/project-euler-problem-51-solution/#comments</comments>
		<pubDate>Wed, 27 May 2009 08:48:55 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 50-59]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=640</guid>
		<description><![CDATA[Find the smallest prime which, by changing the same part of the number, can form eight different primes.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>By replacing the 1<img src="" style="display:none;" alt="^(" /><sup>st</sup><img src="" style="display:none;" alt=")" /> digit of *57, it turns out that six of the possible values: 157, 257, 457, 557, 757, and 857, are all prime.</p>
<p>By replacing the 3<img src="" style="display:none;" alt="^(" /><sup>rd</sup><img src="" style="display:none;" alt=")" /> and 4<img src="" style="display:none;" alt="^(" /><sup>th</sup><img src="" style="display:none;" alt=")" /> digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.</p>
<p>Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.</p>
<h4><u>Analysis</u></h4>
<p>It looked obvious from the example that the minimum prime value for an 8 prime family had to have at least 6 digits.  For that size of prime it would take at least 3 digits to have to be replaced.  The last digit couldn&#8217;t be part of the replacement because that would make it non-prime.</p>
<p>So we took all 68,906 6-digit primes and reduced it to 11,066 by keeping only those primes that had 3 or more repeating digits.  We also remembered which digit was repeating so that replacing it would be easy.</p>
<p>All that was left was to iterate through the primes and report the first one that followed the instructions outlined above.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">open</span> IN<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;&lt;primes51.txt&quot;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Cannot open file: $!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">%primes</span> <span style="color: #339933;">=</span> <span style="color: #000066;">map</span> <span style="color: #009900;">&#123;</span><span style="color: #000066;">chomp</span><span style="color: #339933;">;</span> <span style="color: #000066;">split</span> <span style="color: #339933;">/,/</span><span style="color: #009900;">&#125;</span> <span style="color: #009999;">&lt;IN&gt;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$p</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">sort</span> <span style="color: #000066;">keys</span> <span style="color: #0000ff;">%primes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$dd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$primes</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$p</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$d</span> <span style="color: #009900;">&#40;</span> <span style="color: #000066;">grep</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">$dd</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#40;</span>0<span style="color: #339933;">..</span>9<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">last</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$c</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$xx</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$p</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$xx</span><span style="color: #339933;">=~</span><span style="color: #000066;">s</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$dd</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$d</span><span style="color: #339933;">/</span>g<span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">++</span> <span style="color: #b1b100;">if</span> <span style="color: #000066;">exists</span> <span style="color: #0000ff;">$primes</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$xx</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$i</span><span style="color: #339933;">++;</span>
    <span style="color: #b1b100;">next</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE50 = $p&quot;</span><span style="color: #339933;">;</span>exit<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>Example of what file &#8216;primes51.txt&#8217; looks like:<br />
104999,9<br />
105211,1<br />
105557,5<br />
106121,1</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/27/project-euler-problem-51-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 40 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/25/project-euler-problem-40-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/25/project-euler-problem-40-solution/#comments</comments>
		<pubDate>Mon, 25 May 2009 20:05:07 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/2009/05/25/project-euler-problem-40-solution/</guid>
		<description><![CDATA[Finding the nth digit of the fractional part of the irrational number.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>An irrational decimal fraction is created by concatenating the positive integers:</p>
<p style='text-align:center;'>0.12345678910<span style='color:#dd0000;font-size:14pt;'>1</span>112131415161718192021&#8230;</p>
<p>It can be seen that the 12<sup>th</sup> digit of the fractional part is 1.</p>
<p>If <i>d</i><sub><i>n</i></sub> represents the <i>n</i><sup>th</sup> digit of the fractional part, find the value of the following expression.</p>
<p style='text-align:center;'><i>d</i><sub>1</sub> &times; <i>d</i><sub>10</sub> &times; <i>d</i><sub>100</sub> &times; <i>d</i><sub>1000</sub> &times; <i>d</i><sub>10000</sub> &times; <i>d</i><sub>100000</sub> &times; <i>d</i><sub>1000000</sub></p>
<h4><u>Analysis</u></h4>
<p>This is known as Champernowne&#8217;s constant (OEIS <a href="http://www.research.att.com/~njas/sequences/A033307">A033307</a>).</p>
<p>We concatenate the counting numbers to a string from 1 until the length reaches at least 1 million digits, then multiply the digit at the appropriate positions together.  We know from the problem description that the first two terms <i>d</i><sub>1</sub> &amp; <i>d</i><sub>10</sub> will evaluate to 1 and, therefore, serve no consequence on the product.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$s</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'.'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$n</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$s</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">$n</span><span style="color: #339933;">++</span> <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$s</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">1</span>_000_000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE40 = &quot;</span><span style="color: #339933;">,</span>
<span style="color: #000066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$s</span><span style="color: #339933;">,</span>1e2<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #000066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$s</span><span style="color: #339933;">,</span>1e3<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #000066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$s</span><span style="color: #339933;">,</span>1e4<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #000066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$s</span><span style="color: #339933;">,</span>1e5<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #000066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$s</span><span style="color: #339933;">,</span>1e6<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<ul>
<li>We initialize $s with a character to start the string index at 1 instead of 0.</li>
<li><em>Mathematica: </em><tt>Times @@ Flatten[IntegerDigits@Range@2*^5][[10^Range@6]]</tt></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/25/project-euler-problem-40-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 8 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/17/project-euler-problem-8-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/17/project-euler-problem-8-solution/#comments</comments>
		<pubDate>Mon, 18 May 2009 05:54:12 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 1-9]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1271</guid>
		<description><![CDATA[Discover the largest product of five consecutive digits in the 1000-digit number.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Find the greatest product of five consecutive digits in the 1000-digit number.</p>
<p style='font-family:courier new;font-size:10pt;text-align:center;'>
73167176531330624919225119674426574742355349194934<br />
96983520312774506326239578318016984801869478851843<br />
  &#8230; {continued}
</p>
<h4><u>Analysis</u></h4>
<p>Starting with the largest 5 digit number, 99999, count downwards until that number is a substring of the 1000 digit number.  Split out the five digits and multiply them together.  Ignore any products that could evaluate to zero.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">$string</span> <span style="color: #339933;">=</span> <span style="color: #009999;">&lt;DATA&gt;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$p</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">99999</span><span style="color: #339933;">;</span> <span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span> <span style="color: #339933;">!~</span> <span style="color: #009966; font-style: italic;">/0/</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">$string</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/$i/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$p</span><span style="color: #339933;">*=</span><span style="color: #0000ff;">$_</span> <span style="color: #b1b100;">for</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE8 = $p&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">exit</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">__DATA__</span>
731671765313306249<span style="color: #339933;">...</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>Finds the maximum in 121 iterations.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/17/project-euler-problem-8-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 124 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/15/project-euler-problem-124-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/15/project-euler-problem-124-solution/#comments</comments>
		<pubDate>Fri, 15 May 2009 17:21:00 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/2009/05/15/project-euler-problem-124-solution/</guid>
		<description><![CDATA[Determining the kth element of the sorted radical function.
]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The radical of <i>n</i>, rad(<i>n</i>), is the product of distinct prime factors of <i>n</i>. For example, 504 = 2<img src="" style="display:none;" alt="^(" /><sup>3</sup><img src="" style="display:none;" alt=")" /> &times; 3<img src="" style="display:none;" alt="^(" /><sup>2</sup><img src="" style="display:none;" alt=")" /> &times; 7, so rad(504) = 2 &times; 3 &times; 7 = 42.</p>
<p>If we calculate rad(<i>n</i>) for <i>1</i> &le; <i>n</i> &le; 10, then sort them on rad(<i>n</i>), and sorting on <i>n</i> if the radical values are equal, we get:</p>
<table cellpadding='2' cellspacing='0' border='0' align='center'>
<tr>
<td colspan='2'>
<div style='text-align:center;'><b>Unsorted</b></div>
</td>
<td>&nbsp;</td>
<td colspan='3'>
<div style='text-align:center;'><b>Sorted</b></div>
</td>
</tr>
<tr>
<td>
<div style='text-align:center;'><b><i>n</i></b></div>
</td>
<td>
<div style='text-align:center;'><b>rad(<i>n</i>)</b></div>
</td>
<td></td>
<td>
<div style='text-align:center;'><b><i>n</i></b></div>
</td>
<td>
<div style='text-align:center;'><b>rad(<i>n</i>)</b></div>
</td>
<td>
<div style='text-align:center;'><b>k</b></div>
</td>
</tr>
<tr>
<td>
<div style='text-align:center;'>1</div>
</td>
<td>
<div style='text-align:center;'>1</div>
</td>
<td>&nbsp;</td>
<td>
<div style='text-align:center;'>1</div>
</td>
<td>
<div style='text-align:center;'>1</div>
</td>
<td>
<div style='text-align:center;'>1</div>
</td>
</tr>
<tr>
<td>
<div style='text-align:center;'>2</div>
</td>
<td>
<div style='text-align:center;'>2</div>
</td>
<td>&nbsp;</td>
<td>
<div style='text-align:center;'>2</div>
</td>
<td>
<div style='text-align:center;'>2</div>
</td>
<td>
<div style='text-align:center;'>2</div>
</td>
</tr>
<tr>
<td>
<div style='text-align:center;'>3</div>
</td>
<td>
<div style='text-align:center;'>3</div>
</td>
<td>&nbsp;</td>
<td>
<div style='text-align:center;'>4</div>
</td>
<td>
<div style='text-align:center;'>2</div>
</td>
<td>
<div style='text-align:center;'>3</div>
</td>
</tr>
<tr>
<td>
<div style='text-align:center;'>4</div>
</td>
<td>
<div style='text-align:center;'>2</div>
</td>
<td>&nbsp;</td>
<td>
<div style='text-align:center;'>8</div>
</td>
<td>
<div style='text-align:center;'>2</div>
</td>
<td>
<div style='text-align:center;'>4</div>
</td>
</tr>
<tr>
<td>
<div style='text-align:center;'>5</div>
</td>
<td>
<div style='text-align:center;'>5</div>
</td>
<td>&nbsp;</td>
<td>
<div style='text-align:center;'>3</div>
</td>
<td>
<div style='text-align:center;'>3</div>
</td>
<td>
<div style='text-align:center;'>5</div>
</td>
</tr>
<tr>
<td>
<div style='text-align:center;'>6</div>
</td>
<td>
<div style='text-align:center;'>6</div>
</td>
<td>&nbsp;</td>
<td>
<div style='text-align:center;'>9</div>
</td>
<td>
<div style='text-align:center;'>3</div>
</td>
<td>
<div style='text-align:center;'>6</div>
</td>
</tr>
<tr>
<td>
<div style='text-align:center;'>7</div>
</td>
<td>
<div style='text-align:center;'>7</div>
</td>
<td>&nbsp;</td>
<td>
<div style='text-align:center;'>5</div>
</td>
<td>
<div style='text-align:center;'>5</div>
</td>
<td>
<div style='text-align:center;'>7</div>
</td>
</tr>
<tr>
<td>
<div style='text-align:center;'>8</div>
</td>
<td>
<div style='text-align:center;'>2</div>
</td>
<td>&nbsp;</td>
<td>
<div style='text-align:center;'>6</div>
</td>
<td>
<div style='text-align:center;'>6</div>
</td>
<td>
<div style='text-align:center;'>8</div>
</td>
</tr>
<tr>
<td>
<div style='text-align:center;'>9</div>
</td>
<td>
<div style='text-align:center;'>3</div>
</td>
<td>&nbsp;</td>
<td>
<div style='text-align:center;'>7</div>
</td>
<td>
<div style='text-align:center;'>7</div>
</td>
<td>
<div style='text-align:center;'>9</div>
</td>
</tr>
<tr>
<td>
<div style='text-align:center;'>10</div>
</td>
<td>
<div style='text-align:center;'>10</div>
</td>
<td>&nbsp;</td>
<td>
<div style='text-align:center;'>10</div>
</td>
<td>
<div style='text-align:center;'>10</div>
</td>
<td>
<div style='text-align:center;'>10</div>
</td>
</tr>
</table>
<p>Let E(<i>k</i>) be the <i>k</i>th element in the sorted <i>n</i> column; for example, E(4) = 8 and E(6) = 9.</p>
<p>If rad(<i>n</i>) is sorted for 1 &le; <i>n</i> &le; 100000, find E(10000).</p>
<h4><u>Analysis</u></h4>
<p>This problem can be solved without sorting or a large list of primes by just calculating factors as we concatenate them to a group organized by rad(n).  To find n, just concatenate the group together and print the index relative to zero in the array.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">$limit</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100000</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$target</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">10000</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$rad</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>2<span style="color: #339933;">..</span><span style="color: #0000ff;">$limit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$buffer</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$n</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$n</span> <span style="color: #339933;">&lt;=</span> <span style="color: #0000ff;">$limit</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$n</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$rad</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$j</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$n</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$j</span> <span style="color: #339933;">&lt;=</span> <span style="color: #0000ff;">$limit</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$j</span> <span style="color: #339933;">+=</span> <span style="color: #0000ff;">$n</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #0000ff;">$rad</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$j</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*=</span> <span style="color: #0000ff;">$n</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #0000ff;">$buffer</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$rad</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.=</span><span style="color: #ff0000;">&quot;$n &quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$e</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">map</span> <span style="color: #009900;">&#123;</span><span style="color: #000066;">split</span> <span style="color: #339933;">/</span><span style="color: #000066;">s</span><span style="color: #339933;">+/</span><span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@buffer</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$target</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE124 = $e&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/15/project-euler-problem-124-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 17 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/13/project-euler-problem-17-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/13/project-euler-problem-17-solution/#comments</comments>
		<pubDate>Wed, 13 May 2009 21:44:56 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 10-19]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1210</guid>
		<description><![CDATA[How many letters would be needed to write all the numbers in words from 1 to 1000?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.</p>
<p>If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? </p>
<p>
<strong>NOTE:</strong> Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of &#8220;and&#8221; when writing out numbers is in compliance with British usage.</p>
<h4><u>Analysis</u></h4>
<p>Converted the integers from 1 to 999 to the written equivalent, removed spaces and hyphens, added the character count for each one, including &#8220;onethousand&#8221;.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">@D</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">..</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">70</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">90</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #000066;">qw</span><span style="color: #339933;">|</span>
 zero one two three four five six seven eight nine ten
 eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
 twenty thirty forty fifty sixty seventy eighty ninety<span style="color: #339933;">|;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$i</span> <span style="color: #009900;">&#40;</span>1<span style="color: #339933;">..</span>999<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$x</span><span style="color: #339933;">=</span> num2en<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$x</span><span style="color: #339933;">=~</span><span style="color: #000066;">s</span><span style="color: #339933;">/</span><span style="color: #009900;">&#91;</span>s<span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">//</span>g<span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$l</span><span style="color: #339933;">+=</span> <span style="color: #000066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE17 = &quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$l</span><span style="color: #339933;">+</span><span style="color: #000066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'onethousand'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> num2en <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">return</span> <span style="color: #0000ff;">$D</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$x</span><span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">if</span> <span style="color: #000066;">defined</span> <span style="color: #0000ff;">$D</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$x</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">return</span>  <span style="color: #0000ff;">$D</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$1</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">'0'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">'-'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$D</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$2</span><span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$x</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">m/^(.)(.)$/</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$x</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">m/^(.)(..)$/</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$h</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$rest</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;$D{$1} hundred&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066;">return</span> <span style="color: #0000ff;">$h</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$rest</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'00'</span><span style="color: #339933;">;</span>
      <span style="color: #000066;">return</span> <span style="color: #ff0000;">&quot;$h and &quot;</span> <span style="color: #339933;">.</span> num2en<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$rest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/13/project-euler-problem-17-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 82 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/05/project-euler-problem-82-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/05/project-euler-problem-82-solution/#comments</comments>
		<pubDate>Wed, 06 May 2009 06:29:26 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 80-89]]></category>
		<category><![CDATA[Common Routines]]></category>
		<category><![CDATA[Dynamic Programming (DP)]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=717</guid>
		<description><![CDATA[Find the minimal path sum from the left column to the right column.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p class='info'>NOTE: This problem is a more challenging version of Problem 81.</p>
<p>The minimal path sum in the 5 by 5 matrix below, by starting in any cell in the left column and finishing in any cell in the right column, and only moving up, down, and right, is indicated in red; the sum is equal to 994.</p>
<div style='text-align:center;'>
<table cellpadding='3' cellspacing='0' border='0'>
<tr>
<td>131</td>
<td>673</td>
<td><span style='color:#dd0000;'>234</span></td>
<td><span style='color:#dd0000;'>103</span></td>
<td><span style='color:#dd0000;'>18</span></td>
</tr>
<tr>
<td><span style='color:#dd0000;'>201</span></td>
<td><span style='color:#dd0000;'>96</span></td>
<td><span style='color:#dd0000;'>342</span></td>
<td>965</td>
<td>150</td>
</tr>
<tr>
<td>630</td>
<td>803</td>
<td>746</td>
<td>422</td>
<td>111</td>
</tr>
<tr>
<td>537</td>
<td>699</td>
<td>497</td>
<td>121</td>
<td>956</td>
</tr>
<tr>
<td>805</td>
<td>732</td>
<td>524</td>
<td>37</td>
<td>331</td>
</tr>
</table>
</div>
<p>Find the minimal path sum, in <a href='http://www.projecteuler.net/project/matrix.txt'>matrix.txt</a> (right click and &#8216;Save Link/Target As&#8230;&#8217;), a 31K text file containing a 80 by 80 matrix, from the left column to the right column.</p>
<h4><u>Analysis</u></h4>
<p>This is using the same technique that solved problem 18, 67, 81 &#038; 83.  We no longer have a fixed starting and ending point so we need to track the starting points to a vector.  From there we &#8220;bubble&#8221; the minimum sums by moving from left to right. </p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">@rows</span> <span style="color: #339933;">=</span> <span style="color: #000066;">map</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#91;</span><span style="color: #000066;">split</span> <span style="color: #339933;">/,/</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009999;">&lt;DATA&gt;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$nrows</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$#rows</span><span style="color: #339933;">;</span>
<span style="color: #000066;">push</span> <span style="color: #0000ff;">@cost</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>0<span style="color: #339933;">..</span><span style="color: #0000ff;">$nrows</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$x</span> <span style="color: #009900;">&#40;</span>1<span style="color: #339933;">..</span><span style="color: #0000ff;">$nrows</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$y</span> <span style="color: #009900;">&#40;</span>0<span style="color: #339933;">..</span><span style="color: #0000ff;">$nrows</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$cost</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$y</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> min_n<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$cost</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$y</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$cost</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$y</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$y</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$x</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$y</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">reverse</span> <span style="color: #009900;">&#40;</span>0<span style="color: #339933;">..</span><span style="color: #0000ff;">$nrows</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$cost</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$y</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> min_n<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$cost</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$y</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$cost</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$y</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$y</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$x</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066;">print</span> min_n<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@cost</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> min_n <span style="color: #009900;">&#123;</span> <span style="color: #000066;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">sort</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$a</span><span style="color: #339933;">&lt;=&gt;</span><span style="color: #0000ff;">$b</span><span style="color: #009900;">&#125;</span> <span style="color: #000066;">grep</span> <span style="color: #009900;">&#123;</span><span style="color: #000066;">defined</span><span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@_</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">__DATA__</span>
<span style="color: #cc66cc;">131</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">673</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">234</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">103</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">18</span> 
<span style="color: #cc66cc;">201</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">96</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">342</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">965</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">150</span> 
<span style="color: #cc66cc;">630</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">803</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">746</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">422</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">111</span> 
<span style="color: #cc66cc;">537</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">699</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">497</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">121</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">956</span> 
<span style="color: #cc66cc;">805</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">732</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">524</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">37</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">331</span></pre></div></div>

<h4><u>Comments</u></h4>
<ul>
<li>We’ve included the sample matrix as an example. </li>
<li>The first line of the program reads the data file, which is appended to the end of the program after the __DATA__ separator, into a two dimensional array named @rows. $nrows is the last index of the array so in this case 4.</li>
<li>Check <a href="http://blog.dreamshire.com/2009/03/26/94/#min_n">here</a> for more information on the <em>min_n</em> function.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/05/project-euler-problem-82-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 42 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/27/project-euler-problem-42-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/27/project-euler-problem-42-solution/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 18:17:52 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=987</guid>
		<description><![CDATA[How many triangle words does the list of common English words contain?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The <i>n</i><sup>th</sup> term of the sequence of triangle numbers is given by, <i>t<sub>n</sub></i> = &frac12;<i>n</i>(<i>n</i>+1); so the first ten triangle numbers are:</p>
<p style='text-align:center;'>1, 3, 6, 10, 15, 21, 28, 36, 45, 55, &#8230;</p>
<p>By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = <i>t</i><sub>10</sub>. If the word value is a triangle number then we shall call the word a triangle word.</p>
<p>Using <a href='http://projecteuler.net/project/words.txt'>words.txt</a> (right click and &#8216;Save Link/Target As&#8230;&#8217;), a 16K text file containing nearly two-thousand common English words, how many are triangle words?</p>
<h4><u>Analysis</u></h4>
<p>Remember <a href="http://blog.dreamshire.com/2009/04/17/project-euler-problem-44-solution/">Problems 44 &#038; 45</a>?  The same logic applies except for triangle numbers.  Whenever the equation ( &radic;(1+8n) &#8211; 1 ) / 2 evaluates to an integer the number, <em>n</em>, is a triangle number.</p>
<p>Remember <a href="http://blog.dreamshire.com/2009/04/09/project-euler-problem-22-solution/">Problem 22</a>?  Well, this is the same core solution. </p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">@h</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'A'</span> <span style="color: #339933;">..</span> <span style="color: #ff0000;">'Z'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">..</span> <span style="color: #cc66cc;">26</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">open</span> <span style="color: #009900;">&#40;</span>IN<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;&lt;words.txt&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Can't open file: $!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">@words</span> <span style="color: #339933;">=</span> <span style="color: #000066;">map</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/&quot;(w+)&quot;/</span>  <span style="color: #009900;">&#125;</span> <span style="color: #000066;">split</span> <span style="color: #009966; font-style: italic;">/,/</span><span style="color: #339933;">,</span><span style="color: #009999;">&lt;IN&gt;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000066;">grep</span> <span style="color: #009900;">&#123;</span> is_tri<span style="color: #009900;">&#40;</span>score<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@words</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE42 = $c&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> is_tri <span style="color: #009900;">&#123;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$n</span><span style="color: #339933;">=</span><span style="color: #000066;">shift</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$t</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">8</span><span style="color: #0000ff;">*$n</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000066;">return</span> <span style="color: #000066;">int</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$t</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">$t</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">sub</span> score <span style="color: #009900;">&#123;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$score</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$score</span><span style="color: #339933;">+=</span><span style="color: #0000ff;">$_</span> <span style="color: #b1b100;">for</span> <span style="color: #0000ff;">@h</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">map</span> <span style="color: #000066;">uc</span><span style="color: #339933;">,</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span><span style="color: #000066;">shift</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #000066;">return</span> <span style="color: #0000ff;">$score</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>Even though we used Perl, we still kept it readable, mostly.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/27/project-euler-problem-42-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 37 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/23/project-euler-problem-37-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/23/project-euler-problem-37-solution/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 09:03:44 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 30-39]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=886</guid>
		<description><![CDATA[Find the sum of all eleven primes that are both truncatable from left to right and right to left.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.</p>
<p>Find the sum of the only eleven primes that are both truncatable from left to right and right to left.</p>
<p><small>NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.</small></p>
<h4><u>Analysis</u></h4>
<p>The good news is the problem tells us there&#8217;s only 11.  We can just keep searching a bunch of primes until 11 meet the criteria.</p>
<p>With just some casual thought it occurred to us that since we are going to truncate in both directions only primes ending in 3 or 7 are valid.  Also any prime with at least one digit from the set [0, 2, 4, 5, 6, 8] would make the prime invalid.  Add to that pairs such as &#8217;11&#8242;, &#8217;33&#8242;, &#8217;77&#8242; or &#8217;99&#8242; and the possible prime numbers gets smaller.</p>
<p>Two problems:  The two digit primes 23 and 53 would violate our filter (we found these by looking at the table of prime numbers), so we will have to mark these as exceptions.   And just how big can these primes get?</p>
<p>One way to find out is just to get started and see what happens.  This code gave us our solution set and the answer.</p>
<p>After seeing the results, we realized we could build a tree to ferret out the set of primes.  Here&#8217;s a tree for 2, 5, and 7.  We&#8217;ll leave 3 as an exercise.</p>
<p>2 &#8212; 3</p>
<p>5 &#8212; 3</p>
<p>&nbsp;&nbsp;    3 &#8212; 9 &#8212; 3 &#8212; 9 &#8212; 7<br />
7  &#8212;  9 &#8212; 7</p>
<p>When a prime starts with 7 it can only be followed by 3 or 9.  For the 9 branch only a 7 can come next and no other number would follow.  A 3, 1, 7 or 9 would produce a composite number as 39, 9, 77 or 1.<br />
The same logic follows for the 3 side.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> integer<span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$s</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">23</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">53</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">#first two that are exceptions to our pattern</span>
<span style="color: #000066;">open</span> IN<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;&lt;primes2.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">@p</span> <span style="color: #339933;">=</span> <span style="color: #000066;">grep</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">!/</span><span style="color: #009900;">&#91;</span>024568<span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009966; font-style: italic;">/[37]$/</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009966; font-style: italic;">/^(3[17][^1]|7[39][^13])/</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!/</span><span style="color: #cc66cc;">99</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">77</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">33</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">11</span><span style="color: #339933;">/</span><span style="color: #009900;">&#125;</span> <span style="color: #009999;">&lt;IN&gt;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$c</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$q</span><span style="color: #339933;">=</span><span style="color: #000066;">shift</span> <span style="color: #0000ff;">@p</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>trunc<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">++;</span>
    <span style="color: #0000ff;">$s</span><span style="color: #339933;">+=</span><span style="color: #0000ff;">$q</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE37 = $s, $c primes found&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> trunc <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$c</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$d</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$d</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">return</span> <span style="color: #cc66cc;">0</span> <span style="color: #b1b100;">unless</span> is_prime<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$d</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> is_prime<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$c</span><span style="color: #339933;">%=</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">**</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$d</span><span style="color: #339933;">/=</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #000066;">return</span> <span style="color: #cc66cc;">1</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>Didn&#8217;t have fun with this one.  Here&#8217;s the set: [23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 739397]<br />
Here&#8217;s the start of our regex code to sift out the 11 primes from a file of primes.  It reduces the list of 78,499 primes from 2 &#8211; 1,000,000 to just 28.  Not up to finishing it.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">open</span> IN<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;&lt;primes2.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">@p</span> <span style="color: #339933;">=</span> <span style="color: #000066;">grep</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">!/</span><span style="color: #009900;">&#91;</span>024568<span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009966; font-style: italic;">/[37]$/</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009966; font-style: italic;">/^(3[17][^1]|7[39][^13])/</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!/</span><span style="color: #cc66cc;">99</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">77</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">33</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">11</span><span style="color: #339933;">/</span><span style="color: #009900;">&#125;</span> <span style="color: #009999;">&lt;IN&gt;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/23/project-euler-problem-37-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 203 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/20/project-euler-problem-204-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/20/project-euler-problem-204-solution/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 04:25:13 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Binomials]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=822</guid>
		<description><![CDATA[Squarefree Binomial Coefficients]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The binomial coefficients <img src="" style="display:none;" alt="^(" /><sup>n</sup>C<sub>k</sub> can be arranged in triangular form, Pascal&#8217;s triangle, like this:</p>
<div style="text-align:center;">
<table align="center">
<tr>
<td colspan="7"></td>
<td>1</td>
<td colspan="7"></td>
</tr>
<tr>
<td colspan="6"></td>
<td>1</td>
<td></td>
<td>1</td>
<td colspan="6"></td>
</tr>
<tr>
<td colspan="5"></td>
<td>1</td>
<td></td>
<td>2</td>
<td></td>
<td>1</td>
<td colspan="5"></td>
</tr>
<tr>
<td colspan="4"></td>
<td>1</td>
<td></td>
<td>3</td>
<td></td>
<td>3</td>
<td></td>
<td>1</td>
<td colspan="4"></td>
</tr>
<tr>
<td colspan="3"></td>
<td>1</td>
<td></td>
<td>4</td>
<td></td>
<td>6</td>
<td></td>
<td>4</td>
<td></td>
<td>1</td>
<td colspan="3"></td>
</tr>
<tr>
<td colspan="2"></td>
<td>1</td>
<td></td>
<td>5</td>
<td></td>
<td>10</td>
<td></td>
<td>10</td>
<td></td>
<td>5</td>
<td></td>
<td>1</td>
<td colspan="2"></td>
</tr>
<tr>
<td colspan="1"></td>
<td>1</td>
<td></td>
<td>6</td>
<td></td>
<td>15</td>
<td></td>
<td>20</td>
<td></td>
<td>15</td>
<td></td>
<td>6</td>
<td></td>
<td>1</td>
<td colspan="1"></td>
</tr>
<tr>
<td>1</td>
<td></td>
<td>7</td>
<td></td>
<td>21</td>
<td></td>
<td>35</td>
<td></td>
<td>35</td>
<td></td>
<td>21</td>
<td></td>
<td>7</td>
<td></td>
<td>1</td>
</tr>
</table>
</div>
<p>It can be seen that the first eight rows of Pascal&#8217;s triangle contain twelve distinct numbers: 1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6,&nbsp;7,&nbsp;10,&nbsp;15,&nbsp;20,&nbsp;21&nbsp;and&nbsp;35.</p>
<p>A positive integer <var>n</var> is called squarefree if no square of a prime divides <var>n</var>.<br />
Of the twelve distinct numbers in the first eight rows of Pascal&#8217;s triangle, all except 4 and 20 are squarefree.<br />
The sum of the distinct squarefree numbers in the first eight rows is 105.</p>
<p>Find the sum of the distinct squarefree numbers in the first 51 rows of Pascal&#8217;s triangle.</p>
<h4><u>Analysis</u></h4>
<p>Only a few primes are required.  For example,  51! is the biggest numerator (<sub>n</sub>C<sub>k</sub> = n! / k! (n-k)! ) and the largest prime factor required is &le; &radic;51 which is 7.  We added a few more to try bigger problems.</p>
<p>So we fill a hash&#8217;s keys with unique numbers from Pascal&#8217;s triangle and iterate through the 589 binomial coefficients and track which ones are squarefree, sum them up and print the results.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">@primes</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">13</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">17</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">19</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">51</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">#more rows means more primes</span>
<span style="color: #0000ff;">$s</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#use a hash, b, to select the distinct binomial coefficients from Pascal's triangle</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@_</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #000066;">map</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>1<span style="color: #339933;">..</span><span style="color: #0000ff;">@_</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;=</span><span style="color: #0000ff;">$rows</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066;">grep</span> <span style="color: #0000ff;">$b</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">++,</span> <span style="color: #0000ff;">@_</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$x</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">keys</span> <span style="color: #0000ff;">%b</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$s</span><span style="color: #339933;">+=</span><span style="color: #0000ff;">$x</span> <span style="color: #b1b100;">if</span> sqf<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE203 = $s&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> sqf <span style="color: #009900;">&#123;</span> <span style="color: #000066;">return</span> <span style="color: #339933;">!</span> <span style="color: #000066;">grep</span> <span style="color: #0000ff;">@_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">%</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">*</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@primes</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>Wanton use of grep and map.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/20/project-euler-problem-204-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
