<?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 1-9</title>
	<atom:link href="http://blog.dreamshire.com/category/project-euler-solutions/project-euler-solutions-1-9/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 5 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/18/project-euler-problem-5-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/18/project-euler-problem-5-solution/#comments</comments>
		<pubDate>Mon, 18 May 2009 07:25:30 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 1-9]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1262</guid>
		<description><![CDATA[What is the smallest number divisible by each of the numbers 1 to 20?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.</p>
<p>What is the smallest number that is <dfn title="divisible with no remainder">evenly divisible</dfn> by all of the numbers from 1 to 20?</p>
<h4><u>Analysis</u></h4>
<p>Just find the least common multiple for the integers {2, 3, 4, &#8230;, 20} using Euclid&#8217;s algorithm. </p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> gcd<span style="color: black;">&#40;</span>a,b<span style="color: black;">&#41;</span>: 
  <span style="color: #ff7700;font-weight:bold;">return</span> b <span style="color: #ff7700;font-weight:bold;">and</span> gcd<span style="color: black;">&#40;</span>b, a <span style="color: #66cc66;">%</span> b<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> a
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> lcm<span style="color: black;">&#40;</span>a,b<span style="color: black;">&#41;</span>: 
  <span style="color: #ff7700;font-weight:bold;">return</span> a <span style="color: #66cc66;">*</span> b / gcd<span style="color: black;">&#40;</span>a,b<span style="color: black;">&#41;</span>
&nbsp;
n = <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">21</span><span style="color: black;">&#41;</span>:
    n = lcm<span style="color: black;">&#40;</span>n, i<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE5 = &quot;</span>,n</pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/18/project-euler-problem-5-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 6 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/17/project-euler-problem-6-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/17/project-euler-problem-6-solution/#comments</comments>
		<pubDate>Mon, 18 May 2009 06:21:44 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 1-9]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1277</guid>
		<description><![CDATA[What is the difference between the sum of the squares and the square of the sums?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The sum of the squares of the first ten natural numbers is,</p>
<div style="text-align:center">1<sup>2</sup> + 2<sup>2</sup> + &#8230; + 10<sup>2</sup> = 385</div>
<p>The square of the sum of the first ten natural numbers is,</p>
<div style="text-align:center">(1 + 2 + &#8230; + 10)<sup>2</sup> = 55<sup>2</sup> = 3025</div>
<p>Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 &minus; 385 = 2640.</p>
<p>Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.</p>
<h4><u>Analysis</u></h4>
<p>The formula for finding the square of the sum is adapted from Problem 1: (n*(n+1)/2)**2.  The formula for finding the sum of the squares is: n * (n+1) * (2*n+1) / 6.  We use these formulas and find the difference between the sum of the squares and the square of the sum.</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 = <span style="color: #ff4500;">100</span><span style="color: #66cc66;">;</span>
sqofsum = <span style="color: black;">&#40;</span>n<span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>n+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">**</span> <span style="color: #ff4500;">2</span>
sumofsq = n <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span>n+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>n+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">6</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE6 = &quot;</span>,sqofsum - sumofsq</pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/17/project-euler-problem-6-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 8 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/17/project-euler-problem-8-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/17/project-euler-problem-8-solution/#comments</comments>
		<pubDate>Mon, 18 May 2009 05:54:12 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 1-9]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1271</guid>
		<description><![CDATA[Discover the largest product of five consecutive digits in the 1000-digit number.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Find the greatest product of five consecutive digits in the 1000-digit number.</p>
<p style='font-family:courier new;font-size:10pt;text-align:center;'>
73167176531330624919225119674426574742355349194934<br />
96983520312774506326239578318016984801869478851843<br />
  &#8230; {continued}
</p>
<h4><u>Analysis</u></h4>
<p>Starting with the largest 5 digit number, 99999, count downwards until that number is a substring of the 1000 digit number.  Split out the five digits and multiply them together.  Ignore any products that could evaluate to zero.</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;">$string</span> <span style="color: #339933;">=</span> <span style="color: #009999;">&lt;DATA&gt;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$p</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">99999</span><span style="color: #339933;">;</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;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span> <span style="color: #339933;">!~</span> <span style="color: #009966; font-style: italic;">/0/</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">$string</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/$i/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$p</span><span style="color: #339933;">*=</span><span style="color: #0000ff;">$_</span> <span style="color: #b1b100;">for</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE8 = $p&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">exit</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">__DATA__</span>
731671765313306249<span style="color: #339933;">...</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>Finds the maximum in 121 iterations.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/17/project-euler-problem-8-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 4 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/17/project-euler-problem-4-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/17/project-euler-problem-4-solution/#comments</comments>
		<pubDate>Mon, 18 May 2009 04:59:44 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 1-9]]></category>
		<category><![CDATA[Palindromes]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1260</guid>
		<description><![CDATA[Find the largest palindrome made from the product of two 3-digit numbers.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 &times; 99.</p>
<p>Find the largest palindrome made from the product of two 3-digit numbers.</p>
<h4><u>Analysis</u></h4>
<p>The most efficient way to find the largest palindrome product of two 3-digit numbers is by searching odd factors downwards from 999.  We test each product as a palindrome and save it.  Our search ends when the next product falls below the largest palindrome found.</p>
<p>For a product to qualify as a palindrome the first and last digit must be the same.  In this case it will always be 9, an odd number that can only be derived by the multiplication of two odd numbers.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> is_palindromic<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>==<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
p=<span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">999</span>, <span style="color: #ff4500;">101</span>, -<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>i, <span style="color: #ff4500;">101</span>, -<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> i<span style="color: #66cc66;">*</span>j <span style="color: #66cc66;">&lt;</span> p: <span style="color: #ff7700;font-weight:bold;">break</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> is_palindromic<span style="color: black;">&#40;</span>i<span style="color: #66cc66;">*</span>j<span style="color: black;">&#41;</span>: p = i<span style="color: #66cc66;">*</span>j
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE4 = &quot;</span>,p</pre></div></div>

<h4><u>Comments</u></h4>
<p>Search took 3,072 iterations.<br />
The largest palindrome for some other cases: 4 digits: 9999 &times; 9901 = 99000099, 5 digits: 99979 &times; 99681 = 9966006699, 6 digits: 999999 &times; 999001 = 999000000999, 7 digits: 9997647 &times; 9998017 = 99956644665999</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/17/project-euler-problem-4-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 3 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/17/project-euler-problem-3-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/17/project-euler-problem-3-solution/#comments</comments>
		<pubDate>Mon, 18 May 2009 02:50:44 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 1-9]]></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=1250</guid>
		<description><![CDATA[Find the largest prime factor of a composite number.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The prime factors of 13195 are 5, 7, 13 and 29.</p>
<p>What is the largest prime factor of the number 600851475143 ?</p>
<h4><u>Analysis</u></h4>
<p>The request to find the prime factors of a number comes up much too often.  To satisfy these requests we wrote a small javascript program that can be accessed here: <a href="http://blog.dreamshire.com/2009/04/30/prime-factor-calculator/">prime factor calculator</a>.  It can easily handle numbers up to 16 digits long in an instant.</p>
<p>The solution below is the essence of that script.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> lpf<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> n <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">2</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">while</span> n <span style="color: #66cc66;">%</span> p == <span style="color: #ff4500;">0</span>: n /= p
    <span style="color: #ff7700;font-weight:bold;">if</span> n == <span style="color: #ff4500;">1</span>: <span style="color: #ff7700;font-weight:bold;">return</span> p
&nbsp;
  p = <span style="color: #ff4500;">5</span><span style="color: #66cc66;">;</span> <span style="color: #dc143c;">fl</span> = <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span>p<span style="color: #66cc66;">*</span>p<span style="color: #66cc66;">&lt;</span>=n<span style="color: black;">&#41;</span>: 
    <span style="color: #ff7700;font-weight:bold;">while</span> n <span style="color: #66cc66;">%</span> p == <span style="color: #ff4500;">0</span>: n /= p
    <span style="color: #ff7700;font-weight:bold;">if</span> n == <span style="color: #ff4500;">1</span>: <span style="color: #ff7700;font-weight:bold;">return</span> p
    p+= <span style="color: #ff4500;">3</span>-<span style="color: #dc143c;">fl</span>
    <span style="color: #dc143c;">fl</span> = -<span style="color: #dc143c;">fl</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> n
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE3 = &quot;</span>,lpf<span style="color: black;">&#40;</span><span style="color: #ff4500;">600851475143</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/17/project-euler-problem-3-solution/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 2 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/17/project-euler-problem-2-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/17/project-euler-problem-2-solution/#comments</comments>
		<pubDate>Mon, 18 May 2009 01:55:24 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 1-9]]></category>
		<category><![CDATA[Fibonacci Numbers]]></category>
		<category><![CDATA[Golden Ratio]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1241</guid>
		<description><![CDATA[Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:</p>
<p style='text-align:center;'>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, &#8230;</p>
<p>Find the sum of all the even-valued terms in the sequence which do not exceed four million.</p>
<h4><u>Analysis</u></h4>
<p>The approximate ratio between two consecutive terms in the Fibonacci sequence is the <a href="http://en.wikipedia.org/wiki/Golden_ratio">golden ratio</a> (phi, or φ &asymp; 1.618034).  It can also be demonstrated that every 3rd term of the sequence is even.  By combining this knowledge we can calculate the series of even Fibonacci numbers by multiplying the previous even term in the series by the cube of the golden ratio.</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;">limit = <span style="color: #ff4500;">4000000</span>
phi_cubed = <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>+<span style="color: #ff4500;">5</span><span style="color: #66cc66;">**</span>.5<span style="color: black;">&#41;</span>/<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">3</span>   <span style="color: #808080; font-style: italic;">#golden ratio cubed</span>
&nbsp;
f = <span style="color: #ff4500;">2</span>
<span style="color: #008000;">sum</span> = <span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">while</span> f <span style="color: #66cc66;">&lt;</span> limit:
  <span style="color: #008000;">sum</span> += f
  f = <span style="color: #008000;">round</span><span style="color: black;">&#40;</span>f<span style="color: #66cc66;">*</span>phi_cubed<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answewr to PE2 - &quot;</span>,<span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">sum</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>For a limit of 4,000,000 the loop iterates 10 times.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/17/project-euler-problem-2-solution/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 1 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/17/project-euler-problem-1-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/17/project-euler-problem-1-solution/#comments</comments>
		<pubDate>Mon, 18 May 2009 00:45:05 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 1-9]]></category>
		<category><![CDATA[Inclusion-Exclusion Principle]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1236</guid>
		<description><![CDATA[Add all the natural numbers below one thousand that are multiples of 3 or 5.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.</p>
<p>Find the sum of all the multiples of 3 or 5 below 1000.</p>
<h4><u>Analysis</u></h4>
<p>This is an ideal solution for the <a href="http://en.wikipedia.org/wiki/Inclusion-exclusion_principle">inclusion-exclusion principle</a>.  In general, we sum the numbers divisible by 3 or 5 for all numbers less than n, where n=1000 for this problem.  Next, we would need to subtracxt those numbers divisible by 15 because they are counted twice.</p>
<p>This solution is very extensible instead of using brute force that requires loops.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> sumn<span style="color: black;">&#40;</span>n,d<span style="color: black;">&#41;</span>:
  n=<span style="color: #008000;">int</span><span style="color: black;">&#40;</span>n/d<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>n<span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>n+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>d<span style="color: black;">&#41;</span>
&nbsp;
n = <span style="color: #ff4500;">999</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE1 = &quot;</span>,sumn<span style="color: black;">&#40;</span>n,<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span> + sumn<span style="color: black;">&#40;</span>n,<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span> - sumn<span style="color: black;">&#40;</span>n,<span style="color: #ff4500;">15</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>The formula to sum the counting numbers from 1 to n is: n(n+1)/2.  As for n=10; 10*11/2=55</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/17/project-euler-problem-1-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 7 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/12/project-euler-problem-7-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/12/project-euler-problem-7-solution/#comments</comments>
		<pubDate>Tue, 12 May 2009 19:13:03 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 1-9]]></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=1203</guid>
		<description><![CDATA[Find the 10001st prime.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6<img src="" style="display:none;" alt="^(" /><sup>th</sup> prime is 13.</p>
<p>What is the 10001<sup>st</sup> prime number?</p>
<h4><u>Analysis</u></h4>
<p>Having a sufficiently fast <em>is_prime() </em>funtion makes solving this problem quick and easy.  Just enumerate the positive odd integers from 7 that follow the sequence 6m&plusmn;1 until the n<sup>th</sup> prime is found.  For this problem n = 10001.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> Euler <span style="color: #ff7700;font-weight:bold;">import</span> is_prime  
&nbsp;
n = <span style="color: #ff4500;">10001</span>
p = <span style="color: #ff4500;">5</span><span style="color: #66cc66;">;</span> f = <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">while</span> n<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">3</span>:   <span style="color: #808080; font-style: italic;"># prime numbers  2, 3 and 5 are accounted for</span>
  p += <span style="color: #ff4500;">3</span>-f
  f = -f
  <span style="color: #ff7700;font-weight:bold;">if</span> is_prime<span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>: n -= <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE7 = &quot;</span>, p</pre></div></div>

<h4><u>Comments</u></h4>
<p><a href="http://blog.dreamshire.com/2009/03/26/94/#is_prime">Click here</a> for more information on the <em>is_prime() </em>function.<br />
It took 34,913 iterations to solve this problem.  Checking just odd numbers from 3 by 2&#8242;s takes 52,371 iterations.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/12/project-euler-problem-7-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 9 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/09/project-euler-problem-9-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/09/project-euler-problem-9-solution/#comments</comments>
		<pubDate>Sat, 09 May 2009 09:53:21 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 1-9]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1182</guid>
		<description><![CDATA[Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>A Pythagorean triplet is a set of three natural numbers, <var>a</var>  &le; <var>b</var> &lt; <var>c</var>, for which,</p>
<div style="text-align:center;"> <var>a</var><sup>2</sup> + <var>b</var><sup>2</sup> = <var>c</var><sup>2</sup></div>
<p>For example, 3<sup>2</sup> + 4<sup>2</sup> = 9 + 16 = 25 = 5<sup>2</sup>.</p>
<p>There exists exactly one Pythagorean triplet for which <var>a</var> + <var>b</var> + <var>c</var> = 1000.<br />Find the product <var>abc</var>.</p>
<h4><u>Analysis</u></h4>
<p><a href="http://www.math.uic.edu/~fields/puzzle/triples.html">This site</a> provided a simple formula for generating Pythagorean triplets.  Here&#8217;s the formula:<br />
Suppose that <EM>m</EM> and <EM>n</EM> are two positive integers, with <EM>m</EM> &LT; <EM>n</EM>.<br />
Then <EM>n</EM><SUP>2</SUP> &#8211; <EM>m</EM><SUP>2</SUP>, 2<EM>mn</EM>, and <EM>n</EM><SUP>2</SUP> + <EM>m</EM><SUP>2</SUP> is a Pythagorean triple.</p>
<p>Ok, let&#8217;s work this out:<br />
(n<sup>2</sup> -m<sup>2</sup>) + 2mn + (n<sup>2</sup> + m<sup>2</sup>) = 1000<br />
2n<sup>2</sup> + 2mn = 1000<br />
2n(n+m) = 1000<br />
n(n+m) = 500<br />
for m=20, a<br />
n<sup>2</sup> + mn = 500</p>
<p>So, to find the maximum n, such that m &lt; n, we search downwards and return the first one found.  An assumption for this method is that the gcd(a, b, c) = 1.  This simply means that a, b and c need to be reduced to their lowest terms.  For example, {6, 8, 10} is a Pythagorean triple, but can be reduced to {3, 4, 5}.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> pe9<span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>:
  LL = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>p<span style="color: #66cc66;">**</span>.5<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, LL, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, LL, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
        a = <span style="color: #008000;">abs</span><span style="color: black;">&#40;</span>j<span style="color: #66cc66;">*</span>j - i<span style="color: #66cc66;">*</span>i<span style="color: black;">&#41;</span>
        b = <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>i<span style="color: #66cc66;">*</span>j<span style="color: #66cc66;">;</span>
        c = i<span style="color: #66cc66;">*</span>i + j<span style="color: #66cc66;">*</span>j
        <span style="color: #ff7700;font-weight:bold;">if</span> a+b+c == p:
           <span style="color: #ff7700;font-weight:bold;">return</span> a<span style="color: #66cc66;">*</span>b<span style="color: #66cc66;">*</span>c
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE9 = &quot;</span>,pe9<span style="color: black;">&#40;</span><span style="color: #ff4500;">1000</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/09/project-euler-problem-9-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
