<?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; Solutions 30-39</title>
	<atom:link href="http://blog.dreamshire.com/category/project-euler-solutions/project-euler-solutions-30-39/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 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 33 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/22/project-euler-problem-33-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/22/project-euler-problem-33-solution/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 06:31:52 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 30-39]]></category>
		<category><![CDATA[fractions]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=884</guid>
		<description><![CDATA[Discover all the fractions with an unorthodox cancelling method.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The fraction <sup>49</sup>/<sub>98</sub> is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that <sup>49</sup>/<sub>98</sub> = <sup>4</sup>/<sub>8</sub>, which is correct, is obtained by cancelling the 9s.</p>
<p>We shall consider fractions like, <sup>30</sup>/<sub>50</sub> = <sup>3</sup>/<sub>5</sub>, to be trivial examples.</p>
<p>There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.</p>
<p>If the product of these four fractions is given in its lowest common terms, find the value of the denominator.</p>
<h4><u>Analysis</u></h4>
<p>This is curious problem and was able to solve it in just a few lines and 84 iterations.  Just generated all fractions with 2 digit numerators and denominators and ignored &#8216;trivial&#8217; examples.  Used symmetry to consider distinct fractions &#8211; which was really not necessary, but&#8230;why not.</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;">d = <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, i<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, j<span style="color: black;">&#41;</span>:
      ki = k<span style="color: #66cc66;">*</span><span style="color: #ff4500;">10</span> + i
      ij = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">10</span> + j
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>ij/ki == <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>j<span style="color: black;">&#41;</span>/k<span style="color: black;">&#41;</span>:
        d <span style="color: #66cc66;">*</span>= ij/ki
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE33 = &quot;</span>,d</pre></div></div>

<h4><u>Comments</u></h4>
<p>Added the float function to keep calculations from converting to integers.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/22/project-euler-problem-33-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 32 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/22/project-euler-problem-32-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/22/project-euler-problem-32-solution/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 05:05:27 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 30-39]]></category>
		<category><![CDATA[Pandigital]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=882</guid>
		<description><![CDATA[Find the sum of all numbers that can be written as pandigital products.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>We shall say that an <var>n</var>-digit number is pandigital if it makes use of all the digits 1 to <var>n</var> exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.</p>
<p>The product 7254 is unusual, as the identity, 39 &times; 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.</p>
<p>Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.</p>
<p><small>HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.</small></p>
<h4><u>Analysis</u></h4>
<p>This is a perfect application for a simple brute force program that iterates through the possibilities and produces a solution.  The only thought of optimization was to only generate as many 4 digit products as necessary in order to keep the concatenation of i, j and p at the required 9 digits.  The only combinations are a 1 digit number times a 4 digit number (lowest valid = 1234) or a 2 digit number times a 3 digit number (lowest valid = 123).  </p>
<p>A set is used to ignore duplicate products.</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_pandigital
&nbsp;
p = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <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;">2</span>,<span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>:
  start = <span style="color: #ff4500;">1234</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> i<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">9</span>: start = <span style="color: #ff4500;">123</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>start, <span style="color: #ff4500;">10000</span>/i+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> is_pandigital<span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>j<span style="color: black;">&#41;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>i<span style="color: #66cc66;">*</span>j<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>: p.<span style="color: black;">add</span><span style="color: black;">&#40;</span>i<span style="color: #66cc66;">*</span>j<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE32 = &quot;</span>,<span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</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>This solution took 21,220 iterations.</li>
<li>Of the 9 sets found, the last one was 48 &times; 159 = 7632</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/22/project-euler-problem-32-solution/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 39 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/22/project-euler-problem-39-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/22/project-euler-problem-39-solution/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 19:25:49 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 30-39]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Triangles]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=860</guid>
		<description><![CDATA[If p is the perimeter of a right angle triangle, {a, b, c}, which value, for p ≤ 1000, has the most solutions?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>If <i>p</i> is the perimeter of a right angle triangle with integral length sides, {<i>a</i>,<i>b</i>,<i>c</i>}, there are exactly three solutions for <i>p</i> = 120.</p>
<p style='text-align:center;'>{20,48,52}, {24,45,51}, {30,40,50}</p>
<p>For which value of <i>p</i> &le; 1000, is the number of solutions maximised?</p>
<h4><u>Analysis</u></h4>
<p>The Pythagorean Theorem gives us: a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup> and by intuition a + b + c = <em>p</em> (perimeter).<br />
The decision to iterate perimeters has many advantages over generating right triangles.  Only even numbered perimeters need to be checked; below is our analysis:</p>
<table>
<tr>
<th colspan="2">Independent</th>
<td width=5> </td>
<th colspan="2">Dependent</th>
</tr>
<tr>
<th>a</th>
<th>b</th>
<td width=5> </td>
<th>c</th>
<th><em>p</em></th>
</tr>
<tr>
<td>E</td>
<td>E</td>
<td width=5> </td>
<td>E</td>
<td>E</td>
</tr>
<tr>
<td>O</td>
<td>O</td>
<td width=5> </td>
<td>E</td>
<td>E</td>
</tr>
<tr>
<td>E</td>
<td>O</td>
<td width=5> </td>
<td>O</td>
<td>E</td>
</tr>
<tr>
<td>O</td>
<td>E</td>
<td width=5> </td>
<td>E</td>
<td>E</td>
</tr>
</table>
<p>No matter which integral values we choose for a, b and c such that a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup> the perimeter will be even.</p>
<p>By iterating perimeters we need a method to check for integral (integer) right triangles.<br />
We can interpret the Pythagorean theorem as a<sup>2</sup> + b<sup>2</sup> = (<em>p</em>-a-b)<sup>2</sup> since <em>c</em> = <em>p</em>-<em>a</em>-<em>b</em>.  After expanding and simplifying we have:<br />
<em>b</em> = <em>p</em>(<em>p</em> &#8211; 2a) / 2(<em>p</em>-a) and when this equation evaluates to an integer (MOD(<em>p</em>(<em>p</em> &#8211; 2a), 2(<em>p</em>-a)) = 0) we have our triangle.</p>
<p>One last optimization.  By using a+b>c and symmetry we only need to investigate values of <em>a</em> to <em>p</em>/4.</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;">t_max = <span style="color: #ff4500;">0</span><span style="color: #66cc66;">;</span>
p_limit = <span style="color: #ff4500;">1000</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, p_limit+<span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
  t = <span style="color: #ff4500;">0</span><span style="color: #66cc66;">;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> a <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, p/<span style="color: #ff4500;">4</span>+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span>  p<span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>p - <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>a<span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>p-a<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>: t += <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> t <span style="color: #66cc66;">&gt;</span> t_max: <span style="color: black;">&#40;</span>t_max, p_max<span style="color: black;">&#41;</span> = <span style="color: black;">&#40;</span>t, p<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE30 = &quot;</span>, p_max</pre></div></div>

<h4><u>Comments</u></h4>
<p>Some other results:<br />
10,000 = 9,240<br />
100,000 = 55,440<br />
1,000,000 = 720,720<br />
10,000,000 = 6,126,120</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/22/project-euler-problem-39-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 35 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/08/project-euler-problem-35-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/08/project-euler-problem-35-solution/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 03:33:11 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 30-39]]></category>
		<category><![CDATA[Circular Primes]]></category>
		<category><![CDATA[Complete Problem]]></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=456</guid>
		<description><![CDATA[How many circular primes are there below one million?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.</p>
<p>There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.</p>
<p>How many circular primes are there below one million?</p>
<h4><u>Analysis</u></h4>
<p>We used our file of prime numbers and included those below 1 million and excluded those greater than 10 that contained the digits {0, 2, 4, 5, 6 or 8} because as a prime is rotated it cannot end with any of these digits and remain prime. </p>
<p>We read these numbers into a hash (associative array) using the prime numbers as keys.  The hash also serves as an &#8216;is prime?&#8217; function.  If a particular key exists then the number is prime and if it does not, then it&#8217;s composite.  The program loops through the hash&#8217;s keys and checks each rotation as a prime number.  When every rotation is prime it&#8217;s counted as a circular prime.</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> <span style="color: #009900;">&#40;</span>IN<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;&lt;primes35.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;Error opening file: $!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%is_prime</span> <span style="color: #339933;">=</span> <span style="color: #000066;">map</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span> <span style="color: #000066;">grep</span> <span style="color: #000066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</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: #339933;">!/</span><span style="color: #009900;">&#91;</span>024568<span style="color: #009900;">&#93;</span><span style="color: #339933;">/,</span> <span style="color: #009999;">&lt;IN&gt;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cp</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
P<span style="color: #339933;">:</span> <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$prime</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">keys</span> <span style="color: #0000ff;">%is_prime</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> 2<span style="color: #339933;">..</span><span style="color: #000066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$prime</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span> 
        rotate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$prime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #b1b100;">next</span> P <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$is_prime</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$prime</span><span style="color: #009900;">&#125;</span> 
     <span style="color: #009900;">&#125;</span>
     <span style="color: #0000ff;">$cp</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 PE35 = $cp&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<ul>
<li>Check out the tools post for an explanation of the <a href="http://blog.dreamshire.com/2009/03/26/94/#rotate"><em>rotate()</em></a> function</li>
<li>The next circular prime is a rep unit 19 digits long (R<sub>19</sub> = 1111111111111111111), so using this method for finding circular primes > 1,000,000 is impracticable</li>
<li>OEIS <a href="http://www.research.att.com/~njas/sequences/A016114">A016114</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/08/project-euler-problem-35-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 34 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/08/project-euler-problem-34-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/08/project-euler-problem-34-solution/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 07:24:39 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 30-39]]></category>
		<category><![CDATA[factorials]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=436</guid>
		<description><![CDATA[Find the sum of all numbers which are equal to the sum of the factorial of their digits.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.</p>
<p>Find the sum of all numbers which are equal to the sum of the factorial of their digits.</p>
<p><small>Note: as 1! = 1 and 2! = 2 are not sums they are not included.</small></p>
<h4><u>Analysis</u></h4>
<p>These type of numbers are referred to as factorions and it&#8217;s easy to learn that only 4 exist: 1, 2, 145 &#038; 40585.  The problem description wants us to ignore 1 &#038; 2 so the answer to this problem should become obvious.</p>
<p>But, let&#8217;s write a program anyway to search for factorions.  As usual, when writing a brute force search algorithm, we must first determine our bounds.  The lower bound is 10 because single digit candidates are to be ignored.  The upper bound we discover as follows (from Wikipedia):</p>
<p style="margin: 30px 30px">If n is a natural number of d digits that is a factorion, then 10<sup>d − 1</sup> ≤ n ≤ 9!d and this fails to hold for d ≥ 8. Thus n has 7 digits and the maximum sum of factorials of digits for a 7 digit number is 9!7 = 2,540,160, which is the upper bound.</p>
<p>Afterwards, we learned 50,000 worked just fine.</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;">@fact</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;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">24</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">120</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">720</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span>_040<span style="color: #339933;">,</span> <span style="color: #cc66cc;">40</span>_320<span style="color: #339933;">,</span> <span style="color: #cc66cc;">362</span>_880<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># 0 - 9 factorials </span>
<span style="color: #b1b100;">my</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: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$n</span> <span style="color: #009900;">&#40;</span>10<span style="color: #339933;">..</span>50_000<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$x</span><span style="color: #339933;">+=</span><span style="color: #0000ff;">$fact</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">for</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span><span style="color: #0000ff;">$n</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$s</span><span style="color: #339933;">+=</span><span style="color: #0000ff;">$n</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$n</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">$x</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 PE34 = $s&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">fact = <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">6</span>, <span style="color: #ff4500;">24</span>, <span style="color: #ff4500;">120</span>, <span style="color: #ff4500;">720</span>, <span style="color: #ff4500;">5040</span>, <span style="color: #ff4500;">40320</span>, <span style="color: #ff4500;">362880</span><span style="color: black;">&#41;</span>
s = <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> n <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;">10</span>, <span style="color: #ff4500;">50000</span><span style="color: black;">&#41;</span>:
  x = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span> fact<span style="color: black;">&#91;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> d <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> n == x: s+=n
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE34 = &quot;</span>,s</pre></div></div>

<h4><u>Comment</u></h4>
<p>This is a finite set of numbers and therefore a brute force solution is completely acceptable.  As long as it runs under a minute no further optimization is necessary.</p>
<ul>
<li>We use underscores in numeric literals for readability</li>
<li>See <a href="http://blog.dreamshire.com/2009/04/08/project-euler-problem-74-solution/">Problem 74</a></li>
<li><em>Mathematica:</em> <tt>Sum[Boole[n == Tr[IntegerDigits[n]!]] n, {n, 3, 1*^5}]</tt></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/08/project-euler-problem-34-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 31 Solution</title>
		<link>http://blog.dreamshire.com/2009/03/31/project-euler-problem-31-solution/</link>
		<comments>http://blog.dreamshire.com/2009/03/31/project-euler-problem-31-solution/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 18:16:15 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 30-39]]></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=152</guid>
		<description><![CDATA[Investigating combinations of English currency denominations.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:</p>
<p>1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).<br />
It is possible to make £2 in the following way:</p>
<p>1&times;£1 + 1&times;50p + 2&times;20p + 1&times;5p + 1&times;2p + 3&times;1p<br />
How many different ways can £2 be made using any number of coins?</p>
<h4><u>Analysis</u></h4>
<p>This is a typical problem that demonstrates the use of partitions which can be solved by dynamic programming.  To keep this problem simple: order does not matter, there are always enough coins to make a combination and were not looking for the optimal way to make change.  This a perfect application for tested algorithms where this problem has been used as an example.  As the details and nuances of dynamic programming are somewhat involved; we have provided some links below for a better explanation:<br />
<a href="http://www.algorithmist.com/index.php/Coin_Change">Coin Change &#8211; Algorithmist</a><br />
<a href="http://blog.dreamshire.com/docs/dyn_prog.pdf">Dynamic Programming Solution to the Coin Changing Problem</a></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;">$target</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@coins</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;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@ways</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$coin</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@coins</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$ways</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: #0000ff;">$ways</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$coin</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$coin</span><span style="color: #339933;">..</span><span style="color: #0000ff;">$target</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 PE31 = $ways[$target]&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<ul>
<li>See <a href="http://blog.dreamshire.com/2009/03/31/project-euler-problem-76-solution/">Problem 76</a>.  The advantage this code has is the problem only wants the number of combinations.  It would be a different approach if they wanted a set of combinations.</li>
<li>In Perl, the default variable, $_, takes the index value of the loop.  In this case, values from $coin to $target.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/03/31/project-euler-problem-31-solution/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 30 Solution</title>
		<link>http://blog.dreamshire.com/2009/03/30/project-euler-problem-30-solution/</link>
		<comments>http://blog.dreamshire.com/2009/03/30/project-euler-problem-30-solution/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 02:40:06 +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[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=168</guid>
		<description><![CDATA[Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:<br />
1634 = 1<sup>4</sup> + 6<sup>4</sup> + 3<sup>4</sup> + 4<sup>4</sup><br />
8208 = 8<sup>4</sup> + 2<sup>4</sup> + 0<sup>4</sup> + 8<sup>4</sup><br />
9474 = 9<sup>4</sup> + 4<sup>4</sup> + 7<sup>4</sup> + 4<sup>4</sup></p>
<p><small>As 1 = 1<sup>4</sup> is not a sum it is not included.</small></p>
<p>The sum of these numbers is 1634 + 8208 + 9474 = 19316.</p>
<p>Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.</p>
<h4><u>Analysis</u></h4>
<p>Firstly, “Find the sum” means we will need to build a solution set of valid elements that meet the problem’s criteria, and then add them together to verify we have calculated the correct and complete set.</p>
<p>Secondly, we make a list of constraints which place limits or restriction on the solution set.  This problem excludes the candidate 1 from consideration.  In fact, by intuitive extension, it is excluding all single digit candidates (numbers < 10) as they cannot form a sum of digits.  This is a clue that our search range starts from at least 10.  The truly ambitious may discover a method to calculate the optimal range minimum.</p>
<p>Finally, we write a brute-force program to see if it can derive the correct answer within the one minute time limit.  The only concern is how much search range to consider.  We have already determined the range minimum to be 10 so it’s finding the range maximum that remains a challenge.  After just a moment of thought it becomes clear that the number of digits for the sum must have the same number of digits as a candidate, so we construct the table below:</p>
<table>
<caption>Range Maximum Analysis for exp = 5</caption>
<tr>
<th>Digits (n)</th>
<th width="110" style="text-align:center">Maximum n</th>
<th width="135" style="text-align:center">Limit = 9<sup>5</sup> x n</th>
<th>Comment</th>
</tr>
<tr>
<td style="text-align: center" >9</td>
<td style="text-align: right">999,999,999</td>
<td style="text-align: right">531,441</td>
<td>they need to have the same # of digits</td>
</tr>
<tr>
<td style="text-align: center" >8</td>
<td style="text-align: right">99,999,999</td>
<td style="text-align: right">472,392</td>
<td>nope, still too many</td>
</tr>
<tr>
<td style="text-align: center"  >7</td>
<td style="text-align: right">9,999,999</td>
<td style="text-align: right">413,343</td>
<td>	not quite there</td>
</tr>
<tr>
<td style="text-align: center" >6</td>
<td style="text-align: right">999,999</td>
<td style="text-align: right">354,294</td>
<td>	a valid search limit</td>
</tr>
</table>
<p>In fact, it turns out that 9<sup>5</sup> x (n-1) is actually more than adequate, which reduces the search range and improves the performance of the program.  This was proved empirically.</p>
<table>
<caption>Search Maximum</caption>
<tr>
<th>Exponent (exp)</th>
<th width="140" style="text-align:center">9<sup>exp</sup> x (exp-1)</th>
</tr>
<tr>
<td style="text-align: center" >3</td>
<td style="text-align: right">1,458</td>
</tr>
<tr>
<td style="text-align: center" >4</td>
<td style="text-align: right">19,683</td>
</tr>
<tr>
<td style="text-align: center" >5</td>
<td style="text-align: right">236,196</td>
</tr>
<tr>
<td style="text-align: center" >6</td>
<td style="text-align: right">2,657,205</td>
</tr>
<tr>
<td style="text-align: center" >7</td>
<td style="text-align: right">28,697,814</td>
</tr>
</table>
<h4><u>Solution</u></h4>
<p>We try our solution not only for the target exponent of 5 (about 3 seconds) but also for 3, 4, 6, and 7.  This confirms that the program also works for cases of other powers and is keeping with our aim of extensibility.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">$exp</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$t</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$n</span> <span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">10</span> <span style="color: #339933;">..</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">*</span><span style="color: #0000ff;">*$exp</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$exp</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</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: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$s</span><span style="color: #339933;">+=</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">*</span><span style="color: #0000ff;">*$exp</span> <span style="color: #b1b100;">for</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span><span style="color: #0000ff;">$n</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">#split the digits in $n and assign each to $_.</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$s</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;">$t</span><span style="color: #339933;">+=</span><span style="color: #0000ff;">$s</span><span style="color: #339933;">;</span> <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;$n  &quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;n total: $t for exp = $expn&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>A Google search of the given series in the problem description found:<br />
&#8220;Fixed points for operation of repeatedly replacing a number by the sum of the fourth power of its digits.” (series <a href="http://www.research.att.com/~njas/sequences/A052455">A052455</a>)<br />
With a little further searching, we found:<br />
&#8220;Fixed points for operation of repeatedly replacing a number by the sum of the fifth power of its digits.” (series <a href="http://www.research.att.com/~njas/sequences/A052464">A052464</a>)<br />
We also discovered a series of numbers that are related to this set called narcissistic numbers (or Armstrong numbers) which differ from our target solution set by restricting the number of digits to be equal to the exponent (series <a href="http://www.research.att.com/~njas/sequences/A005188">A005188</a>).</p>
<p>Runs with other exponents<br />
153  370  371  407<br />
 total: 1301 for exp = 3</p>
<p>1634  8208  9474<br />
 total: 19316 for exp = 4</p>
<p>548834<br />
 total: 548834 for exp = 6</p>
<p>1741725 4210818 9800817 9926315 14459929<br />
 total: 40139604 for exp = 7</p>
<h4><u>Comparative Languages</u></h4>
<p><em>Mathematica: </em><tt>Sum[Boole[n == Tr[IntegerDigits[n]^5]] n, {n, 2, 1*^6}]</tt><br />
When calculating the answer for exp=7 the Perl program took 7 minutes, 16 seconds on a modest PC.  In contrast, the C++ version, listed below, took only 43 seconds; that’s about 10 times faster.<br />
We used our C++ program to calculate exp=8 which took about 10 minutes as compared to hours in Perl.</p>
<p>C++ Source: 565 sec. execution time</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;stdafx.h&quot;</span>
<span style="color: #339933;">#include &quot;math.h&quot;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
&nbsp;
<span style="color: #993333;">int</span> i<span style="color: #339933;">,</span>s<span style="color: #339933;">,</span>t<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>exp<span style="color: #339933;">=</span><span style="color: #0000dd;">8</span><span style="color: #339933;">;</span>
<span style="color: #993333;">long</span> <span style="color: #993333;">long</span>  max <span style="color: #339933;">=</span> pow<span style="color: #009900;">&#40;</span><span style="color:#800080;">9.0</span><span style="color: #339933;">,</span>exp<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>exp<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span> n <span style="color: #339933;">&lt;</span> max<span style="color: #339933;">;</span> n<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  s<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  i<span style="color: #339933;">=</span>n<span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    s <span style="color: #339933;">+=</span> pow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">%</span><span style="color:#800080;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>exp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    i <span style="color: #339933;">/=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> s <span style="color: #339933;">==</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%11u, &quot;</span><span style="color: #339933;">,</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>t<span style="color: #339933;">+=</span>s<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span>
<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;n total: %11u for exp = %2d&quot;</span><span style="color: #339933;">,</span> t<span style="color: #339933;">,</span>exp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Output:<br />
24678050, 24678051, 88593477<br />
 total: 137949578 for exp = 8</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/03/30/project-euler-problem-30-solution/feed/</wfw:commentRss>
		<slash:comments>0</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>Project Euler Problem 38 Solution</title>
		<link>http://blog.dreamshire.com/2009/03/26/project-euler-problem-38-solution/</link>
		<comments>http://blog.dreamshire.com/2009/03/26/project-euler-problem-38-solution/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 03:06:58 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 30-39]]></category>
		<category><![CDATA[Pandigital]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=99</guid>
		<description><![CDATA[What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Take the number 192 and multiply it by each of 1, 2, and 3:</p>
<p>192 × 1 = 192<br />
192 × 2 = 384<br />
192 × 3 = 576<br />
By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)</p>
<p>The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).</p>
<p>What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, &#8230; , <i>n</i>) where <i>n</i> > 1?</p>
<h4><u>Analysis</u></h4>
<p>We need to find a 1 to 9 pandigital number greater than 918273645 mentioned above (otherwise 918273645 would be the answer, and we tried that, and it wasn&#8217;t).  </p>
<p>[In the following paragraph we are using ? as a wild card definition for a digit [1-9] and the ++ as the concatenation operator.]</p>
<p>Starting with n=2, we can eliminate (9? × 1) ++ (9? × 2) and (9?? × 1) ++ (9?? × 2) because they will never yield a 9 digit number, but  (9??? × 1) ++ (9??? × 2) will, so n = 2 and our starting search must be a 4 digit number beginning with 9 and never contain a zero; namely 9123.  It should be obvious that any n>2 is impossible.</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_pandigital
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> n <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;">9876</span>, <span style="color: #ff4500;">9123</span>, -<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
  p = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: #66cc66;">*</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: #66cc66;">*</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> is_pandigital<span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>: 
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE38 = &quot;</span>, p
    <span style="color: #ff7700;font-weight:bold;">break</span></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>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/03/26/project-euler-problem-38-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
