<?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 80-89</title>
	<atom:link href="http://blog.dreamshire.com/category/project-euler-solutions/project-euler-solutions-80-89/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dreamshire.com</link>
	<description>Showing what's behind the curtain</description>
	<lastBuildDate>Wed, 01 Sep 2010 02:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Project Euler Problem 82 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/05/project-euler-problem-82-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/05/project-euler-problem-82-solution/#comments</comments>
		<pubDate>Wed, 06 May 2009 06:29:26 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 80-89]]></category>
		<category><![CDATA[Common Routines]]></category>
		<category><![CDATA[Dynamic Programming (DP)]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

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

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

<h4><u>Comments</u></h4>
<ul>
<li>We’ve included the sample matrix as an example. </li>
<li>The first line of the program reads the data file, which is appended to the end of the program after the __DATA__ separator, into a two dimensional array named @rows. $nrows is the last index of the array so in this case 4.</li>
<li>Check <a href="http://blog.dreamshire.com/2009/03/26/94/#min_n">here</a> for more information on the <em>min_n</em> function.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/05/project-euler-problem-82-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 81 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/16/project-euler-problem-81-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/16/project-euler-problem-81-solution/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 23:01:59 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 80-89]]></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=714</guid>
		<description><![CDATA[Find the minimal path sum from the top left to the bottom right by moving right and down.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by <b>only moving to the right and down</b>, is indicated in red and is equal to 2427.</p>
<div style='text-align:center;'>
<table cellpadding='3' cellspacing='0' border='0'>
<tr>
<td><span style='color:#dd0000;'>131</span></td>
<td>673</td>
<td>234</td>
<td>103</td>
<td>18</td>
</tr>
<tr>
<td><span style='color:#dd0000;'>201</span></td>
<td><span style='color:#dd0000;'>96</span></td>
<td><span style='color:#dd0000;'>342</span></td>
<td>965</td>
<td>150</td>
</tr>
<tr>
<td>630</td>
<td>803</td>
<td><span style='color:#dd0000;'>746</span></td>
<td><span style='color:#dd0000;'>422</span></td>
<td>111</td>
</tr>
<tr>
<td>537</td>
<td>699</td>
<td>497</td>
<td><span style='color:#dd0000;'>121</span></td>
<td>956</td>
</tr>
<tr>
<td>805</td>
<td>732</td>
<td>524</td>
<td><span style='color:#dd0000;'>37</span></td>
<td><span style='color:#dd0000;'>331</span></td>
</tr>
</table>
</div>
<p>Find the minimal path sum, in <a href='http://www.projecteuler.net/project/matrix.txt'>matrix.txt</a> (right click and &#8216;Save Link/Target As&#8230;&#8217;), a 31K text file containing a 80 by 80 matrix, from the top left to the bottom right by only moving right and down.</p>
<h4><u>Analysis</u></h4>
<p>As with problems <a href="http://blog.dreamshire.com/2009/04/01/project-euler-problem-18-solution/">18</a> &#038; <a href="http://blog.dreamshire.com/2009/04/01/project-euler-problem-67-solution/">67</a> we have a designated starting and ending place.  We can use the same technique that solved those problems by starting at the bottom and replacing with minimal sums.  See those problems for further information.</p>
<p>Out of bounds (undefined) spaces are ignored from determining a minimum.</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: #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: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$j</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$nrows</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$j</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$j</span><span style="color: #339933;">--</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: #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> min_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: #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> <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 PE81 = $rows[0][0]&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000000; font-weight: bold;">__DATA__</span>
<span style="color: #cc66cc;">131</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">673</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">234</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">103</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">18</span> 
<span style="color: #cc66cc;">201</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">96</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">342</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">965</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">150</span> 
<span style="color: #cc66cc;">630</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">803</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">746</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">422</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">111</span> 
<span style="color: #cc66cc;">537</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">699</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">497</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">121</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">956</span> 
<span style="color: #cc66cc;">805</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">732</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">524</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">37</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">331</span></pre></div></div>

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

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=123</guid>
		<description><![CDATA[Investigating the number of rectangles in a rectangular grid.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Investigating the number of rectangles in a rectangular grid.<br />
By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles:</p>
<div><img src="http://projecteuler.net/project/images/p_085.gif" alt="Project Euler Problem 85"  height="203" width="350" /></div>
<p>Although no rectangular grid exists that contains exactly two million rectangles, find the area of the grid with the nearest solution.</p>
<h4><u>Analysis</u></h4>
<p>To find the sum of the counting numbers from 1 to n is given by the formula n(n+1)/2 or (n<sup>2</sup>+n)/2).  So, for example, the sum of the numbers from 1 to 100 is 100(101)/2 = 5050.</p>
<p>Counting the number of rectangles for an <i>x</i> by <i>y</i> grid is given by the formula: (x<sup>2</sup>+x) (y<sup>2</sup>+y) / 4.  This is the sum of the counting numbers (1..x) multiplied by the sum of the counting numbers (1..y).  In our example for x=3, y=2: (9 + 3) (4 + 2) / 4 = (12 x 6) / 4 = 72/4 = 18.  All we need to do is simply find an x and y that yield nearly 2,000,000 rectangles and calculate the area (xy).  </p>
<p>Like so many of these problems, more consideration goes into finding and proving the search parameters than solving the actual problem but let’s guess that an upper limit of 100 will work for both x and y and write a brute force program to see if it works:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">$min</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$max</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span>_000_000<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$x</span> <span style="color: #009900;">&#40;</span>  <span style="color: #cc66cc;">2</span> <span style="color: #339933;">..</span> <span style="color: #cc66cc;">100</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$y</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$x</span> <span style="color: #339933;">..</span> <span style="color: #cc66cc;">100</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$rec</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #0000ff;">*$x</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$y</span><span style="color: #0000ff;">*$y</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$diff</span> <span style="color: #339933;">=</span> <span style="color: #000066;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$max</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$rec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$diff</span><span style="color: #339933;">&lt;</span><span style="color: #0000ff;">$min</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #0000ff;">$min_rec</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$rec</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$min_x</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$x</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$min_y</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$y</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$min</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$diff</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Area = &quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$min_x</span><span style="color: #0000ff;">*$min_y</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot; ($min_x x $min_y) # Rectangles = $min_recn&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It solves the problem quickly and is fairly extensible.  But after looking at it for awhile we see that once a given x is chosen, the y can be derived by solving the quadratic equation:<br />
18 = (x<sup>2</sup>+x) (y<sup>2</sup>+y) / 4 or 72 = (x<sup>2</sup>+x) (y<sup>2</sup>+y).  So when x is known, as 3 for example:<br />
72 = (9 + 3) (y<sup>2</sup>+y) or 6 = y<sup>2</sup>+y which solves as y = (-3,2).  We are only concerned with the positive component.<br />
Further, the search for x can be terminated once x exceeds y.  This is a huge benefit because we have removed any justification required for limits and reduced the search to one loop and that loop does not have to check values that are irrelevant.  </p>
<h4><u>Solution</u></h4>
<p>Perl Source: < 1 sec. execution time</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$max</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$x</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$rec</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$diff</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min_x</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min_y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span>_000_000<span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span>_000_000<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">do</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$y</span><span style="color: #339933;">=</span><span style="color: #000066;">int</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">sqrt</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$max</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">4</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #0000ff;">*$x</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$rec</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$x</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$x</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;">$y</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$y</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$diff</span> <span style="color: #339933;">=</span> <span style="color: #000066;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$max</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$rec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$diff</span><span style="color: #339933;">&lt;</span><span style="color: #0000ff;">$min</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$min_rec</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min_x</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min_y</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$rec</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$x</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$y</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$diff</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: #b1b100;">until</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$y</span><span style="color: #339933;">&lt;=</span><span style="color: #0000ff;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Area = &quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$min_x</span><span style="color: #0000ff;">*$min_y</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot; ($min_x x $min_y) # Rectangles = $min_rec  (Target: $max, Delta: $min)  n&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>There are cases where a number of rectangles have many solutions and could yield different areas.  One such example is 315 rectangles which yields a=28 (2 x 14) and a=30 (5 x 6).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/03/27/project-euler-problem-85-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
