<?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; Circular Primes</title>
	<atom:link href="http://blog.dreamshire.com/tag/circular-primes/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 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>
