<?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; Prime Numbers</title>
	<atom:link href="http://blog.dreamshire.com/tag/prime-numbers/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 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 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 49 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/05/project-euler-problem-49-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/05/project-euler-problem-49-solution/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 09:52:09 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1467</guid>
		<description><![CDATA[Find arithmetic sequences, made of prime terms, whose four digits are permutations of each other.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.</p>
<p>There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.</p>
<p>What 12-digit number do you form by concatenating the three terms in this sequence?</p>
<h4><u>Analysis</u></h4>
<p>An obvious solution.  Start with the known solution plus 2 (1487+2) and find the next set checking only odd numbers.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> Euler <span style="color: #ff7700;font-weight:bold;">import</span> is_prime, is_perm
&nbsp;
n = <span style="color: #ff4500;">1489</span>  <span style="color: #808080; font-style: italic;"># must be odd</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
  b, c = n+<span style="color: #ff4500;">3330</span>, n+<span style="color: #ff4500;">6660</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> is_prime<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> is_prime<span style="color: black;">&#40;</span>b<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> is_prime<span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span> \
    <span style="color: #ff7700;font-weight:bold;">and</span> is_perm<span style="color: black;">&#40;</span>n,b<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> is_perm<span style="color: black;">&#40;</span>b,c<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">break</span>
  n += <span style="color: #ff4500;">2</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE49 = &quot;</span>, <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>b<span style="color: black;">&#41;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span></pre></div></div>

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

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1218</guid>
		<description><![CDATA[What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.</p>
<p style='margin-left:10px;'>9 = 7 + 2&times;1<sup>2</sup><br />
15 = 7 + 2&times;2<sup>2</sup><br />
21 = 3 + 2&times;3<sup>2</sup><br />
25 = 7 + 2&times;3<sup>2</sup><br />
27 = 19 + 2&times;2<sup>2</sup><br />
33 = 31 + 2&times;1<sup>2</sup></p>
<p>It turns out that the conjecture was false.</p>
<p>What is the smallest odd composite that cannot be written as the sum of a prime and twice a square</p>
<h4><u>Analysis</u></h4>
<p>Generate primes as needed and check the conjecture until a prime isn&#8217;t found.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">n = <span style="color: #ff4500;">5</span>
f = <span style="color: #ff4500;">1</span>
primes = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">all</span><span style="color: black;">&#40;</span> n <span style="color: #66cc66;">%</span> p <span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> primes <span style="color: black;">&#41;</span>:
    primes.<span style="color: black;">add</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">else</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">any</span><span style="color: black;">&#40;</span> <span style="color: black;">&#40;</span>n-<span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>i<span style="color: #66cc66;">*</span>i<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> primes <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, n<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">break</span>
  n += <span style="color: #ff4500;">3</span>-f
  f = -f
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE46 = &quot;</span>, n</pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/28/project-euler-problem-46-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 51 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/27/project-euler-problem-51-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/27/project-euler-problem-51-solution/#comments</comments>
		<pubDate>Wed, 27 May 2009 08:48:55 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 50-59]]></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=640</guid>
		<description><![CDATA[Find the smallest prime which, by changing the same part of the number, can form eight different primes.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>By replacing the 1<img src="" style="display:none;" alt="^(" /><sup>st</sup><img src="" style="display:none;" alt=")" /> digit of *57, it turns out that six of the possible values: 157, 257, 457, 557, 757, and 857, are all prime.</p>
<p>By replacing the 3<img src="" style="display:none;" alt="^(" /><sup>rd</sup><img src="" style="display:none;" alt=")" /> and 4<img src="" style="display:none;" alt="^(" /><sup>th</sup><img src="" style="display:none;" alt=")" /> digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.</p>
<p>Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.</p>
<h4><u>Analysis</u></h4>
<p>It looked obvious from the example that the minimum prime value for an 8 prime family had to have at least 6 digits.  For that size of prime it would take at least 3 digits to have to be replaced.  The last digit couldn&#8217;t be part of the replacement because that would make it non-prime.</p>
<p>So we took all 68,906 6-digit primes and reduced it to 11,066 by keeping only those primes that had 3 or more repeating digits.  We also remembered which digit was repeating so that replacing it would be easy.</p>
<p>All that was left was to iterate through the primes and report the first one that followed the instructions outlined above.</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> IN<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;&lt;primes51.txt&quot;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Cannot open file: $!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">%primes</span> <span style="color: #339933;">=</span> <span style="color: #000066;">map</span> <span style="color: #009900;">&#123;</span><span style="color: #000066;">chomp</span><span style="color: #339933;">;</span> <span style="color: #000066;">split</span> <span style="color: #339933;">/,/</span><span style="color: #009900;">&#125;</span> <span style="color: #009999;">&lt;IN&gt;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$p</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">sort</span> <span style="color: #000066;">keys</span> <span style="color: #0000ff;">%primes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$dd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$primes</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$p</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$d</span> <span style="color: #009900;">&#40;</span> <span style="color: #000066;">grep</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">$dd</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#40;</span>0<span style="color: #339933;">..</span>9<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">last</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$c</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$xx</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$p</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$xx</span><span style="color: #339933;">=~</span><span style="color: #000066;">s</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$dd</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$d</span><span style="color: #339933;">/</span>g<span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">++</span> <span style="color: #b1b100;">if</span> <span style="color: #000066;">exists</span> <span style="color: #0000ff;">$primes</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$xx</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$i</span><span style="color: #339933;">++;</span>
    <span style="color: #b1b100;">next</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE50 = $p&quot;</span><span style="color: #339933;">;</span>exit<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>Example of what file &#8216;primes51.txt&#8217; looks like:<br />
104999,9<br />
105211,1<br />
105557,5<br />
106121,1</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/27/project-euler-problem-51-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 12 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/19/project-euler-problem-12-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/19/project-euler-problem-12-solution/#comments</comments>
		<pubDate>Tue, 19 May 2009 08:54:10 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 10-19]]></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=1289</guid>
		<description><![CDATA[What is the value of the first triangle number to have over five hundred divisors?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The sequence of triangle numbers is generated by adding the natural numbers. So the 7<sup>th</sup> triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:</p>
<p style="text-align:center;">1, 3, 6, 10, 15, 21, 28, 36, 45, 55, &#8230;</p>
<p>Let us list the factors of the first seven triangle numbers:</p>
<blockquote style="font-family:courier new;"><p><b>&nbsp;1</b>: 1<br />
<b>&nbsp;3</b>: 1,3<br />
<b>&nbsp;6</b>: 1,2,3,6<br />
<b>10</b>: 1,2,5,10<br />
<b>15</b>: 1,3,5,15<br />
<b>21</b>: 1,3,7,21<br />
<b>28</b>: 1,2,4,7,14,28</p></blockquote>
<p>We can see that 28 is the first triangle number to have over five divisors.</p>
<p>What is the value of the first triangle number to have over five hundred divisors?</p>
<h4><u>Analysis</u></h4>
<p>Our algorithm for solving this one was improved nicely by a detailed explanation provided by a Project Euler moderator named &#8216;rayfil&#8217;.  His wonderful explanation is published as a PDF document that is available after you solve the problem.  What follows is our own interpretation written in Python.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> expx<span style="color: black;">&#40;</span>n,p<span style="color: black;">&#41;</span>:
  exp=<span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> n <span style="color: #66cc66;">%</span> p == <span style="color: #ff4500;">0</span>: 
    exp+=<span style="color: #ff4500;">1</span><span style="color: #66cc66;">;</span> 
    n = n/p
  <span style="color: #ff7700;font-weight:bold;">return</span> n,exp
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> prime_exp<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  dx = <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> primes: 
    <span style="color: #ff7700;font-weight:bold;">if</span> p<span style="color: #66cc66;">*</span>p <span style="color: #66cc66;">&gt;</span> n: <span style="color: #ff7700;font-weight:bold;">return</span> dx <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">2</span> 
    n,exp=expx<span style="color: black;">&#40;</span>n,p<span style="color: black;">&#41;</span>
    dx<span style="color: #66cc66;">*</span>= exp
    <span style="color: #ff7700;font-weight:bold;">if</span> n==<span style="color: #ff4500;">1</span>: <span style="color: #ff7700;font-weight:bold;">break</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> dx
&nbsp;
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: black;">&#93;</span>
nx=<span style="color: #ff4500;">3</span>
dx=<span style="color: #ff4500;">2</span> 
ndiv=<span style="color: #ff4500;">0</span>
max_div = <span style="color: #ff4500;">500</span>
<span style="color: #ff7700;font-weight:bold;">while</span> ndiv <span style="color: #66cc66;">&lt;</span>= max_div: 
  nx+= <span style="color: #ff4500;">1</span> 
  dxx = prime_exp<span style="color: black;">&#40;</span>nx/<span style="color: #ff4500;">2</span> <span style="color: #ff7700;font-weight:bold;">if</span> nx <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span> == <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">else</span> nx<span style="color: black;">&#41;</span>
  ndiv, dx = dx<span style="color: #66cc66;">*</span>dxx, dxx 
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE12 = &quot;</span>, nx<span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>nx-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>/<span style="color: #ff4500;">2</span></pre></div></div>

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

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1250</guid>
		<description><![CDATA[Find the largest prime factor of a composite number.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The prime factors of 13195 are 5, 7, 13 and 29.</p>
<p>What is the largest prime factor of the number 600851475143 ?</p>
<h4><u>Analysis</u></h4>
<p>The request to find the prime factors of a number comes up much too often.  To satisfy these requests we wrote a small javascript program that can be accessed here: <a href="http://blog.dreamshire.com/2009/04/30/prime-factor-calculator/">prime factor calculator</a>.  It can easily handle numbers up to 16 digits long in an instant.</p>
<p>The solution below is the essence of that script.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

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

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