<?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; Dynamic Programming (DP)</title>
	<atom:link href="http://blog.dreamshire.com/tag/dynamic-programming-dp/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 77 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/08/project-euler-problem-77-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/08/project-euler-problem-77-solution/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 07:05:51 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 70-79]]></category>
		<category><![CDATA[Dynamic Programming (DP)]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1565</guid>
		<description><![CDATA[What is the first value which can be written as the sum of primes in over five thousand different ways?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>It is possible to write ten as the sum of primes in exactly five different ways:</p>
<p style='margin-left:50px;'>7 + 3<br />
5 + 5<br />
5 + 3 + 2<br />
3 + 3 + 2 + 2<br />
2 + 2 + 2 + 2 + 2</p>
<p>What is the first value which can be written as the sum of primes in over five thousand different ways?</p>
<h4><u>Analysis</u></h4>
<p>A straightforward extension to problem 76 using primes instead of counting numbers and having a variable target instead of a fixed one.</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;">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;">29</span>,<span style="color: #ff4500;">31</span>,<span style="color: #ff4500;">37</span>,<span style="color: #ff4500;">41</span>,<span style="color: #ff4500;">43</span>,<span style="color: #ff4500;">47</span>,<span style="color: #ff4500;">53</span>,<span style="color: #ff4500;">59</span>,<span style="color: #ff4500;">61</span>,<span style="color: #ff4500;">67</span>,<span style="color: #ff4500;">71</span>,<span style="color: #ff4500;">73</span>,<span style="color: #ff4500;">79</span><span style="color: black;">&#93;</span>
&nbsp;
target = <span style="color: #ff4500;">11</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
  ways = <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>target
  <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> primes:
    <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>i, target+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
      ways<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span> += ways<span style="color: black;">&#91;</span>j-i<span style="color: black;">&#93;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> ways<span style="color: black;">&#91;</span>target<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">5000</span>: <span style="color: #ff7700;font-weight:bold;">break</span>
  target += <span style="color: #ff4500;">1</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE77 = &quot;</span>, target<span style="color: #66cc66;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>We selected this method because it uses fewer primes than having to guess at both the target and the number of primes needed.  Also, from the example, we can start at 11 for our search.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/06/08/project-euler-problem-77-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 82 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/05/project-euler-problem-82-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/05/project-euler-problem-82-solution/#comments</comments>
		<pubDate>Wed, 06 May 2009 06:29:26 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 80-89]]></category>
		<category><![CDATA[Common Routines]]></category>
		<category><![CDATA[Dynamic Programming (DP)]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

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

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

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

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=314</guid>
		<description><![CDATA[Find the maximum sum travelling from the top of the triangle to the base.]]></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 of the triangle below:</p>
<p>75<br />
95 64<br />
17 47 82<br />
18 35 87 10<br />
20 04 82 47 65<br />
<em>&#8230; {data continues} &#8230;</em></p>
<p>NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)</p>
<h4><u>Analysis</u></h4>
<p>To solve this problem and problem 67, which is much larger, start the search from the bottom to the top, adding the maximums along the way. This will &#8220;bubble&#8221; the maximum total to the top of the pyramid.</p>
<p>Let&#8217;s follow this technique, step by step, with the 4 row triangle example to show how this works.</p>
<p>3<br />
7 5<br />
2 4 6<br />
8 5 9 3<br />
OK, starting at the bottom,<br />
We look at 8 and 5, pick the maximum, 8 in this case, and replace the 2 in the previous row with their sum 10.<br />
We look next at 5 and 9, pick the maximum, 9, and replace the 4 in the previous row with their sum 13.<br />
We look lastly at 9 and 3, pick the maximum, 9, and replace the 6 in the previous row with their sum 15.<br />
Now our array looks like:<br />
3<br />
7 5<br />
10 13 15</p>
<p>Let&#8217;s do it again.  Take the larger of 10 and 13 and add it to 7 making 20.<br />
Take the larger of 13 and 15 and add it to 5 making 20.<br />
Now our array looks like:<br />
3<br />
20 20</p>
<p>At last we take the larger of 20 and 20 (yes, I know they&#8217;re the same) and add it to 3 making 23.<br />
And our array looks like:<br />
23<br />
The maximum total path in the triangle.</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 PE18 = $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>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 the <em>max_n</em> function.</li>
<li>See also problem <a href="http://blog.dreamshire.com/2009/04/01/project-euler-problem-67-solution/">67</a>, <a href="http://">81</a>, 82 &amp; 83. </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/01/project-euler-problem-18-solution/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 76 Solution</title>
		<link>http://blog.dreamshire.com/2009/03/31/project-euler-problem-76-solution/</link>
		<comments>http://blog.dreamshire.com/2009/03/31/project-euler-problem-76-solution/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 21:06:26 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 70-79]]></category>
		<category><![CDATA[Dynamic Programming (DP)]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=293</guid>
		<description><![CDATA[How many different ways can one hundred be written as a sum of at least two positive integers?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>It is possible to write five as a sum in exactly six different ways:</p>
<p>4 + 1<br />
3 + 2<br />
3 + 1 + 1<br />
2 + 2 + 1<br />
2 + 1 + 1 + 1<br />
1 + 1 + 1 + 1 + 1</p>
<p>How many different ways can one hundred be written as a sum of at least two positive integers?</p>
<h4><u>Analysis</u></h4>
<p>See Problem 31 as it is the same type of solution.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second.</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;">100</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@n</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>1<span style="color: #339933;">..</span>99<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;">$i</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@n</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;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">..</span><span style="color: #0000ff;">$target</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 PE76 = $ways[$target]&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>We were able to re-use our code from Problem 31.  </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/03/31/project-euler-problem-76-solution/feed/</wfw:commentRss>
		<slash:comments>1</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>
	</channel>
</rss>
