<?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 40-49</title>
	<atom:link href="http://blog.dreamshire.com/category/project-euler-solutions/project-euler-solutions-40-49/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 49 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/05/project-euler-problem-49-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/05/project-euler-problem-49-solution/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 09:52:09 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></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=1467</guid>
		<description><![CDATA[Find arithmetic sequences, made of prime terms, whose four digits are permutations of each other.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.</p>
<p>There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.</p>
<p>What 12-digit number do you form by concatenating the three terms in this sequence?</p>
<h4><u>Analysis</u></h4>
<p>An obvious solution.  Start with the known solution plus 2 (1487+2) and find the next set checking only 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;">from</span> Euler <span style="color: #ff7700;font-weight:bold;">import</span> is_prime, is_perm
&nbsp;
n = <span style="color: #ff4500;">1489</span>  <span style="color: #808080; font-style: italic;"># must be odd</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
  b, c = n+<span style="color: #ff4500;">3330</span>, n+<span style="color: #ff4500;">6660</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> is_prime<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> is_prime<span style="color: black;">&#40;</span>b<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> is_prime<span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span> \
    <span style="color: #ff7700;font-weight:bold;">and</span> is_perm<span style="color: black;">&#40;</span>n,b<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> is_perm<span style="color: black;">&#40;</span>b,c<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">break</span>
  n += <span style="color: #ff4500;">2</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE49 = &quot;</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>b<span style="color: black;">&#41;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
<ul>
<li>More information on the <em>Euler</em> module can be found on the <a href="http://blog.dreamshire.com/2009/03/26/94/">tools page</a>.</li>
<li>It wasn&#8217;t clear, perhaps intentionally, that the terms in the sequence would increase by the same amount (3330) as the example.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/06/05/project-euler-problem-49-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 47 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/05/project-euler-problem-47-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/05/project-euler-problem-47-solution/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 08:51:57 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></category>
		<category><![CDATA[Prime Factors]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1465</guid>
		<description><![CDATA[Find the first four consecutive integers to have four distinct primes factors.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The first two consecutive numbers to have two distinct prime factors are:</p>
<p style="margin-left:100px;">14 = 2 &times; 7<br />15 = 3 &times; 5</p>
<p>The first three consecutive numbers to have three distinct prime factors are:</p>
<p style="margin-left:100px;">644 = 2&sup2; &times; 7 &times; 23<br />645 = 3 &times; 5 &times; 43<br />646 = 2 &times; 17 &times; 19.</p>
<p>Find the first four consecutive integers to have four distinct primes factors. What is the first of these numbers?</p>
<h4><u>Analysis</u></h4>
<p>Simple approach that took only a few minutes to write.  Start with the first number that has 4 distinct prime factors.  Continue to increment from that number until 4 consecutive numbers have four distinct prime factors.</p>
<h4><u>Solution</u></h4>
<p>Runs < 5 seconds 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> factor
&nbsp;
c = <span style="color: #ff4500;">1</span>
n = <span style="color: #ff4500;">2</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">3</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">5</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">7</span>
<span style="color: #ff7700;font-weight:bold;">while</span> c <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">4</span>:
  n += <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>factor<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">4</span>: c += <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">else</span>: c = <span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE47 = &quot;</span>, n-<span style="color: #ff4500;">3</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>More information on the <em>Euler</em> module can be found on the <a href="http://blog.dreamshire.com/2009/03/26/94/">tools page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/06/05/project-euler-problem-47-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 46 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/28/project-euler-problem-46-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/28/project-euler-problem-46-solution/#comments</comments>
		<pubDate>Thu, 28 May 2009 09:24:44 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></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=1218</guid>
		<description><![CDATA[What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.</p>
<p style='margin-left:10px;'>9 = 7 + 2&times;1<sup>2</sup><br />
15 = 7 + 2&times;2<sup>2</sup><br />
21 = 3 + 2&times;3<sup>2</sup><br />
25 = 7 + 2&times;3<sup>2</sup><br />
27 = 19 + 2&times;2<sup>2</sup><br />
33 = 31 + 2&times;1<sup>2</sup></p>
<p>It turns out that the conjecture was false.</p>
<p>What is the smallest odd composite that cannot be written as the sum of a prime and twice a square</p>
<h4><u>Analysis</u></h4>
<p>Generate primes as needed and check the conjecture until a prime isn&#8217;t found.</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;">5</span>
f = <span style="color: #ff4500;">1</span>
primes = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">all</span><span style="color: black;">&#40;</span> n <span style="color: #66cc66;">%</span> p <span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> primes <span style="color: black;">&#41;</span>:
    primes.<span style="color: black;">add</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">else</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">any</span><span style="color: black;">&#40;</span> <span style="color: black;">&#40;</span>n-<span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>i<span style="color: #66cc66;">*</span>i<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> primes <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, n<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">break</span>
  n += <span style="color: #ff4500;">3</span>-f
  f = -f
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE46 = &quot;</span>, n</pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/28/project-euler-problem-46-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 40 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/25/project-euler-problem-40-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/25/project-euler-problem-40-solution/#comments</comments>
		<pubDate>Mon, 25 May 2009 20:05:07 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></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/2009/05/25/project-euler-problem-40-solution/</guid>
		<description><![CDATA[Finding the nth digit of the fractional part of the irrational number.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>An irrational decimal fraction is created by concatenating the positive integers:</p>
<p style='text-align:center;'>0.12345678910<span style='color:#dd0000;font-size:14pt;'>1</span>112131415161718192021&#8230;</p>
<p>It can be seen that the 12<sup>th</sup> digit of the fractional part is 1.</p>
<p>If <i>d</i><sub><i>n</i></sub> represents the <i>n</i><sup>th</sup> digit of the fractional part, find the value of the following expression.</p>
<p style='text-align:center;'><i>d</i><sub>1</sub> &times; <i>d</i><sub>10</sub> &times; <i>d</i><sub>100</sub> &times; <i>d</i><sub>1000</sub> &times; <i>d</i><sub>10000</sub> &times; <i>d</i><sub>100000</sub> &times; <i>d</i><sub>1000000</sub></p>
<h4><u>Analysis</u></h4>
<p>This is known as Champernowne&#8217;s constant (OEIS <a href="http://www.research.att.com/~njas/sequences/A033307">A033307</a>).</p>
<p>We concatenate the counting numbers to a string from 1 until the length reaches at least 1 million digits, then multiply the digit at the appropriate positions together.  We know from the problem description that the first two terms <i>d</i><sub>1</sub> &amp; <i>d</i><sub>10</sub> will evaluate to 1 and, therefore, serve no consequence on the product.</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;">$s</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'.'</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: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$s</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">$n</span><span style="color: #339933;">++</span> <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$s</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">1</span>_000_000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE40 = &quot;</span><span style="color: #339933;">,</span>
<span style="color: #000066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$s</span><span style="color: #339933;">,</span>1e2<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: #000066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$s</span><span style="color: #339933;">,</span>1e3<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: #000066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$s</span><span style="color: #339933;">,</span>1e4<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: #000066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$s</span><span style="color: #339933;">,</span>1e5<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: #000066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$s</span><span style="color: #339933;">,</span>1e6<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<ul>
<li>We initialize $s with a character to start the string index at 1 instead of 0.</li>
<li><em>Mathematica: </em><tt>Times @@ Flatten[IntegerDigits@Range@2*^5][[10^Range@6]]</tt></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/25/project-euler-problem-40-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 43 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/28/project-euler-problem-43-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/28/project-euler-problem-43-solution/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 09:36:44 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=995</guid>
		<description><![CDATA[Find the sum of all pandigital numbers with an unusual sub-string divisibility property.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.</p>
<p>Let <i>d</i><sub>1</sub> be the 1<sup>st</sup> digit, <i>d</i><sub>2</sub> be the 2<sup>nd</sup> digit, and so on. In this way, we note the following:</p>
<ul>
<li><i>d</i><sub>2</sub><i>d</i><sub>3</sub><i>d</i><sub>4</sub>=406 is divisible by 2</li>
<li><i>d</i><sub>3</sub><i>d</i><sub>4</sub><i>d</i><sub>5</sub>=063 is divisible by 3</li>
<li><i>d</i><sub>4</sub><i>d</i><sub>5</sub><i>d</i><sub>6</sub>=635 is divisible by 5</li>
<li><i>d</i><sub>5</sub><i>d</i><sub>6</sub><i>d</i><sub>7</sub>=357 is divisible by 7</li>
<li><i>d</i><sub>6</sub><i>d</i><sub>7</sub><i>d</i><sub>8</sub>=572 is divisible by 11</li>
<li><i>d</i><sub>7</sub><i>d</i><sub>8</sub><i>d</i><sub>9</sub>=728 is divisible by 13</li>
<li><i>d</i><sub>8</sub><i>d</i><sub>9</sub><i>d</i><sub>10</sub>=289 is divisible by 17</li>
</ul>
<p>Find the sum of all 0 to 9 pandigital numbers with this property.</p>
<h4><u>Analysis</u></h4>
<p>There are only 9 x 9! (3,265,920) possible 0-9 10-digit pandigital numbers.  Simply generate the possibilities and check each 3-digit section to be divisible by the appropriate prime number.  But even on a fast computer this method broke the one minute limitation.</p>
<p>Clearly something had to be done to reduce the number of permutations and upon closer inspection some rules were discovered.  Keep in mind we are dealing with 3 digit numbers that must have unique digits in order to qualify as part of a 0-9 pandigital 10-digit number.</p>
<p>Rule 0: Can&#8217;t start with {0}.</p>
<p>Rule 1: Since <i>d</i><sub>4</sub>  has to be divisible by 2 then <i>d</i><sub>4</sub> has to be from the set {0, 2, 4, 6, 8}.</p>
<p>Rule 2: Since <i>d</i><sub>4</sub><i>d</i><sub>5</sub><i>d</i><sub>6</sub> has to be divisible by 5 then <i>d</i><sub>6</sub> has to be from the set {0, 5}.</p>
<p>Rule 3: <i>d</i><sub>6</sub><i>d</i><sub>7</sub><i>d</i><sub>8</sub> has to be divisible by 11 and, according to Rule 2, has to start with a {0, 5}.  Well, it can&#8217;t start with 0 as that would yield invalid numbers such as {011, 022, &#8230; 099}, so it has to start with 5 and have only unique digits.  The set is<br />
{506, 517, 528, 539, 561, 572, 583, 594}. </p>
<p>New Rule 2: Amend Rule 2 to <i>d</i><sub>6</sub> = {5}.</p>
<p>Rule 5: Since <i>d</i><sub>7</sub><i>d</i><sub>8</sub><i>d</i><sub>9</sub> must be divisible by 13,  begin with {06, 17, 28, 39, 61, 72, 83, 94}, contain no 5s and no repeated digits our set becomes {286, 390, 728, 832}.</p>
<p>Rule 6: Since <i>d</i><sub>8</sub><i>d</i><sub>9</sub><i>d</i><sub>10</sub> must be divisible by 17,  begin with {28, 32, 86, 90}, contain no 5s and no repeated digits our set becomes {289, 867, 901}.</p>
<p>Let&#8217;s review what we have and what combinations are possible for <i>d</i><sub>6</sub> through <i>d</i><sub>10</sub>:<br />
<i>d</i><sub>6</sub> = {5}<br />
<i>d</i><sub>7</sub><i>d</i><sub>8</sub><i>d</i><sub>9</sub> = {286, 390, 728}<br />
<i>d</i><sub>8</sub><i>d</i><sub>9</sub><i>d</i><sub>10</sub> = {289, 867, 901}</p>
<p><i>d</i><sub>6</sub><i>d</i><sub>7</sub><i>d</i><sub>8</sub><i>d</i><sub>9</sub><i>d</i><sub>10</sub> = {52867, 53901, 57289}</p>
<p>Rule 7: <i>d</i><sub>5</sub><i>d</i><sub>6</sub><i>d</i><sub>7</sub> must be divisible by 7 and end with {52, 53, 57} yields {357, 952}</p>
<p>Rule 1: Amend Rule 1 to <i>d</i><sub>4</sub> = {0, 4, 6}.</p>
<p>Our solution set ends with {&#8230;.357289 or &#8230;.952867}</p>
<p>You could finish the problem furthering this technique, but we reduced it enough to have the computer solve the rest.</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> fact<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;">reduce</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x,y:x<span style="color: #66cc66;">*</span>y,<span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,n+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> perm<span style="color: black;">&#40;</span>n, s<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span>==<span style="color: #ff4500;">1</span>: <span style="color: #ff7700;font-weight:bold;">return</span> s
  q, r = <span style="color: #008000;">divmod</span><span style="color: black;">&#40;</span>n, fact<span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> s<span style="color: black;">&#91;</span>q<span style="color: black;">&#93;</span> + perm<span style="color: black;">&#40;</span>r, s<span style="color: black;">&#91;</span>:q<span style="color: black;">&#93;</span> + s<span style="color: black;">&#91;</span>q+<span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> check<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  s = <span style="color: #008000;">str</span><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;">int</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span> == <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">3</span> == <span style="color: #ff4500;">0</span>
&nbsp;
s = <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;">0</span>,<span style="color: #ff4500;">18</span><span style="color: black;">&#41;</span>:
  a = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span> perm<span style="color: black;">&#40;</span>i,<span style="color: #483d8b;">'4310'</span><span style="color: black;">&#41;</span>+<span style="color: #483d8b;">'952867'</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> check<span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span>: s += a
  a = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span> perm<span style="color: black;">&#40;</span>i,<span style="color: #483d8b;">'6410'</span><span style="color: black;">&#41;</span>+<span style="color: #483d8b;">'357289'</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> check<span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span>: s += a
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE43 = &quot;</span>, s</pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/28/project-euler-problem-43-solution/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 42 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/27/project-euler-problem-42-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/27/project-euler-problem-42-solution/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 18:17:52 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></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=987</guid>
		<description><![CDATA[How many triangle words does the list of common English words contain?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The <i>n</i><sup>th</sup> term of the sequence of triangle numbers is given by, <i>t<sub>n</sub></i> = &frac12;<i>n</i>(<i>n</i>+1); so the first ten triangle numbers are:</p>
<p style='text-align:center;'>1, 3, 6, 10, 15, 21, 28, 36, 45, 55, &#8230;</p>
<p>By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = <i>t</i><sub>10</sub>. If the word value is a triangle number then we shall call the word a triangle word.</p>
<p>Using <a href='http://projecteuler.net/project/words.txt'>words.txt</a> (right click and &#8216;Save Link/Target As&#8230;&#8217;), a 16K text file containing nearly two-thousand common English words, how many are triangle words?</p>
<h4><u>Analysis</u></h4>
<p>Remember <a href="http://blog.dreamshire.com/2009/04/17/project-euler-problem-44-solution/">Problems 44 &#038; 45</a>?  The same logic applies except for triangle numbers.  Whenever the equation ( &radic;(1+8n) &#8211; 1 ) / 2 evaluates to an integer the number, <em>n</em>, is a triangle number.</p>
<p>Remember <a href="http://blog.dreamshire.com/2009/04/09/project-euler-problem-22-solution/">Problem 22</a>?  Well, this is the same core solution. </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;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">@h</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'A'</span> <span style="color: #339933;">..</span> <span style="color: #ff0000;">'Z'</span><span style="color: #009900;">&#125;</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;">26</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">open</span> <span style="color: #009900;">&#40;</span>IN<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;&lt;words.txt&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Can't open file: $!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">@words</span> <span style="color: #339933;">=</span> <span style="color: #000066;">map</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/&quot;(w+)&quot;/</span>  <span style="color: #009900;">&#125;</span> <span style="color: #000066;">split</span> <span style="color: #009966; font-style: italic;">/,/</span><span style="color: #339933;">,</span><span style="color: #009999;">&lt;IN&gt;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000066;">grep</span> <span style="color: #009900;">&#123;</span> is_tri<span style="color: #009900;">&#40;</span>score<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@words</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE42 = $c&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> is_tri <span style="color: #009900;">&#123;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$n</span><span style="color: #339933;">=</span><span style="color: #000066;">shift</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$t</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">8</span><span style="color: #0000ff;">*$n</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000066;">return</span> <span style="color: #000066;">int</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$t</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">$t</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">sub</span> score <span style="color: #009900;">&#123;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$score</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$score</span><span style="color: #339933;">+=</span><span style="color: #0000ff;">$_</span> <span style="color: #b1b100;">for</span> <span style="color: #0000ff;">@h</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">map</span> <span style="color: #000066;">uc</span><span style="color: #339933;">,</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span><span style="color: #000066;">shift</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #000066;">return</span> <span style="color: #0000ff;">$score</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>Even though we used Perl, we still kept it readable, mostly.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/27/project-euler-problem-42-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 44 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/17/project-euler-problem-44-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/17/project-euler-problem-44-solution/#comments</comments>
		<pubDate>Sat, 18 Apr 2009 04:45:52 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></category>
		<category><![CDATA[Pentagonal Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=731</guid>
		<description><![CDATA[Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Pentagonal numbers are generated by the formula, P<sub><var>n</var></sub>=<var>n</var>(3<var>n</var>&minus;1)/2. The first ten pentagonal numbers are:</p>
<p style="text-align:center;">1, 5, 12, 22, 35, 51, 70, 92, 117, 145, &#8230;</p>
<p>It can be seen that P<sub>4</sub> + P<sub>7</sub> = 22 + 70 = 92 = P<sub>8</sub>. However, their difference, 70 &minus; 22 = 48, is not pentagonal.</p>
<p>Find the pair of pentagonal numbers, P<sub><var>j</var></sub> and P<sub><var>k</var></sub>, for which their sum and difference is pentagonal and D = |P<sub><var>k</var></sub> &minus; P<sub><var>j</var></sub>| is minimised; what is the value of D?</p>
<h4><u>Analysis</u></h4>
<p>How many pentagonal numbers are required to search through to solve the problem?  We wrote the following program and started with 10,000 pentagonal numbers and, after finding the correct answer, adjusted the requirement down to 2,400.  When the problem asks for D = |P<sub><var>k</var></sub> &minus; P<sub><var>j</var></sub>| to be minimized it simply means the smallest pair which, by our method, is the first occurrence found.</p>
<p>A set (hashable) is used to both hold the pentagonal numbers and to check if a number is pentagonal.  All that&#8217;s left is to permute the pairs and check that the difference and sum are pentagonal numbers.<br />
Runs < 2 seconds in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">pent = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span> n <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">*</span>n - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">2</span> <span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">2400</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span> 
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> pe44<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">for</span> pj <span style="color: #ff7700;font-weight:bold;">in</span> pent: 
    <span style="color: #ff7700;font-weight:bold;">for</span> pk <span style="color: #ff7700;font-weight:bold;">in</span> pent: 
      <span style="color: #ff7700;font-weight:bold;">if</span> pj - pk <span style="color: #ff7700;font-weight:bold;">in</span> pent <span style="color: #ff7700;font-weight:bold;">and</span> pj + pk <span style="color: #ff7700;font-weight:bold;">in</span> pent: 
        <span style="color: #ff7700;font-weight:bold;">return</span> pj - pk
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE44 = &quot;</span>,pe44<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>So, a more challenging program might be to solve the problem without having to guess at how many pentagonal numbers are required.  That would require a function to determine if a number is pentagonal. (When the equation (&radic;(1 + 24 &middot; <em>n</em>) + 1) / 6 evaluates to an integer, <em>n</em>, is a pentagonal number.)</p>
<p>It turned out to be an interesting discovery and took longer to find an answer.  But, as luck would have it, was very helpful for solving problem 45.</p>
<h4><u>Solution</u></h4>
<p>Runs < 6 seconds 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> <span style="color: #dc143c;">math</span> <span style="color: #ff7700;font-weight:bold;">import</span> sqrt
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> is_pentagonal<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  p = <span style="color: black;">&#40;</span> sqrt<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span> + <span style="color: #ff4500;">24</span><span style="color: #66cc66;">*</span>n<span style="color: black;">&#41;</span> + <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;">return</span> p==<span style="color: #008000;">int</span><span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> pe44<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
  i=<span style="color: #ff4500;">0</span><span style="color: #66cc66;">;</span>
  pent = <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
    i+=<span style="color: #ff4500;">1</span>
    pent.<span style="color: black;">append</span><span style="color: black;">&#40;</span> i <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">*</span>i - <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: #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>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>pent<span style="color: black;">&#41;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> is_pentagonal<span style="color: black;">&#40;</span>pent<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>-pent<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> is_pentagonal<span style="color: black;">&#40;</span>pent<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>+pent<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> pent<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>-pent<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE44 = &quot;</span>,pe44<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>See <a href="http://blog.dreamshire.com/2009/04/10/project-euler-problem-45-solution/">Problem 45</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/17/project-euler-problem-44-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 45 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/10/project-euler-problem-45-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/10/project-euler-problem-45-solution/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 19:00:18 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></category>
		<category><![CDATA[Pentagonal Numbers]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=551</guid>
		<description><![CDATA[After 40755, what is the next triangle number that is also pentagonal and hexagonal?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:</p>
<table>
<tr>
<td>Triangle</td>
<td>&nbsp;</td>
<td>T<sub><i>n</i></sub>=<i>n</i>(<i>n</i>+1)/2</td>
<td>&nbsp;</td>
<td>1, 3, 6, 10, 15, &#8230;</td>
</tr>
<tr>
<td>Pentagonal</td>
<td>&nbsp;</td>
<td>P<sub><i>n</i></sub>=<i>n</i>(3<i>n</i>&minus;1)/2</td>
<td>&nbsp;</td>
<td>1, 5, 12, 22, 35, &#8230;</td>
</tr>
<tr>
<td>Hexagonal</td>
<td>&nbsp;</td>
<td>H<sub><i>n</i></sub>=<i>n</i>(2<i>n</i>&minus;1)</td>
<td>&nbsp;</td>
<td>1, 6, 15, 28, 45, &#8230;</td>
</tr>
</table>
<p>It can be verified that T<sub>285</sub> = P<sub>165</sub> = H<sub>143</sub> = 40755.</p>
<p>Find the next triangle number that is also pentagonal and hexagonal.</p>
<h4><u>Analysis</u></h4>
<p>Note that Hexagonal numbers are a subset of Triangle numbers so we only determine the first occurrence of H<sub>i</sub> = P<sub>j</sub> to find our answer.  We can safely start our search from 166 as eluded to in the problem&#8217;s description.  </p>
<p>From our experience with problem 44 we were able to make our search trivial without the use of arrays.</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> <span style="color: #dc143c;">math</span> <span style="color: #ff7700;font-weight:bold;">import</span> sqrt
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> is_pentagonal<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  p = <span style="color: black;">&#40;</span> sqrt<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span> + <span style="color: #ff4500;">24</span><span style="color: #66cc66;">*</span>n<span style="color: black;">&#41;</span> + <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;">return</span> p==<span style="color: #008000;">int</span><span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>
&nbsp;
n = <span style="color: #ff4500;">165</span> 
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
  n += <span style="color: #ff4500;">1</span> 
  <span style="color: #008000;">hex</span> = n <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: #ff7700;font-weight:bold;">if</span> is_pentagonal<span style="color: black;">&#40;</span><span style="color: #008000;">hex</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">break</span> 
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE45 = &quot;</span>,<span style="color: #008000;">hex</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>See <a href="http://blog.dreamshire.com/2009/04/17/project-euler-problem-44-solution/">Problem 44</a>.<br />
The next number in the series is 57722156241751, calculated in about 15 seconds.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/10/project-euler-problem-45-solution/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 41 Solution</title>
		<link>http://blog.dreamshire.com/2009/03/26/project-euler-problem-41-solution/</link>
		<comments>http://blog.dreamshire.com/2009/03/26/project-euler-problem-41-solution/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 22:18:15 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></category>
		<category><![CDATA[Pandigital]]></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=85</guid>
		<description><![CDATA[What is the largest <em>n</em>-digit pandigital prime that exists?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>We shall say that an <em>n</em>-digit number is pandigital if it makes use of all the digits 1 to <em>n</em> exactly once. For example, 2143 is a 4-digit pandigital and is also prime.</p>
<p>What is the largest <em>n</em>-digit pandigital prime that exists?</p>
<h4><u>Analysis</u></h4>
<p>What is the value of <i>n</i>?  We know it&#8217;s between 4 and 9 but that leaves a huge set of prime number candidates to search through.  So let&#8217;s eliminate some values of <i>n</i> by using the rule that any integer is divisible by 3 or 9 whose sum of digits is divisible by 3 or 9; therefore composite and not prime. </p>
<p>9+8+7+6+5+4+3+2+1 = 45,<br />
8+7+6+5+4+3+2+1 = 36,<br />
6+5+4+3+2+1 = 21, and<br />
5+4+3+2+1 = 15,<br />
all of which are divisible by 3 and therefore could not yield a 1 to <em>{5, 6, 8, 9}</em> pandigital prime.  So that leaves 4 and 7 digit prime numbers less than 4321 and 7654321 respectively.  Since we want the largest pandigital prime we&#8217;ll check the 7 digit group first.</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, is_pandigital
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">7654321</span>, <span style="color: #ff4500;">1234567</span>, -<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> is_prime<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> is_pandigital<span style="color: black;">&#40;</span>n, <span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE41 = &quot;</span>, n
    <span style="color: #ff7700;font-weight:bold;">break</span></pre></div></div>

<h4><u>Comments</u></h4>
<ul>
<li>BTW, the largest 4 digit pandigital prime is 4231.  </li>
<li>More information on the <em>Euler</em> module can be found on the <a href="http://blog.dreamshire.com/2009/03/26/94/">tools page</a>.
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/03/26/project-euler-problem-41-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 48 Solution</title>
		<link>http://blog.dreamshire.com/2009/03/24/project-euler-problem-48-solution/</link>
		<comments>http://blog.dreamshire.com/2009/03/24/project-euler-problem-48-solution/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 23:23:44 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></category>
		<category><![CDATA[Large Integers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=3</guid>
		<description><![CDATA[Find the last ten digits of 1<sup>1</sup> + 2<sup>2</sup> + ... + 1000<sup>1000</sup>.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The series, 1<sup>1</sup> + 2<sup>2</sup> + 3<sup>3</sup> + &#8230; + 10<sup>10</sup> = 10405071317.</p>
<p>Find the last ten digits of the series, 1<sup>1</sup> + 2<sup>2</sup> + 3<sup>3</sup> + &#8230; + 1000<sup>1000</sup>.</p>
<h4><u>Analysis</u></h4>
<p>Without Python&#8217;s built-in pow() function and native large integer support this may have been more of a challenge.  In fact, before revisiting this problem we solved it in Perl with the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">$max_n</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$m</span> <span style="color: #339933;">=</span> 1e10<span style="color: #339933;">;</span>
<span style="color: #0000ff;">$sum</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$n</span> <span style="color: #009900;">&#40;</span>1<span style="color: #339933;">..</span><span style="color: #0000ff;">$max_n</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
	<span style="color: #0000ff;">$nx</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> 
	<span style="color: #0000ff;">$nx</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$nx</span> <span style="color: #339933;">*</span> <span style="color: #0000ff;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #0000ff;">$m</span>  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>1<span style="color: #339933;">..</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #0000ff;">$sum</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sum</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">$nx</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #0000ff;">$m</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;PE 48 = $sum&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">def PE48<span style="color: #009900;">&#40;</span>limit<span style="color: #339933;">,</span> <span style="color: #000066;">m</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
    sum <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>
    <span style="color: #b1b100;">while</span> limit<span style="color: #339933;">:</span>
        sum <span style="color: #339933;">+=</span> pow<span style="color: #009900;">&#40;</span>limit<span style="color: #339933;">,</span> limit<span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">*</span><span style="color: #0000ff;">*m</span><span style="color: #009900;">&#41;</span>
        limit <span style="color: #339933;">-=</span> <span style="color: #cc66cc;">1</span>
    <span style="color: #000066;">return</span> sum <span style="color: #339933;">%</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">*</span><span style="color: #0000ff;">*m</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE48 = &quot;</span><span style="color: #339933;">,</span>PE48<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p><i>Note: </i>(a*a*a) % n = ( (a%n) * (a%n) * (a%n) ) % n</p>
<ul>
<li>See also <a href="http://blog.dreamshire.com/2009/03/25/project-euler-problem-97-solution/">problem 97</a> &#038; <a href="http://blog.dreamshire.com/2009/04/26/project-euler-problem-188-solution/">problem 188</a>.</li>
<li>Don’t forget that leading zeros are not printed.</li>
<li>The last iteration, 1000, would not affect the last 10 digits of the sum, but was included to adhere to the problem description.</li>
<li>The full answer is <a href="http://www.betaprojects.com/pe/48/full_answer.txt" target="_blank">3001 digits</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/03/24/project-euler-problem-48-solution/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
