<?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>Project Euler</title>
	<atom:link href="http://www.villainy.org/euler/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.villainy.org/euler</link>
	<description>Quick &#38; dirty solutions to the problems found at http://projecteuler.net/</description>
	<lastBuildDate>Fri, 06 Mar 2009 17:21:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Problem 22</title>
		<link>http://www.villainy.org/euler/177/map/problem-22/</link>
		<comments>http://www.villainy.org/euler/177/map/problem-22/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 17:21:02 +0000</pubDate>
		<dc:creator>Angela</dc:creator>
				<category><![CDATA[map]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[problem22]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[strings]]></category>
		<category><![CDATA[sum]]></category>

		<guid isPermaLink="false">http://www.villainy.org/euler/?p=177</guid>
		<description><![CDATA[What is the total of all the name scores in the file?]]></description>
			<content:encoded><![CDATA[<blockquote><p>
Using <code>names.txt</code>, a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.</p>
<p>For example, when the list is sorted into alphabetical order, <code>COLIN</code>, which is worth <code>3 + 15 + 12 + 9 + 14 = 53</code>, is the 938th name in the list. So, <code>COLIN</code> would obtain a score of <code>938 × 53 = 49714</code>.</p>
<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=22">What is the total of all the name scores in the file?</a>
</p></blockquote>
<p>I love abusing <code>map</code>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p177code1'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1771"><td class="code" id="p177code1"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> word_sum<span style="color: black;">&#40;</span>word<span style="color: black;">&#41;</span>:
  word_sum = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> letter: <span style="color: #008000;">ord</span><span style="color: black;">&#40;</span>letter<span style="color: black;">&#41;</span>-<span style="color: #008000;">ord</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'A'</span><span style="color: black;">&#41;</span>+<span style="color: #ff4500;">1</span>, word<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> word_sum
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> name_scores_sum<span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
  f = <span style="color: #008000;">file</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>
  names = f.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  names = <span style="color: #008000;">sorted</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">re</span>.<span style="color: black;">findall</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'([A-Z]+)'</span>,names<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  sums = <span style="color: #008000;">map</span><span style="color: black;">&#40;</span>word_sum,names<span style="color: black;">&#41;</span>
  scores = <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> <span style="color: black;">&#40;</span>index,<span style="color: #008000;">sum</span><span style="color: black;">&#41;</span>: index<span style="color: #66cc66;">*</span><span style="color: #008000;">sum</span>, <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>sums,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>scores<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span> name_scores_sum<span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>
<span style="color: #ff4500;">871198282</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.villainy.org/euler/177/map/problem-22/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Problem 21</title>
		<link>http://www.villainy.org/euler/170/amicable-numbers/problem-21/</link>
		<comments>http://www.villainy.org/euler/170/amicable-numbers/problem-21/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 23:59:12 +0000</pubDate>
		<dc:creator>Angela</dc:creator>
				<category><![CDATA[amicable numbers]]></category>
		<category><![CDATA[amicable]]></category>
		<category><![CDATA[divisors]]></category>
		<category><![CDATA[euler]]></category>
		<category><![CDATA[factors]]></category>
		<category><![CDATA[problem21]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.villainy.org/euler/?p=170</guid>
		<description><![CDATA[Evaluate the sum of all the amicable numbers under 10000.]]></description>
			<content:encoded><![CDATA[<blockquote><p>
Let <code>d(n)</code> be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).<br />
If <code>d(a) = b</code> and <code>d(b) = a</code>, where <code>a ≠ b</code>, then a and b are an amicable pair and each of a and b are called amicable numbers.</p>
<p>For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore <code>d(220) = 284</code>. The proper divisors of 284 are 1, 2, 4, 71 and 142; so <code>d(284) = 220</code>.</p>
<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=21">Evaluate the sum of all the amicable numbers under 10000.</a>
</p></blockquote>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p170code2'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1702"><td class="code" id="p170code2"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> proper_divisors<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  <span style="color: #483d8b;">&quot;&quot;&quot;
  Find all divisors of n by trial division
  &quot;&quot;&quot;</span>
  divisors = <span style="color: #008000;">set</span><span style="color: black;">&#40;</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;">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;">2</span>, <span style="color: #dc143c;">math</span>.<span style="color: black;">ceil</span><span style="color: black;">&#40;</span>n <span style="color: #66cc66;">**</span> <span style="color: #ff4500;">0.5</span><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>:
        divisors.<span style="color: black;">add</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>
        divisors.<span style="color: black;">add</span><span style="color: black;">&#40;</span>n/i<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> divisors
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> amicable_numbers<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  candidates = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,n<span style="color: black;">&#41;</span>
  amicables = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> a <span style="color: #ff7700;font-weight:bold;">in</span> candidates:
    b = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>proper_divisors<span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> a <span style="color: #66cc66;">!</span>= b \
        <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>proper_divisors<span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> == b \
        <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>proper_divisors<span style="color: black;">&#40;</span>b<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> == a:
      amicables.<span style="color: black;">append</span><span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span>
      amicables.<span style="color: black;">append</span><span style="color: black;">&#40;</span>b<span style="color: black;">&#41;</span>
      candidates.<span style="color: black;">remove</span><span style="color: black;">&#40;</span>b<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> amicables
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>amicable_numbers<span style="color: black;">&#40;</span><span style="color: #ff4500;">10000</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">31626</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.villainy.org/euler/170/amicable-numbers/problem-21/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Timing Decorator</title>
		<link>http://www.villainy.org/euler/158/python/timing-decorator/</link>
		<comments>http://www.villainy.org/euler/158/python/timing-decorator/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 20:45:34 +0000</pubDate>
		<dc:creator>Angela</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[decorator]]></category>
		<category><![CDATA[timing]]></category>
		<category><![CDATA[wrapper]]></category>

		<guid isPermaLink="false">http://www.villainy.org/euler/?p=158</guid>
		<description><![CDATA[I noticed I left some of the timing decorator <code>@wrapper</code> statements in, so here is the timing function I have been using to determine the approximate timing for a solution.]]></description>
			<content:encoded><![CDATA[<p>I noticed I left some of the timing decorator <code>@wrapper</code> statements in some solutions, so here is the timing function I have been using to determine the approximate solution time.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p158code3'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1583"><td class="code" id="p158code3"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> print_timing<span style="color: black;">&#40;</span>func<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">def</span> wrapper<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>arg<span style="color: black;">&#41;</span>:
    t1 = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    res = func<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>arg<span style="color: black;">&#41;</span>
    t2 = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'%s took %0.3f ms'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>func.<span style="color: black;">func_name</span>, <span style="color: black;">&#40;</span>t2-t1<span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">1000.0</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> res
  <span style="color: #ff7700;font-weight:bold;">return</span> wrapper</pre></td></tr></table></div>

<p>In use:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p158code4'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1584"><td class="code" id="p158code4"><pre class="python" style="font-family:monospace;">@print_timing
<span style="color: #ff7700;font-weight:bold;">def</span> some_function<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Hi, I'm a function. And I'm gonna get timed!&quot;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.villainy.org/euler/158/python/timing-decorator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problem 35</title>
		<link>http://www.villainy.org/euler/119/primes/problem-35/</link>
		<comments>http://www.villainy.org/euler/119/primes/problem-35/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 23:49:09 +0000</pubDate>
		<dc:creator>Angela</dc:creator>
				<category><![CDATA[primes]]></category>
		<category><![CDATA[circular primes]]></category>
		<category><![CDATA[problem35]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sets]]></category>
		<category><![CDATA[sieve of eratosthenes]]></category>

		<guid isPermaLink="false">http://www.villainy.org/euler/?p=119</guid>
		<description><![CDATA[How many circular primes are there below one million?]]></description>
			<content:encoded><![CDATA[<blockquote><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><a href="http://projecteuler.net/index.php?section=problems&#038;id=35">How many circular primes are there below one million?</a>
</p></blockquote>
<p>Using python sets, the sieve of Eratosthenes from <a href="http://www.villainy.org/euler/?p=53">problem 10</a> and some pre-filtering for primes with even digits.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p119code5'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1195"><td class="code" id="p119code5"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># sieve of Eratosthenes</span>
<span style="color: #ff7700;font-weight:bold;">def</span> sieve<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  primes = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>,n<span style="color: black;">&#41;</span>
  x = <span style="color: #ff4500;">0</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> primes<span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span> n<span style="color: #66cc66;">**</span><span style="color: #ff4500;">0.5</span>:
    <span style="color: #808080; font-style: italic;"># remove all multiples of the current prime from primes</span>
    primes = <span style="color: black;">&#91;</span>y <span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> primes <span style="color: #ff7700;font-weight:bold;">if</span> y==primes<span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">or</span> y<span style="color: #66cc66;">%</span>primes<span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
    x = x+<span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> primes
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> get_rotations<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
  strx = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
  rotations = <span style="color: black;">&#91;</span>strx<span style="color: black;">&#91;</span>i:<span style="color: black;">&#93;</span> + strx<span style="color: black;">&#91;</span>:i<span style="color: black;">&#93;</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>strx<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
  rotations = <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span>,rotations<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> rotations
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> all_digits_odd<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  n = <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> digit <span style="color: #ff7700;font-weight:bold;">in</span> n:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>digit<span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span> == <span style="color: #ff4500;">0</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;
@print_timing
<span style="color: #ff7700;font-weight:bold;">def</span> num_circular_primes<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
  primes = sieve<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
  <span style="color: #808080; font-style: italic;"># optimize: remove all primes with any even digits</span>
  primes = <span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>all_digits_odd,primes<span style="color: black;">&#41;</span>
  primes = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span>primes<span style="color: black;">&#41;</span>
&nbsp;
  cprimes = <span style="color: #ff4500;">0</span>
  max_prime = <span style="color: #008000;">max</span><span style="color: black;">&#40;</span>primes<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> primes:
    rotations = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span>get_rotations<span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    intersection = primes <span style="color: #66cc66;">&amp;</span> rotations
&nbsp;
    <span style="color: #808080; font-style: italic;"># if all rotations are primes, they are all circular</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>intersection<span style="color: black;">&#41;</span> == <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>rotations<span style="color: black;">&#41;</span>:
      cprimes += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>intersection<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#remove all the rotations from the working set</span>
    primes = primes - rotations
  <span style="color: #ff7700;font-weight:bold;">return</span> cprimes
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span> num_circular_primes<span style="color: black;">&#40;</span><span style="color: #ff4500;">1000000</span><span style="color: black;">&#41;</span>
num_circular_primes took <span style="color: #ff4500;">6531.000</span> ms
<span style="color: #ff4500;">54</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.villainy.org/euler/119/primes/problem-35/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problem 18, Problem 67</title>
		<link>http://www.villainy.org/euler/114/triangle/problem-18-problem-67/</link>
		<comments>http://www.villainy.org/euler/114/triangle/problem-18-problem-67/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 22:39:30 +0000</pubDate>
		<dc:creator>Angela</dc:creator>
				<category><![CDATA[triangle]]></category>
		<category><![CDATA[aggregator]]></category>
		<category><![CDATA[maximal sum]]></category>
		<category><![CDATA[problem 18]]></category>
		<category><![CDATA[problem 67]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.villainy.org/euler/?p=114</guid>
		<description><![CDATA[Find the maximum total from top to bottom of the triangle below:]]></description>
			<content:encoded><![CDATA[<blockquote><p>
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.</p>
<p align="center">
<code>3<br />
7 5<br />
2 4 6<br />
8 5 9 3</code>
</p>
<p>That is, 3 + 7 + 4 + 9 = 23.</p>
<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=67">Find the maximum total from top to bottom of the triangle below:</a></p>
<p align="center">
<code>75<br />
95 64<br />
17 47 82<br />
18 35 87 10<br />
20 04 82 47 65<br />
19 01 23 75 03 34<br />
88 02 77 73 07 63 67<br />
99 65 04 28 06 16 70 92<br />
41 41 26 56 83 40 80 70 33<br />
41 48 72 33 47 32 37 16 94 29<br />
53 71 44 65 25 43 91 52 97 51 14<br />
70 11 33 28 77 73 17 78 39 68 17 57<br />
91 71 52 38 17 14 91 43 58 50 27 29 48<br />
63 66 04 68 89 53 67 30 73 16 69 87 40 31<br />
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23</code>
</p>
<p>NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
</p></blockquote>
<p>Using the functional programming concept of an <i>aggregator</i> (with <code>reduce</code> and a custom aggregating function):</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code6'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1146"><td class="code" id="p114code6"><pre class="python" style="font-family:monospace;">triangle = \
<span style="color: #483d8b;">&quot;&quot;&quot;75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> get_row_maximal_sums<span style="color: black;">&#40;</span>prow,row<span style="color: black;">&#41;</span>:
  <span style="color: #483d8b;">&quot;&quot;&quot;
  Given the previous and current rows, find the sum of
  the maximal path leading to each node of the current row.
  &quot;&quot;&quot;</span>
  sums = <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> index, item <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">try</span>:
      left_pred = prow<span style="color: black;">&#91;</span>index-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IndexError</span>:
      <span style="color: #808080; font-style: italic;"># node has no left predecessor</span>
      left_pred = <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
      right_pred = prow<span style="color: black;">&#91;</span>index<span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IndexError</span>:
      <span style="color: #808080; font-style: italic;"># node has no right predecessor</span>
      right_pred = <span style="color: #ff4500;">0</span>
    sums<span style="color: black;">&#91;</span>index<span style="color: black;">&#93;</span> = item + <span style="color: #008000;">max</span><span style="color: black;">&#40;</span>left_pred,right_pred<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> sums
&nbsp;
<span style="color: #808080; font-style: italic;"># process triangle into list of row lists</span>
t = triangle.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i,row <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>t<span style="color: black;">&#41;</span>:
  t<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> = <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span>,row.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; &quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># problem 18</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #008000;">max</span><span style="color: black;">&#40;</span><span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span>get_row_maximal_sums,t<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">1074</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># problem 67</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #008000;">max</span><span style="color: black;">&#40;</span><span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span>get_row_maximal_sums,t<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">7273</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.villainy.org/euler/114/triangle/problem-18-problem-67/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problem 12</title>
		<link>http://www.villainy.org/euler/109/factors/problem-12/</link>
		<comments>http://www.villainy.org/euler/109/factors/problem-12/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 17:37:46 +0000</pubDate>
		<dc:creator>Angela</dc:creator>
				<category><![CDATA[factors]]></category>
		<category><![CDATA[divisors]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[problem12]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[triangle number]]></category>

		<guid isPermaLink="false">http://www.villainy.org/euler/?p=109</guid>
		<description><![CDATA[What is the value of the first triangle number to have over five hundred divisors?]]></description>
			<content:encoded><![CDATA[<blockquote><p>The sequence of triangle numbers is generated by adding the natural numbers. So the 7<sup>th</sup> triangle number would be <code>1 + 2 + 3 + 4 + 5 + 6 + 7 = 28</code>. The first ten terms would be:</p>
<p><code>1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...</code></p>
<p>Let us list the factors of the first seven triangle numbers:</p>
<p><code><br />
    1: 1<br />
     3: 1,3<br />
     6: 1,2,3,6<br />
    10: 1,2,5,10<br />
    15: 1,3,5,15<br />
    21: 1,3,7,21<br />
    28: 1,2,4,7,14,28<br />
</code></p>
<p>We can see that 28 is the first triangle number to have over five divisors.</p>
<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=12">What is the value of the first triangle number to have over five hundred divisors?</a></p></blockquote>
<p>Iterators again.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p109code7'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1097"><td class="code" id="p109code7"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">math</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> divisors<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  <span style="color: #483d8b;">&quot;&quot;&quot;
  Find all divisors of n by trial division
  &quot;&quot;&quot;</span>
  divisors = <span style="color: #008000;">set</span><span style="color: black;">&#40;</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;">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>, <span style="color: #dc143c;">math</span>.<span style="color: black;">ceil</span><span style="color: black;">&#40;</span>n <span style="color: #66cc66;">**</span> <span style="color: #ff4500;">0.5</span><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>:
        divisors.<span style="color: black;">add</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>
        divisors.<span style="color: black;">add</span><span style="color: black;">&#40;</span>n/i<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> divisors
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Triangle:
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #008000;">self</span>.<span style="color: black;">count</span> = <span style="color: #ff4500;">1</span>
    <span style="color: #008000;">self</span>.<span style="color: #008000;">sum</span> = <span style="color: #ff4500;">0</span>
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__iter__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>
  <span style="color: #ff7700;font-weight:bold;">def</span> next<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #008000;">self</span>.<span style="color: #008000;">sum</span> += <span style="color: #008000;">self</span>.<span style="color: black;">count</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">count</span> += <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: #008000;">sum</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> problem12<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  t = Triangle<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
    x = t.<span style="color: black;">next</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    num = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>divisors<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> num <span style="color: #66cc66;">&gt;</span> n:
      <span style="color: #ff7700;font-weight:bold;">return</span> x, num
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span> problem12<span style="color: black;">&#40;</span><span style="color: #ff4500;">500</span><span style="color: black;">&#41;</span>
problem12 took <span style="color: #ff4500;">6172.000</span> ms
<span style="color: black;">&#40;</span><span style="color: #ff4500;">76576500</span>, <span style="color: #ff4500;">576</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.villainy.org/euler/109/factors/problem-12/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problem 24</title>
		<link>http://www.villainy.org/euler/101/permutations/problem-24/</link>
		<comments>http://www.villainy.org/euler/101/permutations/problem-24/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 22:32:48 +0000</pubDate>
		<dc:creator>Angela</dc:creator>
				<category><![CDATA[permutations]]></category>
		<category><![CDATA[problem24]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.villainy.org/euler/?p=101</guid>
		<description><![CDATA[What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?]]></description>
			<content:encoded><![CDATA[<blockquote><p>A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:</p>
<p><code>012   021   102   120   201   210</code></p>
<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=24">What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?</a></p></blockquote>
<p>This one was <em>fun</em>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p101code8'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1018"><td class="code" id="p101code8"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">math</span> <span style="color: #ff7700;font-weight:bold;">import</span> factorial
@print_timing
<span style="color: #ff7700;font-weight:bold;">def</span> problem24<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  <span style="color: #483d8b;">&quot;&quot;&quot; 
  Determine the nth lexicographic 
  permutation of 0123456789 
  &quot;&quot;&quot;</span>
  answer = <span style="color: #483d8b;">&quot;&quot;</span>
  digits = <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">3</span>,<span style="color: #ff4500;">4</span>,<span style="color: #ff4500;">5</span>,<span style="color: #ff4500;">6</span>,<span style="color: #ff4500;">7</span>,<span style="color: #ff4500;">8</span>,<span style="color: #ff4500;">9</span><span style="color: black;">&#93;</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;">9</span>,-<span style="color: #ff4500;">1</span>,-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    ifact = factorial<span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>
    digit = <span style="color: black;">&#40;</span>n-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> / ifact
    answer += <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>digits.<span style="color: black;">pop</span><span style="color: black;">&#40;</span>digit<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    n = n - <span style="color: black;">&#40;</span>digit <span style="color: #66cc66;">*</span> ifact<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> answer
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span> problem24<span style="color: black;">&#40;</span><span style="color: #ff4500;">1000000</span><span style="color: black;">&#41;</span>
problem24 took <span style="color: #ff4500;">0.000</span> ms
<span style="color: #483d8b;">'2783915460'</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.villainy.org/euler/101/permutations/problem-24/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problem 36</title>
		<link>http://www.villainy.org/euler/96/palindrome/problem-36/</link>
		<comments>http://www.villainy.org/euler/96/palindrome/problem-36/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 18:32:33 +0000</pubDate>
		<dc:creator>Angela</dc:creator>
				<category><![CDATA[palindrome]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.villainy.org/euler/?p=96</guid>
		<description><![CDATA[Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.]]></description>
			<content:encoded><![CDATA[<blockquote><p>&#8220;The decimal number, 585 = 1001001001<sub>2</sub> (binary), is palindromic in both bases.</p>
<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=36">Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.</a></p>
<p>(Please note that the palindromic number, in either base, may not include leading zeros.)&#8221;</p></blockquote>
<p>Python just makes these too easy.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p96code9'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p969"><td class="code" id="p96code9"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> is_palindrome<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
  <span style="color: #483d8b;">&quot;&quot;&quot;
  Returns true if the digits of x are the same both backward
  and forward in both base 2 and base 10.
  &quot;&quot;&quot;</span>
  x10 = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
  x2 = bin<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> x10 == x10<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;">and</span> x2 == x2<span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># check only odd numbers since even base 2 numbers end in 0</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>is_palindrome, <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">1000000</span>,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">872187</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.villainy.org/euler/96/palindrome/problem-36/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problem 28</title>
		<link>http://www.villainy.org/euler/89/spiral/problem-28/</link>
		<comments>http://www.villainy.org/euler/89/spiral/problem-28/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 18:10:02 +0000</pubDate>
		<dc:creator>Angela</dc:creator>
				<category><![CDATA[spiral]]></category>
		<category><![CDATA[diagonals]]></category>
		<category><![CDATA[problem28]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.villainy.org/euler/?p=89</guid>
		<description><![CDATA[Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
21 22 23 24 25
20   07 08   09 10
19 06   01 02 11
18   05 04   03 12
17 16 15 14 13
It can be [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:</p>
<p><code><span style="color: #ff0000;">21</span> 22 23 24 <span style="color: #ff0000;">25</span><br />
20   <span style="color: #ff0000;">07</span> 08   <span style="color: #ff0000;">09</span> 10<br />
19 06   <span style="color: #ff0000;">01</span> 02 11<br />
18   <span style="color: #ff0000;">05</span> 04   <span style="color: #ff0000;">03</span> 12<br />
<span style="color: #ff0000;">17</span> 16 15 14 <span style="color: #ff0000;">13</span></code></p>
<p>It can be verified that the sum of both diagonals is 101.</p>
<p><a href="http://projecteuler.net/index.php?section=problems&amp;id=28">What is the sum of both diagonals in a 1001 by 1001 spiral formed in the same way?</a></p></blockquote>
<p>Observing that the four corners of the spiral can be expressed by:</p>
<blockquote><p><code>k<sup>2</sup>,<br />
k<sup>2</sup>-k+1,<br />
k<sup>2</sup>-2k+2,<br />
k<sup>2</sup>-3k+3,</p></blockquote>
<p>We get:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p89code11'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p8911"><td class="code" id="p89code11"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> spiralx<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  <span style="color: #008000;">sum</span> = <span style="color: #ff4500;">0</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> k <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>,n+<span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
    <span style="color: #008000;">sum</span> = <span style="color: #008000;">sum</span> + <span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span> <span style="color: #66cc66;">*</span> k<span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> - <span style="color: #ff4500;">6</span><span style="color: #66cc66;">*</span>k + <span style="color: #ff4500;">6</span>
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">sum</span> + <span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span> spiralx<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">101</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> spiralx<span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">25</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> spiralx<span style="color: black;">&#40;</span><span style="color: #ff4500;">1001</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">669171001</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.villainy.org/euler/89/spiral/problem-28/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problem 15</title>
		<link>http://www.villainy.org/euler/86/combinatorics/problem-15/</link>
		<comments>http://www.villainy.org/euler/86/combinatorics/problem-15/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 17:32:01 +0000</pubDate>
		<dc:creator>Angela</dc:creator>
				<category><![CDATA[combinatorics]]></category>
		<category><![CDATA[binomial coefficient]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[paths]]></category>
		<category><![CDATA[problem15]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.villainy.org/euler/?p=86</guid>
		<description><![CDATA[How many routes are there through a 20×20 grid?]]></description>
			<content:encoded><![CDATA[<blockquote><p>Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner.</p>
<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=15">How many routes are there through a 20×20 grid?</a></p></blockquote>
<p>Solved using combinatorics. <a href="http://www.joaoff.com/2008/01/20/a-square-grid-path-problem/">João Ferreira</a> has a good description of the problem and general solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.villainy.org/euler/86/combinatorics/problem-15/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
