<?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; Complete Problem</title>
	<atom:link href="http://blog.dreamshire.com/tag/complete-problem/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 23 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/09/project-euler-problem-23-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/09/project-euler-problem-23-solution/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 03:40:51 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 20-29]]></category>
		<category><![CDATA[Complete Problem]]></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=500</guid>
		<description><![CDATA[Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.</p>
<p>A number whose proper divisors are less than the number is called deficient and a number whose proper divisors exceed the number is called abundant.</p>
<p>As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.</p>
<p>Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.</p>
<h4><u>Analysis</u></h4>
<p>According to Wolfram Mathworld&#8217;s discussion on <a href="http://mathworld.wolfram.com/AbundantNumber.html">Abundant Numbers</a>, &#8220;Every number greater than 20161 can be expressed as a sum of two abundant numbers. &#8221;  So our upper bound is 20161 not 28123. </p>
<p>Using our routine from problem 21 to calculate the sum of proper divisors we create a set of abundant numbers on the fly and use Python&#8217;s set operations to make the necessary comparisons.  Any number that can&#8217;t be summed from two abundant numbers in the set are marked as exceptions and totaled.</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;">def</span> d<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  s = <span style="color: #ff4500;">1</span>
  t = n <span style="color: #66cc66;">**</span> .5
  <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;">2</span>, <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>t<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> n <span style="color: #66cc66;">%</span> i == <span style="color: #ff4500;">0</span>: s += i + n / i
  <span style="color: #ff7700;font-weight:bold;">if</span> t == <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>t<span style="color: black;">&#41;</span>: s -= t
  <span style="color: #ff7700;font-weight:bold;">return</span> s
&nbsp;
abn = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
s = <span style="color: #ff4500;">0</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;">1</span>, <span style="color: #ff4500;">20162</span> <span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> d<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> n:
    abn.<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;">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-a <span style="color: #ff7700;font-weight:bold;">in</span> abn<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> a <span style="color: #ff7700;font-weight:bold;">in</span> abn <span style="color: black;">&#41;</span>:
    s += n
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE23 = &quot;</span>, s</pre></div></div>

<h4><u>Comments</u></h4>
<p>OEIS reference to this sequence <a href="http://www.research.att.com/~njas/sequences/A048242">A048242</a>.<br />
This is one of those problems that you shouldn&#8217;t spend too much time on as once you have the answer the problems done; there&#8217;s no more discovery.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/09/project-euler-problem-23-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 35 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/08/project-euler-problem-35-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/08/project-euler-problem-35-solution/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 03:33:11 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 30-39]]></category>
		<category><![CDATA[Circular Primes]]></category>
		<category><![CDATA[Complete Problem]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=456</guid>
		<description><![CDATA[How many circular primes are there below one million?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.</p>
<p>There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.</p>
<p>How many circular primes are there below one million?</p>
<h4><u>Analysis</u></h4>
<p>We used our file of prime numbers and included those below 1 million and excluded those greater than 10 that contained the digits {0, 2, 4, 5, 6 or 8} because as a prime is rotated it cannot end with any of these digits and remain prime. </p>
<p>We read these numbers into a hash (associative array) using the prime numbers as keys.  The hash also serves as an &#8216;is prime?&#8217; function.  If a particular key exists then the number is prime and if it does not, then it&#8217;s composite.  The program loops through the hash&#8217;s keys and checks each rotation as a prime number.  When every rotation is prime it&#8217;s counted as a circular prime.</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: #000066;">open</span> <span style="color: #009900;">&#40;</span>IN<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;&lt;primes35.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;Error opening file: $!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%is_prime</span> <span style="color: #339933;">=</span> <span style="color: #000066;">map</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</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: #009900;">&#125;</span> <span style="color: #000066;">grep</span> <span style="color: #000066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</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: #339933;">!/</span><span style="color: #009900;">&#91;</span>024568<span style="color: #009900;">&#93;</span><span style="color: #339933;">/,</span> <span style="color: #009999;">&lt;IN&gt;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cp</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
P<span style="color: #339933;">:</span> <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$prime</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">keys</span> <span style="color: #0000ff;">%is_prime</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> 2<span style="color: #339933;">..</span><span style="color: #000066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$prime</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span> 
        rotate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$prime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #b1b100;">next</span> P <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$is_prime</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$prime</span><span style="color: #009900;">&#125;</span> 
     <span style="color: #009900;">&#125;</span>
     <span style="color: #0000ff;">$cp</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 PE35 = $cp&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<ul>
<li>Check out the tools post for an explanation of the <a href="http://blog.dreamshire.com/2009/03/26/94/#rotate"><em>rotate()</em></a> function</li>
<li>The next circular prime is a rep unit 19 digits long (R<sub>19</sub> = 1111111111111111111), so using this method for finding circular primes > 1,000,000 is impracticable</li>
<li>OEIS <a href="http://www.research.att.com/~njas/sequences/A016114">A016114</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/08/project-euler-problem-35-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
