<?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 100-149</title>
	<atom:link href="http://blog.dreamshire.com/category/project-euler-solutions/project-euler-solutions-100-149/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 125 Solution</title>
		<link>http://blog.dreamshire.com/2009/07/04/project-euler-problem-125-solution-2/</link>
		<comments>http://blog.dreamshire.com/2009/07/04/project-euler-problem-125-solution-2/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 19:06:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[Palindromes]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1693</guid>
		<description><![CDATA[Finding square sums that are palindromic.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The palindromic number 595 is interesting because it can be written as the sum of consecutive squares: 6<sup>2</sup> + 7<sup>2</sup> + 8<sup>2</sup> + 9<sup>2</sup> + 10<sup>2</sup> + 11<sup>2</sup> + 12<sup>2</sup>.</p>
<p>There are exactly eleven palindromes below one-thousand that can be written as consecutive square sums, and the sum of these palindromes is 4164. Note that 1 = 0<sup>2</sup> + 1<sup>2</sup> has not been included as this problem is concerned with the squares of positive integers.</p>
<p>Find the sum of all the numbers less than 10<sup>8</sup> that are both palindromic and can be written as the sum of consecutive squares.</p>
<h4><u>Analysis</u></h4>
<p>To solve this problem requires a simple search to the square root of the limit (10<sup>8</sup>) or 5,000.  We use a set to eliminate duplicate sum-of-squares.</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_palindromic
&nbsp;
limit = <span style="color: #ff4500;">10</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">8</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>
pal = <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;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, sqrt_limit<span style="color: black;">&#41;</span>:
  sos = i<span style="color: #66cc66;">*</span>i
  <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>i+<span style="color: #ff4500;">1</span>, sqrt_limit<span style="color: black;">&#41;</span>:
    sos += j<span style="color: #66cc66;">*</span>j
    <span style="color: #ff7700;font-weight:bold;">if</span> sos<span style="color: #66cc66;">&gt;</span>=limit: <span style="color: #ff7700;font-weight:bold;">break</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> is_palindromic<span style="color: black;">&#40;</span>sos<span style="color: black;">&#41;</span>: pal.<span style="color: black;">add</span><span style="color: black;">&#40;</span>sos<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE125 = &quot;</span>, <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>pal<span style="color: black;">&#41;</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>pal<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>Takes 371,234 iterations.</li>
<li>The second number printed is the size of the set.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/07/04/project-euler-problem-125-solution-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 133 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/16/project-euler-problem-133-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/16/project-euler-problem-133-solution/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 07:49:39 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[Large Integers]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Repunits]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1654</guid>
		<description><![CDATA[Investigating which primes will never divide a repunit containing 10**n digits.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>A number consisting entirely of ones is called a repunit. We shall define R(<i>k</i>) to be a repunit of length <i>k</i>; for example, R(6) = 111111.</p>
<p>Let us consider repunits of the form R(10<sup><i>n</i></sup>).</p>
<p>Although R(10), R(100), or R(1000) are not divisible by 17, R(10000) is divisible by 17. Yet there is no value of <i>n</i> for which R(10<sup><i>n</i></sup>) will divide by 19. In fact, it is remarkable that 11, 17, 41, and 73 are only four primes below one-hundred that can ever be a factor of R(10<sup><i>n</i></sup>).</p>
<p>Find the sum of all the primes below one-hundred thousand that will never be a factor of R(10<sup><i>n</i></sup>).</p>
<h4><u>Analysis</u></h4>
<p>This is a modified solution from problem 132.  Really all we&#8217;re doing is finding the prime factors of a very large R(n) using the Python modulus power function.  We sum only those primes which are not factors.</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> is_prime
&nbsp;
limit, q, s = <span style="color: #ff4500;">100000</span>, <span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">50</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">5</span>
<span style="color: #ff7700;font-weight:bold;">for</span> p <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;">5</span>, limit, <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>p<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, q, p<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">1</span>: s += p
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE133 = &quot;</span>, s</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/16/project-euler-problem-133-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 132 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/15/project-euler-problem-132-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/15/project-euler-problem-132-solution/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 03:22:07 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Repunits]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1652</guid>
		<description><![CDATA[Determining the first forty prime factors of a very large repunit.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>A number consisting entirely of ones is called a repunit. We shall define R(<i>k</i>) to be a repunit of length <i>k</i>.</p>
<p>For example, R(10) = 1111111111 = 11&times;41&times;271&times;9091, and the sum of these prime factors is 9414.</p>
<p>Find the sum of the first forty prime factors of R(10<sup>9</sup>).</p>
<h4><u>Analysis</u></h4>
<p>Another quick solution using the modulus power function to find the prime factors of a large repunit, r(10<sup>9</sup>).</p>
<h4><u>Solution</u></h4>
<p>Runs < 2 seconds in Python.  Made some simple clarifications 3/17/10.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> Euler <span style="color: #ff7700;font-weight:bold;">import</span> is_prime
&nbsp;
s = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
n, q = <span style="color: #ff4500;">7</span>, <span style="color: #ff4500;">10</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">9</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">40</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> <span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, q, n<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span>: s.<span style="color: black;">add</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>
  n += <span style="color: #ff4500;">2</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE132 = &quot;</span>, <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</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>
<p>Here&#8217;s the first 40 prime factors: [11, 17, 41, 73, 101, 137, 251, 257, 271, 353, 401, 449, 641, 751, 1201, 1409, 1601, 3541, 4001, 4801, 5051, 9091, 10753, 15361, 16001, 19841, 21001, 21401, 24001, 25601, 27961, 37501, 40961, 43201, 60101, 62501, 69857, 76001, 76801, 160001]</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/06/15/project-euler-problem-132-solution/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 130 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/15/project-euler-problem-130-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/15/project-euler-problem-130-solution/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 03:20:40 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Repunits]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1650</guid>
		<description><![CDATA[Finding composite values, n, for which n−1 is divisible by the length of the smallest repunits that divide it.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>A number consisting entirely of ones is called a repunit. We shall define R(<i>k</i>) to be a repunit of length <i>k</i>; for example, R(6) = 111111.</p>
<p>Given that <i>n</i> is a positive integer and GCD(<i>n</i>, 10) = 1, it can be shown that there always exists a value, <i>k</i>, for which R(<i>k</i>) is divisible by <i>n</i>, and let A(<i>n</i>) be the least such value of <i>k</i>; for example, A(7) = 6 and A(41) = 5.</p>
<p>You are given that for all primes, <i>p</i> &gt; 5, that <i>p</i> minus 1 is divisible by A(<i>p</i>). For example, when <i>p</i> = 41, A(41) = 5, and 40 is divisible by 5.</p>
<p>However, there are rare composite values for which this is also true; the first five examples being 91, 259, 451, 481, and 703.</p>
<p>Find the sum of the first twenty-five composite values of <i>n</i> for which<br />GCD(<i>n</i>, 10) = 1 and <i>n</i> &minus; 1 is divisible by A(<i>n</i>).</p>
<h4><u>Analysis</u></h4>
<p>We started with the given information and added to it by simply checking which composite values of n-1 were divisible by A(n).  The problems 129, 130, 132 and 133 were all solved with recreation in mind so not a lot of thought transpired.  Compared to some solutions these do appear to be slower but simpler to understand.  </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> is_prime, gcd
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> A<span style="color: black;">&#40;</span>n<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;">or</span> gcd<span style="color: black;">&#40;</span>n, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">1</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
  x, k = <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> x <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">0</span>:
    x = <span style="color: black;">&#40;</span>x<span style="color: #66cc66;">*</span><span style="color: #ff4500;">10</span>+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> n
    k += <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> k
&nbsp;
s = <span style="color: black;">&#91;</span><span style="color: #ff4500;">91</span>, <span style="color: #ff4500;">259</span>, <span style="color: #ff4500;">451</span>, <span style="color: #ff4500;">481</span>, <span style="color: #ff4500;">703</span><span style="color: black;">&#93;</span>
n = s<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;">len</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span><span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">25</span>:
  n += <span style="color: #ff4500;">2</span>
  an = A<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> an <span style="color: #66cc66;">!</span>= <span style="color: #008000;">None</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: black;">&#40;</span>n-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> an == <span style="color: #ff4500;">0</span>: 
    s.<span style="color: black;">append</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE130 = &quot;</span>, <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</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/15/project-euler-problem-130-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 129 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/15/project-euler-problem-129-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/15/project-euler-problem-129-solution/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 03:18:57 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Repunits]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1648</guid>
		<description><![CDATA[Investigating minimal repunits that divide by n.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>A number consisting entirely of ones is called a repunit. We shall define R(<i>k</i>) to be a repunit of length <i>k</i>; for example, R(6) = 111111.</p>
<p>Given that <i>n</i> is a positive integer and GCD(<i>n</i>, 10) = 1, it can be shown that there always exists a value, <i>k</i>, for which R(<i>k</i>) is divisible by <i>n</i>, and let A(<i>n</i>) be the least such value of <i>k</i>; for example, A(7) = 6 and A(41) = 5.</p>
<p>The least value of <i>n</i> for which A(<i>n</i>) first exceeds ten is 17.</p>
<p>Find the least value of <i>n</i> for which A(<i>n</i>) first exceeds one-million.</p>
<h4><u>Analysis</u></h4>
<p>This was an obvious solution to the problem.  Still not sure about the relevance of repunits so we just took the recreational approach and created a set of solutions that are easy to follow.</p>
<h4><u>Solution</u></h4>
<p>Runs < 2 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> is_prime, gcd
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> A<span style="color: black;">&#40;</span>n<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;">or</span> gcd<span style="color: black;">&#40;</span>n, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">1</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
  x, k = <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> x <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">0</span>:
    x = <span style="color: black;">&#40;</span>x<span style="color: #66cc66;">*</span><span style="color: #ff4500;">10</span>+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> n
    k += <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> k
&nbsp;
limit = <span style="color: #ff4500;">1000001</span>
n = limit
<span style="color: #ff7700;font-weight:bold;">while</span> A<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><span style="color: #66cc66;">&lt;</span>limit: n += <span style="color: #ff4500;">2</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE129 = &quot;</span>, n</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/15/project-euler-problem-129-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 113 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/12/project-euler-problem-113-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/12/project-euler-problem-113-solution/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 21:06:37 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[Bouncy Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1638</guid>
		<description><![CDATA[How many numbers below a googol (10**100) are not "bouncy"?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, 134468.</p>
<p>Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, 66420.</p>
<p>We shall call a positive integer that is neither increasing nor decreasing a &quot;bouncy&quot; number; for example, 155349.</p>
<p>As <i>n</i> increases, the proportion of bouncy numbers below <i>n</i> increases such that there are only 12951 numbers below one-million that are not bouncy and only 277032 non-bouncy numbers below 10<sup>10</sup>.</p>
<p>How many numbers below a googol (10<sup>100</sup>) are not bouncy?</p>
<h4><u>Analysis</u></h4>
<p>Whenever a problem asks for the number of elements in a set, as does this one, then the solution in typically a counting problem solved with combinometrics.</p>
<p>Our solution uses the general formula that counts the monotonically increasing/decreasing digits ignoring leading zeros.</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> binomial
&nbsp;
n=<span style="color: #ff4500;">100</span>
<span style="color: #ff7700;font-weight:bold;">print</span> binomial<span style="color: black;">&#40;</span>n+<span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span> + binomial<span style="color: black;">&#40;</span>n+<span style="color: #ff4500;">9</span>,<span style="color: #ff4500;">9</span><span style="color: black;">&#41;</span> - <span style="color: #ff4500;">10</span><span style="color: #66cc66;">*</span>n - <span style="color: #ff4500;">2</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/12/project-euler-problem-113-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 112 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/11/project-euler-problem-112-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/11/project-euler-problem-112-solution/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 18:22:15 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[Bouncy Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1629</guid>
		<description><![CDATA[Investigating the density of "bouncy" numbers.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, 134468.</p>
<p>Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, 66420.</p>
<p>We shall call a positive integer that is neither increasing nor decreasing a &quot;bouncy&quot; number; for example, 155349.</p>
<p>Clearly there cannot be any bouncy numbers below one-hundred, but just over half of the numbers below one-thousand (525) are bouncy. In fact, the least number for which the proportion of bouncy numbers first reaches 50% is 538.</p>
<p>Surprisingly, bouncy numbers become more and more common and by the time we reach 21780 the proportion of bouncy numbers is equal to 90%.</p>
<p>Find the least number for which the proportion of bouncy numbers is exactly 99%.</p>
<h4><u>Analysis</u></h4>
<p>It was a surprise how long this program took to run.  The <em>is_bouncy()</em> routine seemed to slow things down.  We begin at the 90% point specified in the problem description and wait for the ratio to hit 99%.</p>
<h4><u>Solution</u></h4>
<p>Runs < 7 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;">def</span> is_bouncy<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  inc, dec, s = <span style="color: #008000;">False</span>, <span style="color: #008000;">False</span>, <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;">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: #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: #ff7700;font-weight:bold;">if</span> s<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;</span> s<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>: inc = <span style="color: #008000;">True</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> s<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span> s<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>: dec = <span style="color: #008000;">True</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> inc <span style="color: #ff7700;font-weight:bold;">and</span> dec: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
&nbsp;
n, p = <span style="color: #ff4500;">21780</span>, <span style="color: #ff4500;">0.90</span>
b = n <span style="color: #66cc66;">*</span> p
<span style="color: #ff7700;font-weight:bold;">while</span> p <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">0.99</span>:
  n += <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> is_bouncy<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>: b += <span style="color: #ff4500;">1</span>
  p = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>b<span style="color: black;">&#41;</span>/n
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE112 = &quot;</span>, n</pre></div></div>

<h4><u>Comments</u></h4>
<p>The <em>float()</em> function was required to convert to floating point.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/06/11/project-euler-problem-112-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 104 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/04/project-euler-problem-104-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/04/project-euler-problem-104-solution/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 22:53:47 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1521</guid>
		<description><![CDATA[Finding Fibonacci numbers for which the first and last nine digits are pandigital.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The Fibonacci sequence is defined by the recurrence relation:</p>
<p>F<sub><i>n</i></sub> = F<sub><i>n</i>&minus;1</sub> + F<sub><i>n</i>&minus;2</sub>, where F<sub>1</sub> = 1 and F<sub>2</sub> = 1.</p>
<p>It turns out that F<sub>541</sub>, which contains 113 digits, is the first Fibonacci number for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9, but not necessarily in order). And F<sub>2749</sub>, which contains 575 digits, is the first Fibonacci number for which the first nine digits are 1-9 pandigital.</p>
<p>Given that F<sub><i>k</i></sub> is the first Fibonacci number for which the first nine digits AND the last nine digits are 1-9 pandigital, find <i>k</i>.</p>
<h4><u>Analysis</u></h4>
<p>Even calculating the Fibonacci sequence without any optimization this solution was lightning fast.  Getting the bottom 9 digits was easy and has been done in previous problems.  The top 9 required the formula:</p>
<p>phi = (1 + sqrt(5)) / 2<br />
 t = n * log10(phi) + log10(1/sqrt(5))<br />
 t = int((pow(10, t &#8211; int(t) + 8)))</p>
<p>So, after finding a candidate with the bottom 9 digits pandigital we query the formula to see if the top 9 are pandigital.  The first one found will be our first solution.</p>
<h4><u>Solution</u></h4>
<p>Runs < 2 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> is_pandigital
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> top_digits<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
<span style="color: #808080; font-style: italic;"># t = n * log10(phi)          + log10(1/sqrt(5))</span>
  t = n <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">0.20898764024997873</span> - <span style="color: #ff4500;">0.34948500216800940</span>
  t = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, t - <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>t<span style="color: black;">&#41;</span> + <span style="color: #ff4500;">8</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> t
&nbsp;
fn, f0, f1 = <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff7700;font-weight:bold;">not</span> is_pandigital<span style="color: black;">&#40;</span>f1<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff7700;font-weight:bold;">not</span> is_pandigital<span style="color: black;">&#40;</span>top_digits<span style="color: black;">&#40;</span>fn<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
 f0, f1 = f1, <span style="color: black;">&#40;</span>f1+f0<span style="color: black;">&#41;</span><span style="color: #66cc66;">%</span>10<span style="color: #66cc66;">**</span><span style="color: #ff4500;">9</span>
 fn += <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE104 = &quot;</span>, fn</pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/06/04/project-euler-problem-104-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 135 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/27/project-euler-problem-135-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/27/project-euler-problem-135-solution/#comments</comments>
		<pubDate>Thu, 28 May 2009 03:12:05 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1411</guid>
		<description><![CDATA[Determining the number of solutions of the equation x**2 − y**2 − z**2 = n.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Given the positive integers, <i>x</i>, <i>y</i>, and <i>z</i>, are consecutive terms of an arithmetic progression, the least value of the positive integer, <i>n</i>, for which the equation, <i>x</i><sup>2</sup> &minus; <i>y</i><sup>2</sup> &minus; <i>z</i><sup>2</sup> = <i>n</i>, has exactly two solutions is <i>n</i> = 27:</p>
<p style='text-align:center;'>34<sup>2</sup> &minus; 27<sup>2</sup> &minus; 20<sup>2</sup> = 12<sup>2</sup> &minus; 9<sup>2</sup> &minus; 6<sup>2</sup> = 27</p>
<p>It turns out that <i>n</i> = 1155 is the least value which has exactly ten solutions.</p>
<p>How many values of <i>n</i> less than one million have exactly ten distinct solutions?</p>
<h4><u>Analysis</u></h4>
<p>Concept code.  More later.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> Euler <span style="color: #ff7700;font-weight:bold;">import</span> is_prime
&nbsp;
nmax = <span style="color: #ff4500;">55992</span>
s = <span style="color: #ff4500;">0</span>
inc=<span style="color: #ff4500;">3</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;">3</span>, nmax , <span style="color: #ff4500;">4</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>: s += inc
  <span style="color: #ff7700;font-weight:bold;">if</span> is_prime<span style="color: black;">&#40;</span>n+<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>: s += <span style="color: black;">&#40;</span>inc-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> n<span style="color: #66cc66;">&gt;</span>nmax//<span style="color: #ff4500;">16</span>: inc=<span style="color: #ff4500;">2</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> n<span style="color: #66cc66;">&gt;</span>nmax//<span style="color: #ff4500;">4</span>: inc=<span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE135 = &quot;</span>, s</pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/27/project-euler-problem-135-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 100 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/27/project-euler-problem-100-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/27/project-euler-problem-100-solution/#comments</comments>
		<pubDate>Thu, 28 May 2009 00:36:43 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1404</guid>
		<description><![CDATA[Finding the number of blue discs for which there is 50% chance of taking two blue.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>If a box contains twenty-one coloured discs, composed of fifteen blue discs and six red discs, and two discs were taken at random, it can be seen that the probability of taking two blue discs, P(BB) = (15/21)&times;(14/20) = 1/2.</p>
<p>The next such arrangement, for which there is exactly 50% chance of taking two blue discs at random, is a box containing eighty-five blue discs and thirty-five red discs.</p>
<p>By finding the first arrangement to contain over 10<sup>12</sup> = 1,000,000,000,000 discs in total, determine the number of blue discs that the box would contain.</p>
<h4><u>Analysis</u></h4>
<p>Given b=blue discs, n=total number of discs then the general equation is:<br />
(b/n) (b-1) / (n-1) = 1/2, which implies:<br />
(b<sup>2</sup>-b) / (n<sup>2</sup>-n) = 1/2, or<br />
(2b<sup>2</sup>-2b) / (n<sup>2</sup>-n) = 1, or<br />
2b<sup>2</sup>-2b = n<sup>2</sup>-n, or finally<br />
2b<sup>2</sup> &#8211; 2b &#8211; n<sup>2</sup> + n = 0 which is, in fact, a Diophantine Quadratic Equation and can be solved using a program provided on the following website: <a href="http://www.alpertron.com.ar/QUAD.HTM">http://www.alpertron.com.ar/QUAD.HTM</a></p>
<p><em>Here is the input to the program for our equation:</em><br />
[ 2] x<sup>2</sup> +<br />
[ 0] xy +<br />
[-1] y<sup>2</sup> +<br />
[-2] x +<br />
[ 1] y +<br />
[ 0] = 0  </p>
<p><em>Here is the output from the program:</em></p>
<p>2x<sup>2</sup> &#8211; y<sup>2</sup> &#8211; 2x + y = 0</p>
<p>X<sub>0</sub> = 0<br />
Y<sub>0</sub> = 1</p>
<p>and also:<br />
X<sub>0</sub> = 1<br />
Y<sub>0</sub> = 1</p>
<p>X<sub>n+1</sub> = P X<sub>n</sub> + Q Y<sub>n</sub> + K<br />
Y<sub>n+1</sub> = R X<sub>n</sub> + S Y<sub>n</sub> + L</p>
<p>P = 3<br />
Q = 2<br />
K = -2<br />
R = 4<br />
S = 3<br />
L = -3</p>
<p>From here we have two equations that can calculate <em>b</em> and <em>n</em> for each term in the series.  Instead of starting at <em>b</em>=1, <em>n</em>=1 we start with the values in the problem description to save a few iterations; <em>b</em>=85, <em>n</em>=120.  We continue this series until <em>n</em>>10<sup>12</sup>.</p>
<p>  The equations are: blue disks = 3b + 2n &#8211; 2; <em>n</em> disks = 4b + 3n &#8211; 3 (also <em>n</em> = int(b/&radic;2/2), but that&#8217;s another story). </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;">b = <span style="color: #ff4500;">85</span>
n = <span style="color: #ff4500;">120</span>
<span style="color: #ff7700;font-weight:bold;">while</span> n <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">10</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">12</span>:
  b,n = <span style="color: #ff4500;">3</span><span style="color: #66cc66;">*</span>b + <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>n - <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">4</span><span style="color: #66cc66;">*</span>b + <span style="color: #ff4500;">3</span><span style="color: #66cc66;">*</span>n - <span style="color: #ff4500;">3</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE100 = &quot;</span>,b</pre></div></div>

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