<?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>Elliott Reid</title>
	<atom:link href="http://elliottreid.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://elliottreid.com</link>
	<description>Portfolio of programming challenges, studies and samples.</description>
	<lastBuildDate>Wed, 04 May 2011 11:15:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Project Euler 25 (Python)</title>
		<link>http://elliottreid.com/project-euler-25-python/</link>
		<comments>http://elliottreid.com/project-euler-25-python/#comments</comments>
		<pubDate>Wed, 04 May 2011 11:15:25 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python Solutions]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://elliottreid.com/?p=299</guid>
		<description><![CDATA[Question 25 from Project Euler: The Fibonacci sequence is defined by the recurrence relation: Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be: F1 = 1 F2 = 1 F3 = 2 F4 = 3 F5 = 5 F6 = 8 F7 = 13 [...]]]></description>
			<content:encoded><![CDATA[<p>Question 25 from <a href="http://projecteuler.net" target="_blank" title="Project Euler">Project Euler</a>:</p>
<blockquote><p>
The Fibonacci sequence is defined by the recurrence relation: Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be:<br />
F1 = 1<br />
F2 = 1<br />
F3 = 2<br />
F4 = 3<br />
F5 = 5<br />
F6 = 8<br />
F7 = 13<br />
F8 = 21<br />
F9 = 34<br />
F10 = 55<br />
F11 = 89<br />
F12 = 144<br />
The 12th term, F12, is the first term to contain three digits.</p>
<p>What is the first term in the Fibonacci sequence to contain 1000 digits?
</p></blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
length = <span style="color: #ff4500;">0</span> <span style="color: #66cc66;">;</span> x = <span style="color: #ff4500;">1</span> <span style="color: #66cc66;">;</span> y = <span style="color: #ff4500;">2</span> <span style="color: #66cc66;">;</span> n = z = <span style="color: #ff4500;">3</span>
<span style="color: #808080; font-style: italic;">#y = previous term</span>
<span style="color: #808080; font-style: italic;">#x is the term before that</span>
<span style="color: #808080; font-style: italic;">#z = current term</span>
<span style="color: #808080; font-style: italic;">#use n to count terms</span>
<span style="color: #ff7700;font-weight:bold;">while</span> length <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">1000</span>:
    <span style="color: #808080; font-style: italic;">#create new term</span>
    <span style="color: #808080; font-style: italic;">#reassign old values</span>
    <span style="color: #808080; font-style: italic;">#set length</span>
    z = x+y
    x, y = y, z
    length = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>z<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    n += <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">print</span> n
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>-start</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://elliottreid.com/project-euler-25-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler 24 (Python)</title>
		<link>http://elliottreid.com/project-euler-24-python/</link>
		<comments>http://elliottreid.com/project-euler-24-python/#comments</comments>
		<pubDate>Mon, 02 May 2011 10:51:54 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python Solutions]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://elliottreid.com/?p=293</guid>
		<description><![CDATA[Question 24 from Project Euler: 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: 012 021 102 [...]]]></description>
			<content:encoded><![CDATA[<p>Question 24 from <a href="http://projecteuler.net" target="_blank" title="Project Euler">Project Euler</a>:</p>
<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: 012   021   102   120   201   210</p>
<p>What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
</p></blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>, <span style="color: #dc143c;">math</span>
start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> factorial<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: #ff4500;">0</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> n <span style="color: #66cc66;">*</span> factorial<span style="color: black;">&#40;</span>n-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
target = <span style="color: #ff4500;">1000000</span>
answer = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
digits = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
nDigits = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>digits<span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;">#the sorted permutation list will have 10! = 3628800 permutations</span>
<span style="color: #808080; font-style: italic;">#solve a digit at a time</span>
<span style="color: #808080; font-style: italic;">#narrowing the target and crossing off digits</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>digits<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">1</span>:
    x = factorial<span style="color: black;">&#40;</span>nDigits-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
    y = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: black;">ceil</span><span style="color: black;">&#40;</span>target/<span style="color: #008000;">float</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    answer.<span style="color: black;">append</span><span style="color: black;">&#40;</span>digits<span style="color: black;">&#91;</span>y-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    digits.<span style="color: black;">remove</span><span style="color: black;">&#40;</span>digits<span style="color: black;">&#91;</span>y-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    target -= x<span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>y-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
    nDigits -= <span style="color: #ff4500;">1</span>
<span style="color: #808080; font-style: italic;">#avoid being off by one digit</span>
answer.<span style="color: black;">append</span><span style="color: black;">&#40;</span>digits<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span>, answer<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>-start</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://elliottreid.com/project-euler-24-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler 23 (Python)</title>
		<link>http://elliottreid.com/project-euler-23-python/</link>
		<comments>http://elliottreid.com/project-euler-23-python/#comments</comments>
		<pubDate>Sun, 01 May 2011 22:19:14 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python Solutions]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://elliottreid.com/?p=289</guid>
		<description><![CDATA[Question 23 from Project Euler: A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect [...]]]></description>
			<content:encoded><![CDATA[<p>Question 23 from <a href="http://projecteuler.net" target="_blank" title="Project Euler">Project Euler</a>:</p>
<blockquote><p>
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.</p>
<p>A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.</p>
<p>As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.</p>
<p>Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
</p></blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> 
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> sumDivisors<span style="color: black;">&#40;</span>n, total=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#look for divisors with modulus</span>
    sqr = <span style="color: #008000;">int</span><span style="color: black;">&#40;</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;">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>, sqr<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>:
            total += i + n / i
    <span style="color: #ff7700;font-weight:bold;">if</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> == sqr:
        total -= sqr
    <span style="color: #ff7700;font-weight:bold;">return</span> total
&nbsp;
total = <span style="color: #ff4500;">0</span>
abundants = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">28123</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#use sumDivisors to populate abundants set</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> sumDivisors<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> n:
        abundants.<span style="color: black;">add</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;">#look in abundants for n - abundant</span>
    <span style="color: #808080; font-style: italic;">#if it's not present then n is not the sum of two abundants</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">any</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>n-a <span style="color: #ff7700;font-weight:bold;">in</span> abundants<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> a <span style="color: #ff7700;font-weight:bold;">in</span> abundants<span style="color: black;">&#41;</span>:
        total += n
<span style="color: #ff7700;font-weight:bold;">print</span> total
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>-start</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://elliottreid.com/project-euler-23-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler 22 (Python)</title>
		<link>http://elliottreid.com/project-euler-22-python/</link>
		<comments>http://elliottreid.com/project-euler-22-python/#comments</comments>
		<pubDate>Sun, 01 May 2011 14:38:05 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python Solutions]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://elliottreid.com/?p=258</guid>
		<description><![CDATA[Question 22 from Project Euler: Using names.txt, 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. For example, when the list is sorted into [...]]]></description>
			<content:encoded><![CDATA[<p>Question 22 from <a href="http://projecteuler.net" target="_blank" title="Project Euler">Project Euler</a>:</p>
<blockquote><p>
Using names.txt, 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, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 x 53 = 49714.</p>
<p>What is the total of all the name scores in the file?
</p></blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>, <span style="color: #dc143c;">string</span>
start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> 
&nbsp;
names = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;names.txt&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&quot;'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">','</span><span style="color: black;">&#41;</span>
names.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
letterScores=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'a'</span>:<span style="color: #ff4500;">1</span>, <span style="color: #483d8b;">'b'</span>:<span style="color: #ff4500;">2</span>, <span style="color: #483d8b;">'c'</span>:<span style="color: #ff4500;">3</span>, <span style="color: #483d8b;">'d'</span>:<span style="color: #ff4500;">4</span>, <span style="color: #483d8b;">'e'</span>:<span style="color: #ff4500;">5</span>, <span style="color: #483d8b;">'f'</span>:<span style="color: #ff4500;">6</span>, 
<span style="color: #483d8b;">'g'</span>:<span style="color: #ff4500;">7</span>, <span style="color: #483d8b;">'h'</span>:<span style="color: #ff4500;">8</span>, <span style="color: #483d8b;">'i'</span>:<span style="color: #ff4500;">9</span>, <span style="color: #483d8b;">'j'</span>:<span style="color: #ff4500;">10</span>, <span style="color: #483d8b;">'k'</span>:<span style="color: #ff4500;">11</span>, <span style="color: #483d8b;">'l'</span>:<span style="color: #ff4500;">12</span>, <span style="color: #483d8b;">'m'</span>:<span style="color: #ff4500;">13</span>, <span style="color: #483d8b;">'n'</span>:<span style="color: #ff4500;">14</span>, 
<span style="color: #483d8b;">'o'</span>:<span style="color: #ff4500;">15</span>, <span style="color: #483d8b;">'p'</span>:<span style="color: #ff4500;">16</span>, <span style="color: #483d8b;">'q'</span>:<span style="color: #ff4500;">17</span>, <span style="color: #483d8b;">'r'</span>:<span style="color: #ff4500;">18</span>, <span style="color: #483d8b;">'s'</span>:<span style="color: #ff4500;">19</span>, <span style="color: #483d8b;">'t'</span>:<span style="color: #ff4500;">20</span>, <span style="color: #483d8b;">'u'</span>:<span style="color: #ff4500;">21</span>, 
<span style="color: #483d8b;">'v'</span>:<span style="color: #ff4500;">22</span>, <span style="color: #483d8b;">'w'</span>:<span style="color: #ff4500;">23</span>, <span style="color: #483d8b;">'x'</span>:<span style="color: #ff4500;">24</span>, <span style="color: #483d8b;">'y'</span>:<span style="color: #ff4500;">25</span>, <span style="color: #483d8b;">'z'</span>:<span style="color: #ff4500;">26</span><span style="color: black;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#loop through names, nest to loop chars</span>
<span style="color: #808080; font-style: italic;">#use char and letterScores to tally up the total</span>
total = <span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">for</span> index, name <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>names<span style="color: black;">&#41;</span>:
    nameScore = <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> letter <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>name<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
        nameScore += letterScores<span style="color: black;">&#91;</span><span style="color: #dc143c;">string</span>.<span style="color: black;">lower</span><span style="color: black;">&#40;</span>name<span style="color: black;">&#91;</span>letter<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    total += nameScore <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span>index+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> total    
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>-start</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://elliottreid.com/project-euler-22-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler 21 (Python)</title>
		<link>http://elliottreid.com/project-euler-21-python/</link>
		<comments>http://elliottreid.com/project-euler-21-python/#comments</comments>
		<pubDate>Sun, 01 May 2011 12:59:32 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python Solutions]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://elliottreid.com/?p=252</guid>
		<description><![CDATA[Question 21 from Project Euler: Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a is not equal to b, then a and b are an amicable pair and each of a and b [...]]]></description>
			<content:encoded><![CDATA[<p>Question 21 from <a href="http://projecteuler.net" target="_blank" title="Project Euler">Project Euler</a>:</p>
<blockquote><p>
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a is not equal to b, 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 d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.</p>
<p>Evaluate the sum of all the amicable numbers under 10000.
</p></blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> 
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> sumDivisors<span style="color: black;">&#40;</span>n, total=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#look for divisors with modulus</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: #008000;">int</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>:
            total += i + n / i
    <span style="color: #ff7700;font-weight:bold;">return</span> total
&nbsp;
total = <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;">2</span>, <span style="color: #ff4500;">10000</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#test for amicables</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> sumDivisors<span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> i <span style="color: #ff7700;font-weight:bold;">and</span> sumDivisors<span style="color: black;">&#40;</span>sumDivisors<span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> == i:
        <span style="color: #808080; font-style: italic;">#tally up both numbers</span>
        total += sumDivisors<span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>+i
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> total
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>-start</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://elliottreid.com/project-euler-21-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler 20 (Python)</title>
		<link>http://elliottreid.com/project-euler-20-python/</link>
		<comments>http://elliottreid.com/project-euler-20-python/#comments</comments>
		<pubDate>Sun, 01 May 2011 10:43:37 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python Solutions]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://elliottreid.com/?p=247</guid>
		<description><![CDATA[Question 20 from Project Euler: n! means n x (n &#8211; 1) x &#8230; 3 x 2 x 1 For example, 10! = 10 x 9 x &#8230; 3 x 2 x 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 [...]]]></description>
			<content:encoded><![CDATA[<p>Question 20 from <a href="http://projecteuler.net" target="_blank" title="Project Euler">Project Euler</a>:</p>
<blockquote><p>
n! means n x (n &#8211; 1) x &#8230;  3 x 2 x 1</p>
<p>For example, 10! = 10 x 9 x &#8230;  3 x 2 x 1 = 3628800,<br />
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.</p>
<p>Find the sum of the digits in the number 100!
</p></blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#find the number</span>
n = <span style="color: #ff4500;">1</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: #ff4500;">100</span><span style="color: black;">&#41;</span>:
    n = n <span style="color: #66cc66;">*</span> i
&nbsp;
<span style="color: #808080; font-style: italic;">#convert to string</span>
<span style="color: #808080; font-style: italic;">#convert chars to ints and add them to total</span>
nStr = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>
answer = <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: #008000;">len</span><span style="color: black;">&#40;</span>nStr<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
    answer += <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>nStr<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> answer
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>-start</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://elliottreid.com/project-euler-20-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler 19 (Python)</title>
		<link>http://elliottreid.com/project-euler-19-python/</link>
		<comments>http://elliottreid.com/project-euler-19-python/#comments</comments>
		<pubDate>Sat, 30 Apr 2011 16:06:42 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python Solutions]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://elliottreid.com/?p=242</guid>
		<description><![CDATA[Question 19 from Project Euler: You are given the following information, but you may prefer to do some research for yourself. 1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. A [...]]]></description>
			<content:encoded><![CDATA[<p>Question 19 from <a href="http://projecteuler.net" target="_blank" title="Project Euler">Project Euler</a>:</p>
<blockquote><p>
You are given the following information, but you may prefer to do some research for yourself.</p>
<p>1 Jan 1900 was a Monday.<br />
Thirty days has September,<br />
April, June and November.<br />
All the rest have thirty-one,<br />
Saving February alone,<br />
Which has twenty-eight, rain or shine.<br />
And on leap years, twenty-nine.<br />
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.</p>
<p>How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
</p></blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>, <span style="color: #dc143c;">datetime</span>
start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#loop years and nest for months</span>
<span style="color: #808080; font-style: italic;">#get datetime object for first day</span>
<span style="color: #808080; font-style: italic;">#test for sundays</span>
total = <span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">for</span> year <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;">1901</span>, <span style="color: #ff4500;">2001</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> month <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">13</span><span style="color: black;">&#41;</span>:
        day = <span style="color: #dc143c;">datetime</span>.<span style="color: black;">date</span><span style="color: black;">&#40;</span>year, month, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> day.<span style="color: black;">weekday</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">6</span>:
            total += <span style="color: #ff4500;">1</span>    
<span style="color: #ff7700;font-weight:bold;">print</span> total 
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>-start</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://elliottreid.com/project-euler-19-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler 18 (Python)</title>
		<link>http://elliottreid.com/project-euler-18-python/</link>
		<comments>http://elliottreid.com/project-euler-18-python/#comments</comments>
		<pubDate>Sat, 30 Apr 2011 14:26:21 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python Solutions]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://elliottreid.com/?p=236</guid>
		<description><![CDATA[Question 18 from Project Euler: 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. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the [...]]]></description>
			<content:encoded><![CDATA[<p>Question 18 from <a href="http://projecteuler.net" target="_blank" title="Project Euler">Project Euler</a>:</p>
<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.<br />
3<br />
7 4<br />
2 4 6<br />
8 5 9 3</p>
<p>That is, 3 + 7 + 4 + 9 = 23.</p>
<p>Find the maximum total from top to bottom of the triangle below:<br />
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
</p></blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
triangle = <span style="color: black;">&#91;</span>
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">75</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">95</span>, <span style="color: #ff4500;">64</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">17</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">82</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">18</span>, <span style="color: #ff4500;">35</span>, <span style="color: #ff4500;">87</span>, <span style="color: #ff4500;">10</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">20</span>,  <span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">82</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">65</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">19</span>,  <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">23</span>, <span style="color: #ff4500;">75</span>,  <span style="color: #ff4500;">3</span>, <span style="color: #ff4500;">34</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">88</span>,  <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">77</span>, <span style="color: #ff4500;">73</span>,  <span style="color: #ff4500;">7</span>, <span style="color: #ff4500;">63</span>, <span style="color: #ff4500;">67</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">99</span>, <span style="color: #ff4500;">65</span>,  <span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">28</span>,  <span style="color: #ff4500;">6</span>, <span style="color: #ff4500;">16</span>, <span style="color: #ff4500;">70</span>, <span style="color: #ff4500;">92</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">41</span>, <span style="color: #ff4500;">41</span>, <span style="color: #ff4500;">26</span>, <span style="color: #ff4500;">56</span>, <span style="color: #ff4500;">83</span>, <span style="color: #ff4500;">40</span>, <span style="color: #ff4500;">80</span>, <span style="color: #ff4500;">70</span>, <span style="color: #ff4500;">33</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">41</span>, <span style="color: #ff4500;">48</span>, <span style="color: #ff4500;">72</span>, <span style="color: #ff4500;">33</span>, <span style="color: #ff4500;">47</span>, <span style="color: #ff4500;">32</span>, <span style="color: #ff4500;">37</span>, <span style="color: #ff4500;">16</span>, <span style="color: #ff4500;">94</span>, <span style="color: #ff4500;">29</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">53</span>, <span style="color: #ff4500;">71</span>, <span style="color: #ff4500;">44</span>, <span style="color: #ff4500;">65</span>, <span style="color: #ff4500;">25</span>, <span style="color: #ff4500;">43</span>, <span style="color: #ff4500;">91</span>, <span style="color: #ff4500;">52</span>, <span style="color: #ff4500;">97</span>, <span style="color: #ff4500;">51</span>, <span style="color: #ff4500;">14</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">70</span>, <span style="color: #ff4500;">11</span>, <span style="color: #ff4500;">33</span>, <span style="color: #ff4500;">28</span>, <span style="color: #ff4500;">77</span>, <span style="color: #ff4500;">73</span>, <span style="color: #ff4500;">17</span>, <span style="color: #ff4500;">78</span>, <span style="color: #ff4500;">39</span>, <span style="color: #ff4500;">68</span>, <span style="color: #ff4500;">17</span>, <span style="color: #ff4500;">57</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">91</span>, <span style="color: #ff4500;">71</span>, <span style="color: #ff4500;">52</span>, <span style="color: #ff4500;">38</span>, <span style="color: #ff4500;">17</span>, <span style="color: #ff4500;">14</span>, <span style="color: #ff4500;">91</span>, <span style="color: #ff4500;">43</span>, <span style="color: #ff4500;">58</span>, <span style="color: #ff4500;">50</span>, <span style="color: #ff4500;">27</span>, <span style="color: #ff4500;">29</span>, <span style="color: #ff4500;">48</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span><span style="color: #ff4500;">63</span>, <span style="color: #ff4500;">66</span>,  <span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">68</span>, <span style="color: #ff4500;">89</span>, <span style="color: #ff4500;">53</span>, <span style="color: #ff4500;">67</span>, <span style="color: #ff4500;">30</span>, <span style="color: #ff4500;">73</span>, <span style="color: #ff4500;">16</span>, <span style="color: #ff4500;">69</span>, <span style="color: #ff4500;">87</span>, <span style="color: #ff4500;">40</span>, <span style="color: #ff4500;">31</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span> <span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">62</span>, <span style="color: #ff4500;">98</span>, <span style="color: #ff4500;">27</span>, <span style="color: #ff4500;">23</span>,  <span style="color: #ff4500;">9</span>, <span style="color: #ff4500;">70</span>, <span style="color: #ff4500;">98</span>, <span style="color: #ff4500;">73</span>, <span style="color: #ff4500;">93</span>, <span style="color: #ff4500;">38</span>, <span style="color: #ff4500;">53</span>, <span style="color: #ff4500;">60</span>,  <span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">23</span>,<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#work up from the bottom row</span>
<span style="color: #ff7700;font-weight:bold;">for</span> row <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>triangle<span style="color: black;">&#41;</span>-<span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">0</span>, -<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> col <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;">#assign the number in the row above each adjacent pair</span>
        <span style="color: #808080; font-style: italic;">#to the greatest value of that pair</span>
        triangle<span style="color: black;">&#91;</span>row-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>col<span style="color: black;">&#93;</span> += <span style="color: #008000;">max</span><span style="color: black;">&#40;</span>triangle<span style="color: black;">&#91;</span>row<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>col<span style="color: black;">&#93;</span>, triangle<span style="color: black;">&#91;</span>row<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>col+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> triangle<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</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>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>-start</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://elliottreid.com/project-euler-18-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler 17 (Python)</title>
		<link>http://elliottreid.com/project-euler-17-python/</link>
		<comments>http://elliottreid.com/project-euler-17-python/#comments</comments>
		<pubDate>Sat, 30 Apr 2011 08:46:04 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python Solutions]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://elliottreid.com/?p=231</guid>
		<description><![CDATA[Question 17 from Project Euler: If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, [...]]]></description>
			<content:encoded><![CDATA[<p>Question 17 from <a href="http://projecteuler.net" target="_blank" title="Project Euler">Project Euler</a>:</p>
<blockquote><p>
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.</p>
<p>If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
</p></blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> num2txt<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: #ff4500;">1000</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'onethousand'</span><span style="color: black;">&#41;</span>
&nbsp;
    words = <span style="color: black;">&#123;</span><span style="color: #ff4500;">1</span>:<span style="color: #483d8b;">'one'</span>,<span style="color: #ff4500;">2</span>:<span style="color: #483d8b;">'two'</span>,<span style="color: #ff4500;">3</span>:<span style="color: #483d8b;">'three'</span>,<span style="color: #ff4500;">4</span>:<span style="color: #483d8b;">'four'</span>,<span style="color: #ff4500;">5</span>:<span style="color: #483d8b;">'five'</span>,<span style="color: #ff4500;">6</span>:<span style="color: #483d8b;">'six'</span>,<span style="color: #ff4500;">7</span>:<span style="color: #483d8b;">'seven'</span>,
    <span style="color: #ff4500;">8</span>:<span style="color: #483d8b;">'eight'</span>,<span style="color: #ff4500;">9</span>:<span style="color: #483d8b;">'nine'</span>,<span style="color: #ff4500;">10</span>:<span style="color: #483d8b;">'ten'</span>,<span style="color: #ff4500;">11</span>:<span style="color: #483d8b;">'eleven'</span>,<span style="color: #ff4500;">12</span>:<span style="color: #483d8b;">'twelve'</span>,<span style="color: #ff4500;">13</span>:<span style="color: #483d8b;">'thirteen'</span>,
    <span style="color: #ff4500;">14</span>:<span style="color: #483d8b;">'fourteen'</span>,<span style="color: #ff4500;">15</span>:<span style="color: #483d8b;">'fifteen'</span>,<span style="color: #ff4500;">16</span>:<span style="color: #483d8b;">'sixteen'</span>,<span style="color: #ff4500;">17</span>:<span style="color: #483d8b;">'seventeen'</span>,
    <span style="color: #ff4500;">18</span>:<span style="color: #483d8b;">'eighteen'</span>,<span style="color: #ff4500;">19</span>:<span style="color: #483d8b;">'nineteen'</span>,<span style="color: #ff4500;">20</span>:<span style="color: #483d8b;">'twenty'</span>,<span style="color: #ff4500;">30</span>:<span style="color: #483d8b;">'thirty'</span>,<span style="color: #ff4500;">40</span>:<span style="color: #483d8b;">'forty'</span>,
    <span style="color: #ff4500;">50</span>:<span style="color: #483d8b;">'fifty'</span>,<span style="color: #ff4500;">60</span>:<span style="color: #483d8b;">'sixty'</span>,<span style="color: #ff4500;">70</span>:<span style="color: #483d8b;">'seventy'</span>,<span style="color: #ff4500;">80</span>:<span style="color: #483d8b;">'eighty'</span>,<span style="color: #ff4500;">90</span>:<span style="color: #483d8b;">'ninety'</span>,
    <span style="color: #ff4500;">100</span>:<span style="color: #483d8b;">'hundred'</span>,<span style="color: #ff4500;">1000</span>:<span style="color: #483d8b;">'thousand'</span><span style="color: black;">&#125;</span>
&nbsp;
    total = <span style="color: #ff4500;">0</span>
    hundreds = n / <span style="color: #ff4500;">100</span> <span style="color: #808080; font-style: italic;">#digit from hundreds column</span>
    tens = n <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">100</span> / <span style="color: #ff4500;">10</span> <span style="color: #808080; font-style: italic;">#digit from tens column</span>
    ones = n <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">10</span> <span style="color: #808080; font-style: italic;">#digit from ones column</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> hundreds <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>: <span style="color: #808080; font-style: italic;">#3 digit number</span>
        total += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>words<span style="color: black;">&#91;</span>hundreds<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#digit as text (eg: one)</span>
        total += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>words<span style="color: black;">&#91;</span><span style="color: #ff4500;">100</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#hundred</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> tens <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span>: <span style="color: #808080; font-style: italic;">#two digit number &gt; 19</span>
            total += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>words<span style="color: black;">&#91;</span>tens<span style="color: #66cc66;">*</span><span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>+<span style="color: #ff4500;">3</span> <span style="color: #808080; font-style: italic;">#+3 accounts for &quot;and&quot;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> ones <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:
                total += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>words<span style="color: black;">&#91;</span>ones<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> tens == <span style="color: #ff4500;">1</span>: <span style="color: #808080; font-style: italic;">#teen or ten/eleven/twelve</span>
            total += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>words<span style="color: black;">&#91;</span>ones+<span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>+<span style="color: #ff4500;">3</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> ones <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>: <span style="color: #808080; font-style: italic;">#and digit (eg: &quot;andone&quot;)</span>
            total += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>words<span style="color: black;">&#91;</span>ones<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>+<span style="color: #ff4500;">3</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> tens <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>: <span style="color: #808080; font-style: italic;">#2 digit number</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> tens == <span style="color: #ff4500;">1</span>: <span style="color: #808080; font-style: italic;">#teen or ten/eleven/twelve</span>
            total += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>words<span style="color: black;">&#91;</span>ones+<span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> tens <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span>: <span style="color: #808080; font-style: italic;">#two digit number &gt; 19</span>
                total += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>words<span style="color: black;">&#91;</span>tens<span style="color: #66cc66;">*</span><span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> ones <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:
                    total += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>words<span style="color: black;">&#91;</span>ones<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>: <span style="color: #808080; font-style: italic;">#single digit number</span>
        total += <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>words<span style="color: black;">&#91;</span>ones<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> total
&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: #008000;">map</span><span style="color: black;">&#40;</span>num2txt, <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">1001</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>-start</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://elliottreid.com/project-euler-17-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler 16 (Python)</title>
		<link>http://elliottreid.com/project-euler-16-python/</link>
		<comments>http://elliottreid.com/project-euler-16-python/#comments</comments>
		<pubDate>Fri, 29 Apr 2011 10:30:37 +0000</pubDate>
		<dc:creator>Elliott</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python Solutions]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://elliottreid.com/?p=211</guid>
		<description><![CDATA[Question 16 from Project Euler: 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? 1 2 3 4 5 6 7 8 9 10 11 12 import time start = time.time&#40;&#41; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Question 16 from <a href="http://projecteuler.net" target="_blank" title="Project Euler">Project Euler</a>:</p>
<blockquote><p>
2<span style="vertical-align:super;">15</span> = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.</p>
<p>What is the sum of the digits of the number 2<span style="vertical-align:super;">1000</span>?
</p></blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#set value as string</span>
numStr = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">1000</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;">#loop by char, convert to int and add to total</span>
total = <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: #008000;">len</span><span style="color: black;">&#40;</span>numStr<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
    total += <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>numStr<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> total
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>-start</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://elliottreid.com/project-euler-16-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

