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

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1610</guid>
		<description><![CDATA[By analysing a user's login attempts, can you determine the secret numeric passcode?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>A common security method used for online banking is to ask the user for three random characters from a passcode. For example, if the passcode was 531278, they may asked for the 2nd, 3rd, and 5th characters; the expected reply would be: 317.</p>
<p>The text file, <a href='http://projecteuler.net/project/keylog.txt'>keylog.txt</a>, contains fifty successful login attempts.</p>
<p>Given that the three characters are always asked for in order, analyse the file so as to determine the shortest possible secret passcode of unknown length.</p>
<h4><u>Analysis</u></h4>
<p>See Solution.</p>
<h4><u>Solution</u></h4>
<p>Runs < 3 minutes in Human mind.<br />
There are four question to answer before a solution can be determined.<br />
1. How many discrete digits are in the code?<br />
2. What are those digits?<br />
3. What order are those digits in?<br />
4. Do the digits repeat?</p>
<p>By analysing the file we can quickly answer 1 and 2:  There are eight digits in the code from the set {0,1,2,3,6,7,8,9}.  We know this because {4,5} are missing and in order to answer the question we would have to know that.<br />
Question 4 is answered by default because if they did repeat we would never know how long the code would be. So they don't!<br />
So the only real challenge is to answer question 3, which is the typical:<br />
If Mary is taller than Jan and Johnny is taller than Mary, but Pee-Wee, blah, blah, blah...<br />
So we don't really need a computer just a set of examples, for which we have plenty, to make our decision.</p>
<p>Given that the samples are in the order of the original 8 digit code we look at the most frequent last digit and maximize the number of digits that come before it:<br />
{1,2,3,6,7,8,9} come before 0.  0 is the last digit (samples: 680, 710, 290, 380).<br />
{1,2,3,6,7,8} come before 9.  9 is the second to the last digit (samples: 689, 729, 318).<br />
{1,2,3,6,7} come before 8.  8 is the third to the last number (samples: 168, 728, 318).<br />
{1,3,6,7} come before 2.  So, 2 is next. (samples: 762, 162, 362)<br />
{1,3,7} come before 6.  6 is next (samples: 316, 762).<br />
{3,7} come before 1.  1 is next (sample: 731)<br />
and 7 comes before 3. 7 is the first number (again, sample: 731)</p>
<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/06/10/project-euler-problem-79-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 75 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/08/project-euler-problem-75-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/08/project-euler-problem-75-solution/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 21:14:57 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 70-79]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Triangles]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1570</guid>
		<description><![CDATA[Find the number of different lengths of wire can that can form a right angle triangle in only one way.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>It turns out that 12 cm is the smallest length of wire that can be bent to form an integer sided right angle triangle in exactly one way, but there are many more examples.</p>
<p style='margin-left:50px;'><b>12 cm</b>: (3,4,5)<br />
<b>24 cm</b>: (6,8,10)<br />
<b>30 cm</b>: (5,12,13)<br />
<b>36 cm</b>: (9,12,15)<br />
<b>40 cm</b>: (8,15,17)<br />
<b>48 cm</b>: (12,16,20)</p>
<p>In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using 120 cm it is possible to form exactly three different integer sided right angle triangles.</p>
<p style='margin-left:50px;'><b>120 cm</b>: (30,40,50), (20,48,52), (24,45,51)</p>
<p>Given that L is the length of the wire, for how many values of L &le; 2,000,000 can exactly one integer sided right angle triangle be formed?</p>
<h4><u>Analysis</u></h4>
<p>This is an extension to the <a href="http://blog.dreamshire.com/2009/05/09/project-euler-problem-9-solution/">Problem 9</a> solution.  </p>
<h4><u>Solution</u></h4>
<p>Runs < 3 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> gcd
&nbsp;
limit = <span style="color: #ff4500;">2000000</span>
sqrt_limit = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>limit<span style="color: #66cc66;">**</span>.5<span style="color: black;">&#41;</span>
tx = <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>limit
<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>, sqrt_limit, <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;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, sqrt_limit, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> gcd<span style="color: black;">&#40;</span>i, j<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span>: 
      <span style="color: #008000;">sum</span> = <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> + <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>i<span style="color: #66cc66;">*</span>j + i<span style="color: #66cc66;">*</span>i + j<span style="color: #66cc66;">*</span>j
      <span style="color: #ff7700;font-weight:bold;">for</span> s <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">sum</span>, limit, <span style="color: #008000;">sum</span><span style="color: black;">&#41;</span>:
        tx<span style="color: black;">&#91;</span>s<span style="color: black;">&#93;</span>+=<span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE75 = &quot;</span>, tx.<span style="color: black;">count</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/06/08/project-euler-problem-75-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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 78 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/19/project-euler-problem-78-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/19/project-euler-problem-78-solution/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 21:27:49 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 70-79]]></category>
		<category><![CDATA[Large Integers]]></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=158</guid>
		<description><![CDATA[Investigating the number of ways in which coins can be separated into piles.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Let p(<em>n</em>) represent the number of different ways in which <em>n</em> coins can be separated into piles. For example, five coins can separated into piles in exactly seven different ways, so <em>p</em>(5)=7.<br />
<center><br />
OOOOO<br />
OOOO   O<br />
OOO   OO<br />
OOO   O   O<br />
OO   OO   O<br />
OO   O   O   O<br />
O   O   O   O   O<br />
</center><br />
Find the least value of <em>n</em> for which p(<em>n</em>) is evenly divisible by one million.</p>
<h4><u>Analysis</u></h4>
<p>This problem is really asking to find the first term in the sequence of integer partitions that&#8217;s divisible by 1,000,000.  </p>
<p>A partition of an integer, n, is one way of describing how many ways the sum of positive integers, &le; n, can be added together to equal n, regardless of order.  The function p(n) is used to denote the number of partitions for n.  Below we show our 5 &#8220;coins&#8221; as addends to evaluate 7 partitions, that is p(5)=7.<br />
<center><tt>5 = 5<br />
  = 4+1<br />
  = 3+2<br />
  = 3+1+1<br />
  = 2+2+1<br />
  = 2+1+1+1<br />
  = 1+1+1+1+1</tt></center><br />
We use a <a href="http://en.wikipedia.org/wiki/Integer_partition#Generating_function">generating function</a> to create the series until we find the required <em>n</em>.<br />
The generating function requires at most 500 so-called generalized pentagonal numbers, given by <em>n</em>(3<em>n</em> &#8211; 1)/2 with 0, &plusmn; 1, &plusmn; 2, &plusmn; 3&#8230;, the first few of which are 0, 1, 2, 5, 7, 12, 15, 22, 26, 35, &#8230; (Sloane&#8217;s <a href="http://www.research.att.com/~njas/sequences/A001318">A001318</a>). </p>
<p>We have the following generating function which uses our pentagonal numbers as exponents:<br />
<img src="http://mathworld.wolfram.com/images/equations/PartitionFunctionP/Inline40.gif" alt="generating function" /></p>
<h4><u>Solution</u></h4>
<p>Runs < 10 seconds in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$n</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$m</span><span style="color: #009900;">&#41;</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;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> 1e6<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>1<span style="color: #339933;">..</span>250<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>             <span style="color: #666666; font-style: italic;">#generate pentagonal numbers for generating function</span>
  <span style="color: #0000ff;">$p</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</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: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">push</span> <span style="color: #0000ff;">@k</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$n</span><span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>         <span style="color: #666666; font-style: italic;">#expand generating function to calculate p(n)</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$p</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">%</span>4<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">?</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #0000ff;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$n</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$k</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$k</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #0000ff;">$n</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #0000ff;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$p</span> <span style="color: #339933;">%</span> <span style="color: #0000ff;">$m</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 PE78 = &quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$n</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>The full answer is:<br />
363253009254357859308323315773967616467<br />
158361736338932270710864607092686080534<br />
895417314045435376684389911706807452721<br />
591544937406153858232021581676352762505<br />
545553421158554245989201590354130448112<br />
450821973350979535709118842524107301749<br />
07784762924663654000000</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/19/project-euler-problem-78-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 70 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/13/project-euler-problem-70-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/13/project-euler-problem-70-solution/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 22:57:46 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 70-79]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Totient]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=652</guid>
		<description><![CDATA[Investigate values of n for which φ(n) is a permutation of n.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Euler&#8217;s Totient function, &phi;(<var>n</var>) [sometimes called the phi function], is used to determine the number of positive numbers less than or equal to <var>n</var> which are relatively prime to <var>n</var>. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, &phi;(9)=6.<br />The number 1 is considered to be relatively prime to every positive number, so &phi;(1)=1. </p>
<p>Interestingly, &phi;(87109)=79180, and it can be seen that 87109 is a permutation of 79180.</p>
<p>Find the value of <var>n</var>, 1 &lt;<var>n</var> &lt; 10<sup>7</sup>, for which &phi;(<var>n</var>) is a permutation of <var>n</var> and the ratio <var>n</var>/&phi;(<var>n</var>) produces a minimum.</p>
<h4><u>Analysis</u></h4>
<p>We can reduce our search significantly by selecting prime pairs (<em>p</em><sub>1</sub>,  <em>p</em><sub>2</sub> ) and calculate <em>n</em> as <em>n</em> = <em>p</em><sub>1</sub> x <em>p</em><sub>2</sub>.  This allows the totient to be calculated as  <nobr>&phi;(<var>n</var>) = (<em>p</em><sub>1</sub>-1)(<em>p</em><sub>2</sub>-1)</nobr> for <em>n</em>.  Now, just find the minimum ratio <em>n/&phi;(n) </em> for those <em>n</em> and &phi;(<var>n</var>)  that are permutations of one another.</p>
<p>The range of primes was selected by taking the square root of the upper bound, 10,000,000, which is about 3162 and taking &plusmn;25% for a range of primes from 2000 to 4000 (247 primes).</p>
<h4><u>Solution</u></h4>
<p>Runs < 3 seconds in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">@p</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span> <span style="color: #009966; font-style: italic;">/s/</span><span style="color: #339933;">,</span><span style="color: #009999;">&lt;DATA&gt;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$min_q</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min_n</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$max</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span>_000_000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$p1</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@p</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$p2</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">++</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$#p</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #0000ff;">$n</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$p1</span> <span style="color: #339933;">*</span> <span style="color: #0000ff;">$p2</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">last</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$n</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000ff;">$max</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$phi</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p1</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: #009900;">&#40;</span><span style="color: #0000ff;">$p2</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$n</span> <span style="color: #339933;">/</span> <span style="color: #0000ff;">$phi</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$min_q</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min_n</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$q</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">if</span> permutation<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$phi</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">$min_q</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000ff;">$q</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 PE70 = $min_n&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> permutation <span style="color: #009900;">&#123;</span> <span style="color: #000066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #339933;">,</span> <span style="color: #000066;">sort</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span> <span style="color: #0000ff;">@_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">eq</span> <span style="color: #000066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #339933;">,</span> <span style="color: #000066;">sort</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span> <span style="color: #0000ff;">@_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">__DATA__</span>
<span style="color: #cc66cc;">2003</span> <span style="color: #cc66cc;">2011</span> <span style="color: #cc66cc;">2017</span> <span style="color: #cc66cc;">2027</span> <span style="color: #cc66cc;">2029</span> <span style="color: #cc66cc;">2039</span> <span style="color: #cc66cc;">2053</span> <span style="color: #cc66cc;">2063</span> <span style="color: #cc66cc;">2069</span> <span style="color: #cc66cc;">2081</span> <span style="color: #cc66cc;">2083</span> <span style="color: #cc66cc;">2087</span> <span style="color: #cc66cc;">2089</span> <span style="color: #339933;">...</span> <span style="color: #cc66cc;">3967</span> <span style="color: #cc66cc;">3989</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>The prime numbers from 2000 &#8211; 4000 were included at the end of the script and abbreviated here to save space.<br />
We tried the same method for <var>n</var>, 1 &lt;<var>n</var> &lt; 10<sup>9</sup>, using the square root 31623 and the 25% bounds of 23,700 through 39,500.  The result was 990326167 in about 50 seconds.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/13/project-euler-problem-70-solution/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 74 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/08/project-euler-problem-74-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/08/project-euler-problem-74-solution/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 21:12:14 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 70-79]]></category>
		<category><![CDATA[factorials]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=444</guid>
		<description><![CDATA[Determine the number of factorial chains that contain exactly sixty non-repeating terms.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The number 145 is well known for the property that the sum of the factorial of its digits is equal to 145:</p>
<p>1! + 4! + 5! = 1 + 24 + 120 = 145</p>
<p>Perhaps less well known is 169, in that it produces the longest chain of numbers that link back to 169; it turns out that there are only three such loops that exist:</p>
<p>169 &rarr; 363601 &rarr; 1454 &rarr; 169<br />
871 &rarr; 45361 &rarr; 871<br />
872 &rarr; 45362 &rarr; 872</p>
<p>It is not difficult to prove that EVERY starting number will eventually get stuck in a loop. For example,</p>
<p>69 &rarr; 363600 &rarr; 1454 &rarr; 169 &rarr; 363601 (&rarr; 1454)<br />
78 &rarr; 45360 &rarr; 871 &rarr; 45361 (&rarr; 871)<br />
540 &rarr; 145 (&rarr; 145)</p>
<p>Starting with 69 produces a chain of five non-repeating terms, but the longest non-repeating chain with a starting number below one million is sixty terms.</p>
<p>How many chains, with a starting number below one million, contain exactly sixty non-repeating terms?</p>
<h4><u>Analysis</u></h4>
<p>Knowing of a clever solution and debating the use of making one weighed in favor of utilizing some of the code from problem 34.  We wrote a recursive solution that was slow but intuitive (As the the saying goes, &#8216;In order to understand recursion, one must first understand recursion&#8217;).</p>
<p>By placing the factorials in an array saved having to recalculate them as we only needed the first 10.  Also, starting from 70 was a parameter inferred from the problem description. </p>
<p>We did want a solution that was somewhat extensible and able to answer other questions if the need ever arose.  Making changes to bounds or to the size of the series would be easy to change.</p>
<h4><u>Solution</u></h4>
<p>Runs < 55 seconds 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;">%sum</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@fact</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;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">24</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">120</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">720</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span>_040<span style="color: #339933;">,</span> <span style="color: #cc66cc;">40</span>_320<span style="color: #339933;">,</span> <span style="color: #cc66cc;">362</span>_880<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<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: #cc66cc;">70</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">999999</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: #0000ff;">%h</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$x60</span><span style="color: #339933;">++</span> <span style="color: #b1b100;">if</span> chains<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">60</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 PE74 = $x60&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> chains <span style="color: #009900;">&#123;</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: #000066;">return</span> <span style="color: #000066;">scalar</span> <span style="color: #000066;">keys</span> <span style="color: #0000ff;">%h</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$h</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$h</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">++;</span>
  <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$sum</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$sum</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">+=</span> <span style="color: #0000ff;">$fact</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">for</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span><span style="color: #0000ff;">$n</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000066;">return</span> chains<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sum</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/08/project-euler-problem-74-solution/feed/</wfw:commentRss>
		<slash:comments>1</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>
	</channel>
</rss>
