<?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; Palindromes</title>
	<atom:link href="http://blog.dreamshire.com/tag/palindromes/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 4 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/17/project-euler-problem-4-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/17/project-euler-problem-4-solution/#comments</comments>
		<pubDate>Mon, 18 May 2009 04:59:44 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 1-9]]></category>
		<category><![CDATA[Palindromes]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1260</guid>
		<description><![CDATA[Find the largest palindrome made from the product of two 3-digit numbers.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 &times; 99.</p>
<p>Find the largest palindrome made from the product of two 3-digit numbers.</p>
<h4><u>Analysis</u></h4>
<p>The most efficient way to find the largest palindrome product of two 3-digit numbers is by searching odd factors downwards from 999.  We test each product as a palindrome and save it.  Our search ends when the next product falls below the largest palindrome found.</p>
<p>For a product to qualify as a palindrome the first and last digit must be the same.  In this case it will always be 9, an odd number that can only be derived by the multiplication of two 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;">def</span> is_palindromic<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">return</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>n<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
p=<span style="color: #ff4500;">0</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: #ff4500;">999</span>, <span style="color: #ff4500;">101</span>, -<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>i, <span style="color: #ff4500;">101</span>, -<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> i<span style="color: #66cc66;">*</span>j <span style="color: #66cc66;">&lt;</span> p: <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>i<span style="color: #66cc66;">*</span>j<span style="color: black;">&#41;</span>: p = i<span style="color: #66cc66;">*</span>j
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE4 = &quot;</span>,p</pre></div></div>

<h4><u>Comments</u></h4>
<p>Search took 3,072 iterations.<br />
The largest palindrome for some other cases: 4 digits: 9999 &times; 9901 = 99000099, 5 digits: 99979 &times; 99681 = 9966006699, 6 digits: 999999 &times; 999001 = 999000000999, 7 digits: 9997647 &times; 9998017 = 99956644665999</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/17/project-euler-problem-4-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 55 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/10/project-euler-problem-55-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/10/project-euler-problem-55-solution/#comments</comments>
		<pubDate>Sat, 11 Apr 2009 05:56:42 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 50-59]]></category>
		<category><![CDATA[Palindromes]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=585</guid>
		<description><![CDATA[How many Lychrel numbers are there below ten-thousand?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.</p>
<p>Not all numbers produce palindromes so quickly. For example,</p>
<p style='margin-left:50px;'>349 + 943 = 1292,<br />
1292 + 2921 = 4213<br />
4213 + 3124 = 7337</p>
<p>That is, 349 took three iterations to arrive at a palindrome.</p>
<p>Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown to require over fifty iterations before producing a palindrome: 4668731596684224866951378664 (53 iterations, 28-digits).</p>
<p>Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.</p>
<p>How many Lychrel numbers are there below ten-thousand?</p>
<p class="info">NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.</p>
<h4><u>Analysis</u></h4>
<p>Our bounds are specified by the problem&#8217;s description; specifically to search a range from 10 to 9999 and only check a depth to 49 iterations.</p>
<p>The function <em>is_lychrel()</em>  takes the candidate and adds itself to its reverse.  If this sum is a palindrome then the it&#8217;s not a Lychrel number and we return a false result.  This process is repeated up to 49 times.</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;
<span style="color: #ff7700;font-weight:bold;">def</span> is_lychrel<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;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">50</span><span style="color: black;">&#41;</span>:
    n = n + <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> is_palindromic<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">for</span> n <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;">10</span>, <span style="color: #ff4500;">10000</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">if</span> is_lychrel<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><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>.<br />
for N<100,000 and up to 500 iterations produces 6091 Lychrel numbers</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/10/project-euler-problem-55-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
