<?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 60-69</title>
	<atom:link href="http://blog.dreamshire.com/category/project-euler-solutions/project-euler-solutions-60-69/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 68 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/10/project-euler-problem-68-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/10/project-euler-problem-68-solution/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 03:19:20 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 60-69]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1618</guid>
		<description><![CDATA[What is the maximum 16-digit string for a "magic" 5-gon ring?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Consider the following &quot;magic&quot; 3-gon ring, filled with the numbers 1 to 6, and each line adding to nine.</p>
<div style="text-align:center;">
<img src="http://projecteuler.net/project/images/p_068_1.gif" alt="" />
</div>
<p>Working <b>clockwise</b>, and starting from the group of three with the numerically lowest external node (4,3,2 in this example), each solution can be described uniquely. For example, the above solution can be described by the set: 4,3,2; 6,2,1; 5,1,3.</p>
<p>It is possible to complete the ring with four different totals: 9, 10, 11, and 12. There are eight solutions in total.</p>
<div style="text-align:center;">
<table width="400" cellspacing="0" cellpadding="0">
<tr>
<td width="100"><b>Total</b></td>
<td width="300"><b>Solution Set</b></td>
</tr>
<tr>
<td>9</td>
<td>4,2,3; 5,3,1; 6,1,2</td>
</tr>
<tr>
<td>9</td>
<td>4,3,2; 6,2,1; 5,1,3</td>
</tr>
<tr>
<td>10</td>
<td>2,3,5; 4,5,1; 6,1,3</td>
</tr>
<tr>
<td>10</td>
<td>2,5,3; 6,3,1; 4,1,5</td>
</tr>
<tr>
<td>11</td>
<td>1,4,6; 3,6,2; 5,2,4</td>
</tr>
<tr>
<td>11</td>
<td>1,6,4; 5,4,2; 3,2,6</td>
</tr>
<tr>
<td>12</td>
<td>1,5,6; 2,6,4; 3,4,5</td>
</tr>
<tr>
<td>12</td>
<td>1,6,5; 3,5,4; 2,4,6</td>
</tr>
</table>
</div>
<p>By concatenating each group it is possible to form 9-digit strings; the maximum string for a 3-gon ring is 432621513.</p>
<p>Using the numbers 1 to 10, and depending on arrangements, it is possible to form 16- and 17-digit strings. What is the maximum <b>16-digit</b> string for a &quot;magic&quot; 5-gon ring?
<div style="text-align:center;">
<img src="http://projecteuler.net/project/images/p_068_2.gif" alt="" />
</div>
</p>
<h4><u>Analysis</u></h4>
<p>see Solution.</p>
<h4><u>Solution</u></h4>
<p>The clues &#8220;magic&#8221; and 16-digit not 17-digit number give this one away.  Just as the example, the bigger numbers go on the outside and the smaller numbers on the inside.  They total 14 from all views, and, following the instructions above:<br />
<em>Working <b>clockwise</b>, and starting from the group of three with the numerically lowest external node, each solution can be described uniquely. </em></p>
<p>On our first try we discovered the answer and have included it below, so don&#8217;t look if you want to face the challenge yourself.<br />
<img src="/images/p68.gif" /></p>
<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/06/10/project-euler-problem-68-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 65 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/10/project-euler-problem-65-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/10/project-euler-problem-65-solution/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 07:00:44 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 60-69]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=362</guid>
		<description><![CDATA[Find the sum of digits in the numerator of the 100th convergent of the continued fraction for <em>e</em>.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The square root of 2 can be written as an infinite continued fraction.</p>
<div style="margin-left:20px;">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>&radic;2 = 1 +</td>
<td colspan="4">
<div style="text-align:center;">1</p>
</div>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>2 +</td>
<td colspan="3">
<div style="text-align:center;">1<br /><img src="images/blackdot.gif" width="110" height="1" alt="" /></div>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>2 +</td>
<td colspan="2">
<div style="text-align:center;">1<br /><img src="images/blackdot.gif" width="85" height="1" alt="" /></div>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>2 +</td>
<td>
<div style="text-align:center;">1<br /><img src="images/blackdot.gif" width="60" height="1" alt="" /></div>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>2 + &#8230;</td>
</tr>
</table>
</div>
<p>The infinite continued fraction can be written, &radic;2 = [1;(2)], (2) indicates that 2 repeats <i>ad infinitum</i>. In a similar way, &radic;23 = [4;(1,3,1,8)].</p>
<p>It turns out that the sequence of partial values of continued fractions for square roots provide the best rational approximations. Let us consider the convergents for &radic;2.</p>
<div style="margin-left:20px;">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>1 +</td>
<td>
<div style="text-align:center;">1</p>
</div>
</td>
<td>= 3/2</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<div style="text-align:center;">2</div>
</td>
<td>&nbsp;</td>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>1 +</td>
<td colspan="2">
<div style="text-align:center;">1<br /><img src="images/blackdot.gif" width="50" height="1" alt="" /></div>
</td>
<td>= 7/5</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>2 +</td>
<td>
<div style="text-align:center;">1</p>
</div>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>
<div style="text-align:center;">2</div>
</td>
<td>&nbsp;</td>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>1 +</td>
<td colspan="3">
<div style="text-align:center;">1<br /><img src="images/blackdot.gif" width="80" height="1" alt="" /></div>
</td>
<td>= 17/12</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>2 +</td>
<td colspan="2">
<div style="text-align:center;">1<br /><img src="images/blackdot.gif" width="50" height="1" alt="" /></div>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>2 +</td>
<td>
<div style="text-align:center;">1</p>
</div>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>
<div style="text-align:center;">2</div>
</td>
<td>&nbsp;</td>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>1 +</td>
<td colspan="4">
<div style="text-align:center;">1<br /><img src="images/blackdot.gif" width="110" height="1" alt="" /></div>
</td>
<td>= 41/29</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>2 +</td>
<td colspan="3">
<div style="text-align:center;">1<br /><img src="images/blackdot.gif" width="80" height="1" alt="" /></div>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>2 +</td>
<td colspan="2">
<div style="text-align:center;">1<br /><img src="images/blackdot.gif" width="50" height="1" alt="" /></div>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>2 +</td>
<td>
<div style="text-align:center;">1</p>
</div>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>
<div style="text-align:center;">2</div>
</td>
<td>&nbsp;</td>
</tr>
</table>
</div>
<p>Hence the sequence of the first ten convergents for &radic;2 are:</p>
<div class="info">1, 3/2, 7/5, 17/12, 41/29, 99/70, 239/169, 577/408, 1393/985, 3363/2378, &#8230;</div>
<p>What is most surprising is that the important mathematical constant,<br /><i>e</i> = [2; 1,2,1, 1,4,1, 1,6,1 , ... , 1,2<i>k</i>,1, ...].</p>
<p>The first ten terms in the sequence of convergents for <i>e</i> are:</p>
<div class="info">2, 3, 8/3, 11/4, 19/7, 87/32, 106/39, 193/71, 1264/465, 1457/536, &#8230;</div>
<p>The sum of digits in the numerator of the 10<sup>th</sup> convergent is 1+4+5+7=17.</p>
<p>Find the sum of digits in the numerator of the 100<sup>th</sup> convergent of the continued fraction for <i>e</i>.</p>
<h4><u>Analysis</u></h4>
<p>Continue the series for the numerator to 100 terms and print the sum of the digits. </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;">n, d, i = <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">100</span>
<span style="color: #ff7700;font-weight:bold;">while</span> i<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span>:
  c = <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> i<span style="color: #66cc66;">%</span>3 == <span style="color: #ff4500;">0</span>: c = <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>i/<span style="color: #ff4500;">3</span>
  n, d, i = d, c<span style="color: #66cc66;">*</span>d+n, i-<span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE65 = &quot;</span>, <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>i<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;">str</span><span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/10/project-euler-problem-65-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 69 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/04/project-euler-problem-69-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/04/project-euler-problem-69-solution/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 08:52:52 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 60-69]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Totient]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=376</guid>
		<description><![CDATA[Find the value of n ≤ 1,000,000 for which n/φ(n) is a maximum.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Euler&#8217;s Totient function, φ(n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9)=6.</p>
<table>
<tr>
<th>n
<th>Relatively Prime
<th>φ(n)
<th>n/φ(n)</tr>
<tr>
<td>2
<td>1
<td>1
<td>2 </tr>
<tr>
<td>3
<td>1,2
<td>2
<td>1.5 </tr>
<tr>
<td>4
<td>1,3
<td>2
<td>2 </tr>
<tr>
<td>5
<td>1,2,3,4
<td>4
<td>1.25 </tr>
<tr>
<td>6
<td>1,5
<td>2
<td>3 </tr>
<tr>
<td>7
<td>1,2,3,4,5,6
<td>6
<td>1.1666&#8230; </tr>
<tr>
<td>8
<td>1,3,5,7
<td>4
<td>2 </tr>
<tr>
<td>9
<td>1,2,4,5,7,8
<td>6
<td>1.5 </tr>
<tr>
<td>10
<td>1,3,7,9
<td>4
<td>2.5 </tr>
</table>
<p>It can be seen that n=6 produces a maximum n/φ(n) for n &le; 10.</p>
<p>Find the value of n &le; 1,000,000 for which n/φ(n) is a maximum.</p>
<h4><u>Analysis</u></h4>
<p>We&#8217;re trying to find the smallest φ(<em>n</em>) and the largest <em>n</em> in order to keep <em>n</em>/φ(n) at a maximum.  One method is to multiply as many consecutive primes as possible without exceeding the limit of 1,000,000.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 seconds in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">primes = <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: #ff4500;">7</span>,<span style="color: #ff4500;">11</span>,<span style="color: #ff4500;">13</span>,<span style="color: #ff4500;">17</span>,<span style="color: #ff4500;">19</span>,<span style="color: #ff4500;">23</span>,<span style="color: #ff4500;">27</span>,<span style="color: #ff4500;">31</span>,<span style="color: #ff4500;">37</span>,<span style="color: #ff4500;">41</span><span style="color: black;">&#93;</span>
limit=<span style="color: #ff4500;">10</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">6</span>
<span style="color: #008000;">max</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> primes:
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">max</span> <span style="color: #66cc66;">*</span> p <span style="color: #66cc66;">&gt;</span> limit: <span style="color: #ff7700;font-weight:bold;">break</span>
  <span style="color: #008000;">max</span> <span style="color: #66cc66;">*</span>= p
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE69 = &quot;</span>, <span style="color: #008000;">max</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>for 10<sup>7</sup>? 9,699,690 = 2 x 3 x 5 x 7 x 11 x 13 x 17 x 19, and following the same method for 10<sup>9</sup> would be 223,092,870</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/04/project-euler-problem-69-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 62 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/03/project-euler-problem-62-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/03/project-euler-problem-62-solution/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 21:03:24 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 60-69]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=366</guid>
		<description><![CDATA[Find the smallest cube for which exactly five permutations of its digits are cube.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The cube, 41063625 (345<sup>3</sup>), can be permuted to produce two other cubes: 56623104 (384<sup>3</sup>) and 66430125 (405<sup>3</sup>). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.</p>
<p>Find the smallest cube for which exactly five permutations of its digits are cube.</p>
<h4><u>Analysis</u></h4>
<p>Well after establishing that 10,000 is as far as we need to go, we brute force a search by cubing all the counting numbers in the loop&#8217;s range and sorting the digits in ascending order to use as a hash key.  Then we increment a counter, <code>%sd</code>, and assign the first cube to our  <nobr><code>%cubes</code></nobr> hash using that key.  This way we are able to, later after the loop completes, grep out those cubes that belong to a family of 5 and find the minimum.</p>
<h4><u>Solution</u></h4>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$limit</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span>_000<span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$min_cube</span> <span style="color: #339933;">=</span> 9e20<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;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$x</span><span style="color: #339933;">&lt;</span><span style="color: #0000ff;">$limit</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$x</span><span style="color: #339933;">++,</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$x</span><span style="color: #339933;">**</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$cs</span> <span style="color: #339933;">=</span> <span style="color: #000066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #339933;">,</span> <span style="color: #000066;">sort</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span><span style="color: #0000ff;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$sd</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$cs</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">++;</span>
  <span style="color: #0000ff;">$cubes</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$cs</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">||=</span><span style="color: #0000ff;">$c</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #0000ff;">$min_cube</span> <span style="color: #339933;">=</span> min_n<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$min_cube</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$cubes</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">for</span> <span style="color: #000066;">grep</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$sd</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span> <span style="color: #000066;">keys</span> <span style="color: #0000ff;">%sd</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE62 = $min_cube&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>The Perl short circuit assignment operator, ||=, simply assigns a value to a variable only if that variable is undefined or zero.  Just a way to make sure the lowest value is set (since the loop is ascending) and never replaced.<br />
The series of cube roots are: 5027 7061 7202 8288 8384</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/03/project-euler-problem-62-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 67 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/01/project-euler-problem-67-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/01/project-euler-problem-67-solution/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 02:13:40 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 60-69]]></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=332</guid>
		<description><![CDATA[Using an efficient algorithm find the maximal sum in the triangle?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.</p>
<p>3<br />
7 5<br />
2 4 6<br />
8 5 9 3</p>
<p>That is, 3 + 7 + 4 + 9 = 23.</p>
<p>Find the maximum total from top to bottom in triangle.txt (right click and &#8216;Save Link/Target As&#8230;&#8217;), a 15K text file containing a triangle with one-hundred rows.</p>
<p>NOTE: This is a much more difficult version of Problem 18. It is not possible to try every route to solve this problem, as there are 2<sup>99</sup> altogether! If you could check one trillion (10<sup>12</sup>) routes every second it would take over twenty billion years to check them all. There is an efficient algorithm to solve it. ;o)</p>
<h4><u>Analysis</u></h4>
<p>Used the program from <a href="http://blog.dreamshire.com/2009/04/01/project-euler-problem-18-solution/">problem 18</a>.  It takes less than a second as compared to waiting for the sun to burn out.</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;">@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: #000066;">s</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: #b1b100;">my</span> <span style="color: #0000ff;">$nrows</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$#rows</span><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;">$i</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$nrows</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$j</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #0000ff;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$j</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+=</span> max_n<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$j</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$j</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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE67 = $rows[0][0]&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">__DATA__</span>
<span style="color: #cc66cc;">3</span>
<span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">5</span>
<span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">6</span>
<span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">3</span></pre></div></div>

<h4><u>Comments</u></h4>
<ul>
<li>We’ve included the sample data as an example.</li>
</li>
<p>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 3.</li>
<li>Check <a href="http://blog.dreamshire.com/2009/03/26/94/#min_n">here</a> for more information on <em>max_n()</em>.</li>
<li>See <a href="http://blog.dreamshire.com/2009/04/01/project-euler-problem-18-solution/">problem 18</a>, <a href="http://blog.dreamshire.com/2009/04/16/project-euler-problem-81-solution/">problem 81</a>, 82 &amp; 83.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/01/project-euler-problem-67-solution/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 63 Solution</title>
		<link>http://blog.dreamshire.com/2009/03/30/project-euler-problem-63-solution/</link>
		<comments>http://blog.dreamshire.com/2009/03/30/project-euler-problem-63-solution/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 07:58:28 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 60-69]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=170</guid>
		<description><![CDATA[How many n-digit positive integers exist which are also an nth power?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The 5-digit number, 16807=7<sup>5</sup>, is also a fifth power. Similarly, the 9-digit number, 134217728=8<sup>9</sup>, is a ninth power.</p>
<p>How many <em>n</em>-digit positive integers exist which are also an <em>n</em>th power?</p>
<h4><u>Analysis</u></h4>
<p>Note: log() = log<sub>10</sub>; ln() = log<sub><em>e</em></sub>.  One way to know how many digits a number has is to use int(log n) +1 or if you don’t have a log function then use the natural log function as int(ln n / ln 10) +1.  For this problem n cannot be greater then 10 since 10<sup>n</sup> is always n+1 digit long.  </p>
<p>So we need to create a function <em>f</em> such that <em>f(n)</em> = upper limit of n.   Using our log function and using 9 as an example we define <em>f(9)</em> as int (1 / (1 &#8211; log<sub>10</sub> 9)) or 21 such that 9<sup>n</sup> < 10<sup>n-1</sup>. Thus 9<sup>n</sup> is n digits long when n &le; 21. We repeat this for 1 through 8 and sum for an answer.</p>
<h4><u>Solution</u></h4>
<table border="0">
<tr>
<th>n</th>
<th>int( 1 / (1-log<sub>10</sub> n ) )</th>
</tr>
<tr>
<td style="text-align: center">1</td>
<td style="text-align: right"> 1 </td>
</tr>
<tr>
<td style="text-align: center">2</td>
<td style="text-align: right"> 1 </td>
</tr>
<tr>
<td style="text-align: center">3</td>
<td style="text-align: right"> 1 </td>
</tr>
<tr>
<td style="text-align: center">4</td>
<td style="text-align: right"> 2 </td>
</tr>
<tr>
<td style="text-align: center">5</td>
<td style="text-align: right"> 3 </td>
</tr>
<tr>
<td style="text-align: center">6</td>
<td style="text-align: right"> 4 </td>
</tr>
<tr>
<td style="text-align: center">7</td>
<td style="text-align: right"> 6 </td>
</tr>
<tr>
<td style="text-align: center">8</td>
<td style="text-align: right"> 10 </td>
</tr>
<tr>
<td style="text-align: center">9</td>
<td style="text-align: right"> 21 </td>
</tr>
</table>
<h4><u>Comments</u></h4>
<p>We didn&#8217;t see a need to write a program for this one.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/03/30/project-euler-problem-63-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
