<?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; Common Routines</title>
	<atom:link href="http://blog.dreamshire.com/tag/common-routines/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 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 26 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/30/project-euler-problem-26-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/30/project-euler-problem-26-solution/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 22:23:48 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 20-29]]></category>
		<category><![CDATA[Common Routines]]></category>
		<category><![CDATA[Large Integers]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1041</guid>
		<description><![CDATA[Find the value of d < 1000 for which 1/d contains the longest recurring cycle.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:</p>
<blockquote>
<table>
<tr>
<td><sup>1</sup>/<sub>2</sub></td>
<td>=&nbsp;</td>
<td>0.5</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>3</sub></td>
<td>=&nbsp;</td>
<td>0.(3)</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>4</sub></td>
<td>=&nbsp;</td>
<td>0.25</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>5</sub></td>
<td>=&nbsp;</td>
<td>0.2</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>6</sub></td>
<td>=&nbsp;</td>
<td>0.1(6)</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>7</sub></td>
<td>=&nbsp;</td>
<td>0.(142857)</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>8</sub></td>
<td>=&nbsp;</td>
<td>0.125</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>9</sub></td>
<td>=&nbsp;</td>
<td>0.(1)</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>10</sub></td>
<td>=&nbsp;</td>
<td>0.1</td>
</tr>
</table>
</blockquote>
<p>Where 0.1(6) means 0.166666&#8230;, and has a 1-digit recurring cycle. It can be seen that <sup>1</sup>/<sub>7</sub> has a 6-digit recurring cycle.</p>
<p>Find the value of <i>d</i> &lt; 1000 for which <sup>1</sup>/<sub><i>d</i></sub> contains the longest recurring cycle in its decimal fraction part.</p>
<h4><u>Analysis</u></h4>
<p>This is a useful application of <a href="http://en.wikipedia.org/wiki/Fermat's_little_theorem">Fermat&#8217;s little theorem</a> that says: 1/<em>d</em> has a cycle of n digits if 10<sup>n</sup>&minus;1 mod <em>d</em> = 0 for prime <em>d</em>.  It also follows that a prime number in the denominator, <em>d</em>,  can yield up to <em>d</em>-1 repeating decimal digits.</p>
<p>Since the problem wants the longest string of repeating digits from 1 &lt; <i>d</i> &lt; 1000 we started at the top and worked down calculating the length of the chain until we found one with d &minus; 1 digits.</p>
<p>Calculating a cycle length:<br />
d=7 ; c=1<br />
9 % 7 = 2 ; c=2<br />
99 % 7 = 1; c=3<br />
999 % 7 = 5; c=4<br />
9999 % 7 = 3; c=5<br />
99999 % 7 = 4; c=6<br />
999999 % 7 = 0; break</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> Euler <span style="color: #ff7700;font-weight:bold;">import</span> is_prime
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> d <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">999</span>, <span style="color: #ff4500;">1</span>, -<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> is_prime<span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">continue</span>
  c = <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span><span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, c<span style="color: black;">&#41;</span> - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> d <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">0</span>:
    c += <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>d-c<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span>: <span style="color: #ff7700;font-weight:bold;">break</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE26 = &quot;</span>,d</pre></div></div>

<h4><u>Comments</u></h4>
<p>More information on the <em>Euler</em> module can be found on the <a href="http://blog.dreamshire.com/2009/03/26/94/">tools page</a>.<br />
for d < 2000: 1997<br />
for d < 2500: 2473<br />
for d < 10,000: 9967<br />
for d < 20,000: 19993<br />
for d < 25,000: 24989</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/30/project-euler-problem-26-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 58 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/11/project-euler-problem-58-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/11/project-euler-problem-58-solution/#comments</comments>
		<pubDate>Sun, 12 Apr 2009 00:15:21 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 50-59]]></category>
		<category><![CDATA[Common Routines]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=607</guid>
		<description><![CDATA[Investigate the number of primes that lie on the diagonals of the spiral grid.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Starting with 1 and spiralling counterclockwise in the following way, a square spiral with side length 7 is formed.</p>
<p style='text-align:center;font-family:courier new;'><span style='color:#ff0000;font-family:courier new;'>37</span> 36 35 34 33 32 <span style='color:#ff0000;font-family:courier new;'>31</span><br />
38 <span style='color:#ff0000;font-family:courier new;'>17</span> 16 15 14 <span style='color:#ff0000;font-family:courier new;'>13</span> 30<br />
39 18 <span style='color:#ff0000;font-family:courier new;'>&nbsp;5</span> &nbsp;4 <span style='color:#ff0000;font-family:courier new;'>&nbsp;3</span> 12 29<br />
40 19 &nbsp;6 &nbsp;1 &nbsp;2 11 28<br />
41 20 <span style='color:#ff0000;font-family:courier new;'>&nbsp;7</span> &nbsp;8 &nbsp;9 10 27<br />
42 21 22 23 24 25 26<br />
<span style='color:#ff0000;font-family:courier new;'>43</span> 44 45 46 47 48 49</p>
<p>It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13 &asymp; 62%.</p>
<p>If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?</p>
<h4><u>Analysis</u></h4>
<p>Forget about the geometry and determine the series: (3, 5, 7, 9), (13, 17, 21, 25), (31, 37, 43, 49), &#8230; This represents the &#8220;corners&#8221; for every square layer.<br />
Determine and count the primes, <em>p<sub>n</sub></em>,  in the series ignoring every 4th one since it will always be a square and therefore composite.  As soon as we reach a ratio of primes to series length (<em>n</em>),  <em>p<sub>n</sub></em>/<em>n</em> < 10% we can calculate a side length as <em>n</em>/2.</p>
<p> It&#8217;s important to have a decent <a href="http://blog.dreamshire.com/2009/03/26/94/#is_prime"><em>is_prime()</em></a> function to achieve a better run time.  </p>
<h4><u>Solution</u></h4>
<p>Runs < 8 seconds in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> Euler <span style="color: #ff7700;font-weight:bold;">import</span> is_prime
n_prime, d, avg, n = <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">while</span> avg <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">0.10</span>:
  n_prime += is_prime<span style="color: black;">&#40;</span>d + n<span style="color: black;">&#41;</span> + is_prime<span style="color: black;">&#40;</span>d + n<span style="color: #66cc66;">*</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> + is_prime<span style="color: black;">&#40;</span>d + n<span style="color: #66cc66;">*</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
  d += n<span style="color: #66cc66;">*</span><span style="color: #ff4500;">4</span>
  n += <span style="color: #ff4500;">2</span>
  avg = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>n_prime<span style="color: black;">&#41;</span> / <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span> <span style="color: #66cc66;">*</span> n<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE58 = &quot;</span>,n-<span style="color: #ff4500;">1</span></pre></div></div>

<h4><u>Comments</u></h4>
<ul>
<li>More information on the <em>Euler</em> module can be found on the <a href="http://blog.dreamshire.com/2009/03/26/94/">tools page</a>.</li>
<li>Note how we calculate the answer as n-1.  This is because we count only even n and the sides of our layer has to be odd.</li>
<li>See <a href="http://blog.dreamshire.com/2009/05/02/project-euler-problem-28-solution/">problem 28</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/11/project-euler-problem-58-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 36 Solution</title>
		<link>http://blog.dreamshire.com/2009/03/29/project-euler-problem-36-solution/</link>
		<comments>http://blog.dreamshire.com/2009/03/29/project-euler-problem-36-solution/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 06:01:03 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 30-39]]></category>
		<category><![CDATA[Common Routines]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=172</guid>
		<description><![CDATA[Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The decimal number, 585 = 1001001001<sub>2</sub> (binary), is palindromic in both bases.</p>
<p>Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.</p>
<p><small>(Please note that the palindromic number, in either base, may not include leading zeros.)</small></p>
<h4><u>Analysis</u></h4>
<p>Since leading zeros are not allowed, the top and bottom bits for binary numbers have to be one in order to be palindromic.  This eliminates even numbers from consideration.</p>
<h4><u>Solution</u></h4>
<p>Runs < 2 second in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">$s</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> 
<span style="color: #0000ff;">$limit</span> <span style="color: #339933;">=</span> 1e6<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</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> <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: #cc66cc;">2</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;">$n</span> <span style="color: #b1b100;">if</span> palindromic<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> palindromic<span style="color: #009900;">&#40;</span>dec2bin<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#41;</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 PE36 = $s&quot;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> dec2bin <span style="color: #009900;">&#123;</span> <span style="color: #000066;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%b&quot;</span><span style="color: #339933;">,</span><span style="color: #000066;">shift</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">sub</span> palindromic <span style="color: #009900;">&#123;</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;">reverse</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: #009900;">&#125;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>Results < one trillion:<br />
{ 1, 3, 5, 7, 9, 33, 99, 313, 585, 717, 7447, 9009, 15351, 32223, 39993, 53235, 53835,<br />
73737, 585585, 1758571, 1934391, 1979791, 3129213, 5071705, 5259525,<br />
5841485, 13500531, 719848917, 910373019, 939474939, 1290880921, 7451111547,<br />
10050905001, 18462126481, 32479297423, 75015151057, 110948849011, 136525525631 }</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/03/29/project-euler-problem-36-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Common Functions and Routines for Project Euler</title>
		<link>http://blog.dreamshire.com/2009/03/26/94/</link>
		<comments>http://blog.dreamshire.com/2009/03/26/94/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 03:02:47 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Tools]]></category>
		<category><![CDATA[Common Routines]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=94</guid>
		<description><![CDATA[Useful tools, files and routines to help solve problems on Project Euler]]></description>
			<content:encoded><![CDATA[<p>Helpful tools used to solve problems in Project Euler:</p>
<p>A list of prime numbers from 2 to a billion.<br />
Anytime we deal with problems involving prime numbers we use our file of primes instead of generating them each time they’re called for. This is a reasonable practice since prime generation has been around for centuries. </p>
<p>We also use a module of routines that are used often in solving problems.  <strong><em>Euler.py </em></strong>is included as needed.  The example below shows typical Python usage:</p>
<pre>from Euler import is_prime, is_perm</pre>
<p>Here is the contents of Euler.py</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> fact<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x,y:x<span style="color: #66cc66;">*</span>y,<span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,n+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> is_perm<span style="color: black;">&#40;</span>a,b<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">sorted</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> == <span style="color: #008000;">sorted</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>b<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> is_palindromic<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>==<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> is_pandigital<span style="color: black;">&#40;</span>n, s=<span style="color: #ff4500;">9</span><span style="color: black;">&#41;</span>: n=<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>return <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>==s <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #483d8b;">'1234567890'</span><span style="color: black;">&#91;</span>:s<span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> is_prime<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> n == <span style="color: #ff4500;">2</span> <span style="color: #ff7700;font-weight:bold;">or</span> n == <span style="color: #ff4500;">3</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> n <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">2</span> <span style="color: #ff7700;font-weight:bold;">or</span> n<span style="color: #66cc66;">%</span>2 == <span style="color: #ff4500;">0</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> n <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">9</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> n<span style="color: #66cc66;">%</span>3 == <span style="color: #ff4500;">0</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
  r = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>n<span style="color: #66cc66;">**</span>.5<span style="color: black;">&#41;</span>
  f = <span style="color: #ff4500;">5</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> f <span style="color: #66cc66;">&lt;</span>= r:
    <span style="color: #ff7700;font-weight:bold;">if</span> n<span style="color: #66cc66;">%</span>f == <span style="color: #ff4500;">0</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> n<span style="color: #66cc66;">%</span><span style="color: black;">&#40;</span>f+<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
    f +=<span style="color: #ff4500;">6</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> trial_division<span style="color: black;">&#40;</span>n, bound=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> n == <span style="color: #ff4500;">1</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">3</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> n<span style="color: #66cc66;">%</span>p == <span style="color: #ff4500;">0</span>: <span style="color: #ff7700;font-weight:bold;">return</span> p
    <span style="color: #ff7700;font-weight:bold;">if</span> bound == <span style="color: #008000;">None</span>: bound = n
    dif = <span style="color: black;">&#91;</span><span style="color: #ff4500;">6</span>, <span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">6</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
    m = <span style="color: #ff4500;">7</span><span style="color: #66cc66;">;</span> i = <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> m <span style="color: #66cc66;">&lt;</span>= bound <span style="color: #ff7700;font-weight:bold;">and</span> m<span style="color: #66cc66;">*</span>m <span style="color: #66cc66;">&lt;</span>= n:
        <span style="color: #ff7700;font-weight:bold;">if</span> n<span style="color: #66cc66;">%</span>m == <span style="color: #ff4500;">0</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> m
        m += dif<span style="color: black;">&#91;</span>i<span style="color: #66cc66;">%</span>8<span style="color: black;">&#93;</span>
        i += <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> n
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> factor<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span>-<span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> n <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">0</span>: n = -n
    F = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> n <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">1</span>:
        p = trial_division<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>
        e = <span style="color: #ff4500;">1</span>
        n /= p
        <span style="color: #ff7700;font-weight:bold;">while</span> n<span style="color: #66cc66;">%</span>p == <span style="color: #ff4500;">0</span>:
            e += <span style="color: #ff4500;">1</span><span style="color: #66cc66;">;</span> n /= p
        F.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>p,e<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    F.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> F
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> gcd<span style="color: black;">&#40;</span>a, b<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> a <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">0</span>:  a = -a
  <span style="color: #ff7700;font-weight:bold;">if</span> b <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">0</span>:  b = -b
  <span style="color: #ff7700;font-weight:bold;">if</span> a == <span style="color: #ff4500;">0</span>: <span style="color: #ff7700;font-weight:bold;">return</span> b
  <span style="color: #ff7700;font-weight:bold;">if</span> b == <span style="color: #ff4500;">0</span>: <span style="color: #ff7700;font-weight:bold;">return</span> a
  <span style="color: #ff7700;font-weight:bold;">while</span> b <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">0</span>: 
    <span style="color: black;">&#40;</span>a, b<span style="color: black;">&#41;</span> = <span style="color: black;">&#40;</span>b, a<span style="color: #66cc66;">%</span>b<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> a
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> binomial<span style="color: black;">&#40;</span>n, k<span style="color: black;">&#41;</span>:
  nt = <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> t <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">min</span><span style="color: black;">&#40;</span>k, n-k<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
    nt = nt<span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>n-t<span style="color: black;">&#41;</span>//<span style="color: black;">&#40;</span>t+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> nt</pre></div></div>

<hr id="sum" />
<h3><em>sum: Sum an Array</em></h3>
<p>Add the elements of an array and return the sum.</p>
<pre>sub sum { my $s=0; $s+=$_ for @_; return $s }</pre>
<p><b>Explanation:</b> Add the elements of an array.  Used mostly to make a program more readable.</p>
<hr id="#dec2bin" />
<h3><em>dec2bin: Convert a Decimal Number to Binary</em></h3>
<p>Convert decimal (base 10) numbers to binary (base 2)</p>
<pre>sub dec2bin { sprintf "%b", shift) }</pre>
<p><b>Explanation:</b> Converts a decimal number < 10<sup>15</sup> to a binary string.</p>
<hr / id="min_n">
<h3><em>min_n &#038; max_n: Minimum &#038; Maximum of a Numerical List</em></h3>
<pre>sub max_n { return (sort {$a<=>$b} grep {defined} @_)[-1] }</pre>
<pre>sub min_n { return (sort {$a<=>$b} grep {defined} @_)[0] }</pre>
<p><b>Explanation:</b> Simple routine to accept a list of numerical values and return the minimum/maximum.  The technique is fast and simple; sort the list ascending numerically and return the last element [-1] for the maximum or the first element [0] for the minimum.  Undefined values are not considered.<br />
For example: print min_n(5,7,0,-8,100) would print <em>-8</em>.</p>
<hr id="rotate" />
<h3><em>rotate: Rotate a String Left One Character</em></h3>
<pre>sub rotate { @_[0]=~s/(\d)(\d+)/\2\1/ }</pre>
<p><b>Explanation:</b> This function accepts a string as its single argument and puts the first character and remaining characters into \1 and \2 as a regular expression.  They are then reassembled in reverse order effecting a left-wise rotation.<br />
<strong>NOTE:</strong> This function DOES change the argument.<br />
For example: print rotate(&#8216;abcdef&#8217;) would print <em>bcdefa</em>.</p>
<hr id="fact" />
<h3><em>fact: Calculate Factorial</em></h3>
<p>This hash must first be defined:</p>
<pre>my %fact = (0,1,1,1);</pre>
<pre>
sub fact {
  my $n = shift;
  return $fact{$n} if exists $fact{$n};
  return ($fact{$n} = fact($n-1) * $n)
}
</pre>
<p><b>Explanation:</b> Memoization helps this recursive function calculate factorials especially for frequent calls in the same program.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/03/26/94/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
	</channel>
</rss>
