<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>CodeWords</title>
	<atom:link href="http://codewords.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://codewords.wordpress.com</link>
	<description>Code, in words.</description>
	<lastBuildDate>Mon, 29 Aug 2011 19:25:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='codewords.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>CodeWords</title>
		<link>http://codewords.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://codewords.wordpress.com/osd.xml" title="CodeWords" />
	<atom:link rel='hub' href='http://codewords.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Project Euler Problem #12 Solution</title>
		<link>http://codewords.wordpress.com/2011/02/13/project-euler-problem-12-solution/</link>
		<comments>http://codewords.wordpress.com/2011/02/13/project-euler-problem-12-solution/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 03:14:41 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://codewords.wordpress.com/?p=610</guid>
		<description><![CDATA[By the use of a theorem and a (relatively) efficient factorization algorithm, we can solve this problem fairly quickly. First, the theorem: Theorem. If , then . This theorem tells us that if we can decompose into its prime factors, we can efficiently calculate the number of divisors of . It remains, therefore, for us [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=610&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>By the use of a theorem and a (relatively) efficient factorization algorithm, we can solve this problem fairly quickly. First, the theorem:</p>
<p><strong>Theorem.</strong> If <img src='http://s0.wp.com/latex.php?latex=n+%3D+p_1%5E%7B%5Calpha_1%7D+%5Ccdots+p_s%5E%7B%5Calpha_s%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n = p_1^{&#92;alpha_1} &#92;cdots p_s^{&#92;alpha_s}' title='n = p_1^{&#92;alpha_1} &#92;cdots p_s^{&#92;alpha_s}' class='latex' />, then</p>
<div align="center">
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle+d%28n%29+%3D+%28%5Calpha_1+%2B+1%29%28%5Calpha_2+%2B+1%29+%5Ccdots+%28%5Calpha_s+%2B+1%29+%3D+%5Cprod_%7Bi%3D1%7D%5Es+%28%5Calpha_i+%2B+1%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='&#92;displaystyle d(n) = (&#92;alpha_1 + 1)(&#92;alpha_2 + 1) &#92;cdots (&#92;alpha_s + 1) = &#92;prod_{i=1}^s (&#92;alpha_i + 1)' title='&#92;displaystyle d(n) = (&#92;alpha_1 + 1)(&#92;alpha_2 + 1) &#92;cdots (&#92;alpha_s + 1) = &#92;prod_{i=1}^s (&#92;alpha_i + 1)' class='latex' />.
</div>
<p></p>
<p>This theorem tells us that if we can decompose <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n' title='n' class='latex' /> into its prime factors, we can efficiently calculate the number of divisors of <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n' title='n' class='latex' />. It remains, therefore, for us to find a sufficiently efficient factorization algorithm. How about this:</p>
<p><pre class="brush: python;">
def prime_factorization(n):
	divisor = 2
	while n &gt; 1:
		factors = []
		while n % divisor == 0:
			factors.append(divisor)
			n /= divisor
		if factors:
			yield factors
		divisor += 1
</pre></p>
<p>This algorithm starts with a divisor of <code>2</code> and, if <code>n</code> is divisible by the divisor, it divides <code>n</code> by the divisor as long as <code>n</code> is still divisible by the divisor. When <code>n</code> no longer is, it increments the divisor and tries again until <code>n</code> is reduced to <code>1</code>. Furthermore, each of the prime factors of <code>n</code> that equal each other are grouped into subsequences. That is, for <code>n = 1701</code>, we get <code>[[3, 3, 3, 3, 3], [7]]</code>. This makes it easier to use the theorem above.</p>
<p>We use a generator function to yield the triangle numbers, and a divisor-counting function that makes use of the theorem above, to get the desired number. All of the code, then, together, is:</p>
<p><pre class="brush: python;">
def triangle_numbers():
	n = 1
	while True:
		yield n * (n + 1) / 2
		n += 1

def prime_factorization(n):
	candidate_factor = 2
	while n &gt; 1:
		factors = []
		while n % candidate_factor == 0:
			factors.append(candidate_factor)
			n /= candidate_factor
		if factors:
			yield factors
		candidate_factor += 1

def count_divisors(n):
	count = 1
	for tuple in prime_factorization(n):
		count *= (len(tuple) + 1) 
	return count

for t in triangle_numbers():
	if count_divisors(t) &gt; 500:
		print t
		break
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codewords.wordpress.com/610/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codewords.wordpress.com/610/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codewords.wordpress.com/610/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codewords.wordpress.com/610/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codewords.wordpress.com/610/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codewords.wordpress.com/610/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codewords.wordpress.com/610/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codewords.wordpress.com/610/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codewords.wordpress.com/610/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codewords.wordpress.com/610/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codewords.wordpress.com/610/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codewords.wordpress.com/610/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codewords.wordpress.com/610/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codewords.wordpress.com/610/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=610&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codewords.wordpress.com/2011/02/13/project-euler-problem-12-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ca3fb44ca21bbb9f642fe6257ce2d81f?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">raphil</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler Problem #14 Solution</title>
		<link>http://codewords.wordpress.com/2011/02/11/project-euler-problem-14-solution/</link>
		<comments>http://codewords.wordpress.com/2011/02/11/project-euler-problem-14-solution/#comments</comments>
		<pubDate>Sat, 12 Feb 2011 04:21:19 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://codewords.wordpress.com/?p=606</guid>
		<description><![CDATA[We calculate the length of each Collatz sequence with starting number and find the value of n for which the length is maximum. Note that we actually calculate one less than the length of each sequence, but since that subtraction is applied to each length, it doesn&#8217;t matter.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=606&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We calculate the length of each Collatz sequence with starting number <img src='http://s0.wp.com/latex.php?latex=n+%5Cin+%5B1%2C+10%5E6%29+%5Ccap+%5Cmathbb%7BN%7D&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n &#92;in [1, 10^6) &#92;cap &#92;mathbb{N}' title='n &#92;in [1, 10^6) &#92;cap &#92;mathbb{N}' class='latex' /> and find the value of <em>n</em> for which the length is maximum.</p>
<p>Note that we actually calculate one less than the length of each sequence, but since that subtraction is applied to each length, it doesn&#8217;t matter.</p>
<p><pre class="brush: python;">
def is_even(n):
	return n % 2 == 0

def collatz_length(n):
	length = 0
	while n &gt; 1:
		length += 1
		if is_even(n):
			n /= 2
		else:
			n = 3 * n + 1
	return length

max_n = 0
max_len = 0
for n in range(1, 1000000):
	len = collatz_length(n)
	if len &gt; max_len:
		max_len = len
		max_n = n

print max_n
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codewords.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codewords.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codewords.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codewords.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codewords.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codewords.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codewords.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codewords.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codewords.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codewords.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codewords.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codewords.wordpress.com/606/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codewords.wordpress.com/606/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codewords.wordpress.com/606/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=606&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codewords.wordpress.com/2011/02/11/project-euler-problem-14-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ca3fb44ca21bbb9f642fe6257ce2d81f?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">raphil</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler Problem #24 Solution</title>
		<link>http://codewords.wordpress.com/2011/02/08/project-euler-problem-24-solution/</link>
		<comments>http://codewords.wordpress.com/2011/02/08/project-euler-problem-24-solution/#comments</comments>
		<pubDate>Wed, 09 Feb 2011 02:56:52 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://codewords.wordpress.com/?p=601</guid>
		<description><![CDATA[This code gets the millionth lexicographic permutation of (0, 1, 2, 3, 4, 5, 6, 7, 8, 9):<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=601&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This code gets the millionth lexicographic permutation of (0, 1, 2, 3, 4, 5, 6, 7, 8, 9):</p>
<p><pre class="brush: python;">
from itertools import permutations

MILLIONTH = 10 ** 6 - 1

print reduce(lambda x, y: str(x) + str(y), list(permutations(range(10), 10))[MILLIONTH])
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codewords.wordpress.com/601/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codewords.wordpress.com/601/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codewords.wordpress.com/601/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codewords.wordpress.com/601/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codewords.wordpress.com/601/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codewords.wordpress.com/601/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codewords.wordpress.com/601/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codewords.wordpress.com/601/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codewords.wordpress.com/601/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codewords.wordpress.com/601/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codewords.wordpress.com/601/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codewords.wordpress.com/601/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codewords.wordpress.com/601/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codewords.wordpress.com/601/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=601&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codewords.wordpress.com/2011/02/08/project-euler-problem-24-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ca3fb44ca21bbb9f642fe6257ce2d81f?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">raphil</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler Problem #29 Solution</title>
		<link>http://codewords.wordpress.com/2011/02/07/project-euler-problem-29-solution/</link>
		<comments>http://codewords.wordpress.com/2011/02/07/project-euler-problem-29-solution/#comments</comments>
		<pubDate>Tue, 08 Feb 2011 03:39:52 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://codewords.wordpress.com/?p=590</guid>
		<description><![CDATA[Pretty self-explanatory: This code takes advantage of the fact that the set object silently rejects duplicates when an attempt is made to add one.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=590&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Pretty self-explanatory:</p>
<p><pre class="brush: python;">
import itertools

products = set()

for pair in itertools.product(range(2, 101), range(2, 101)):
	products.add(pair[0] ** pair[1])

print len(products)
</pre></p>
<p>This code takes advantage of the fact that the <code>set</code> object silently rejects duplicates when an attempt is made to add one.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codewords.wordpress.com/590/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codewords.wordpress.com/590/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codewords.wordpress.com/590/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codewords.wordpress.com/590/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codewords.wordpress.com/590/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codewords.wordpress.com/590/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codewords.wordpress.com/590/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codewords.wordpress.com/590/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codewords.wordpress.com/590/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codewords.wordpress.com/590/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codewords.wordpress.com/590/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codewords.wordpress.com/590/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codewords.wordpress.com/590/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codewords.wordpress.com/590/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=590&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codewords.wordpress.com/2011/02/07/project-euler-problem-29-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ca3fb44ca21bbb9f642fe6257ce2d81f?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">raphil</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler Problem #56 Solution</title>
		<link>http://codewords.wordpress.com/2011/02/06/project-euler-problem-56-solution/</link>
		<comments>http://codewords.wordpress.com/2011/02/06/project-euler-problem-56-solution/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 22:30:48 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://codewords.wordpress.com/?p=565</guid>
		<description><![CDATA[We effectively search a 100 &#215; 100 grid and find the cell with the greatest digital sum. Using some functional magic, we can do this:<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=565&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We effectively search a 100 &times; 100 grid and find the cell with the greatest digital sum. Using some functional magic, we can do this:</p>
<p><pre class="brush: python;">
from itertools import *

pairs = product(range(1, 100), range(1, 100))
exponentials = imap(lambda pair: pair[0] ** pair[1], pairs)
strings = imap(lambda exponential: str(exponential), exponentials)
sums = imap(lambda string: sum(map(lambda digit: int(digit), string)), strings)

print max(sums)
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codewords.wordpress.com/565/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codewords.wordpress.com/565/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codewords.wordpress.com/565/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codewords.wordpress.com/565/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codewords.wordpress.com/565/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codewords.wordpress.com/565/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codewords.wordpress.com/565/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codewords.wordpress.com/565/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codewords.wordpress.com/565/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codewords.wordpress.com/565/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codewords.wordpress.com/565/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codewords.wordpress.com/565/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codewords.wordpress.com/565/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codewords.wordpress.com/565/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=565&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codewords.wordpress.com/2011/02/06/project-euler-problem-56-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ca3fb44ca21bbb9f642fe6257ce2d81f?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">raphil</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler Problem #28 Solution</title>
		<link>http://codewords.wordpress.com/2011/02/06/project-euler-problem-28-solution/</link>
		<comments>http://codewords.wordpress.com/2011/02/06/project-euler-problem-28-solution/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 22:30:46 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://codewords.wordpress.com/?p=563</guid>
		<description><![CDATA[This algorithm calculates the sum of the diagonal numbers in a 1001 &#215; 1001 spiral:<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=563&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This algorithm calculates the sum of the diagonal numbers in a 1001 &times; 1001 spiral:</p>
<p><pre class="brush: python;">
current = 0
step = 2

sum = 0
limit = 1001 ** 2

spiral = range(1, limit + 1)

while current &lt; limit:
	for i in range(4):
		if current &gt; limit:
			break
		sum += spiral[current]
		current += step
	step += 2

print sum
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codewords.wordpress.com/563/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codewords.wordpress.com/563/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codewords.wordpress.com/563/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codewords.wordpress.com/563/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codewords.wordpress.com/563/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codewords.wordpress.com/563/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codewords.wordpress.com/563/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codewords.wordpress.com/563/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codewords.wordpress.com/563/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codewords.wordpress.com/563/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codewords.wordpress.com/563/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codewords.wordpress.com/563/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codewords.wordpress.com/563/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codewords.wordpress.com/563/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=563&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codewords.wordpress.com/2011/02/06/project-euler-problem-28-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ca3fb44ca21bbb9f642fe6257ce2d81f?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">raphil</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler Problem #40 Solution</title>
		<link>http://codewords.wordpress.com/2011/02/06/project-euler-problem-40-solution/</link>
		<comments>http://codewords.wordpress.com/2011/02/06/project-euler-problem-40-solution/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 22:30:27 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://codewords.wordpress.com/?p=557</guid>
		<description><![CDATA[Simple:<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=557&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Simple:</p>
<p><pre class="brush: python;">
digits = &quot;&quot;

for n in range(1, 1000001):
	digits += str(n)

product = 1
for i in range(7):
	product *= int(digits[10 ** i - 1])

print product
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codewords.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codewords.wordpress.com/557/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codewords.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codewords.wordpress.com/557/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codewords.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codewords.wordpress.com/557/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codewords.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codewords.wordpress.com/557/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codewords.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codewords.wordpress.com/557/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codewords.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codewords.wordpress.com/557/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codewords.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codewords.wordpress.com/557/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=557&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codewords.wordpress.com/2011/02/06/project-euler-problem-40-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ca3fb44ca21bbb9f642fe6257ce2d81f?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">raphil</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler Problem #53 Solution</title>
		<link>http://codewords.wordpress.com/2011/02/06/project-euler-problem-53-solution/</link>
		<comments>http://codewords.wordpress.com/2011/02/06/project-euler-problem-53-solution/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 22:30:01 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://codewords.wordpress.com/?p=539</guid>
		<description><![CDATA[Simply iterate over each where and and count which ones are greater than 1,000,000. In Python:<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=539&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Simply iterate over each <img src='http://s0.wp.com/latex.php?latex=_nC_r&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='_nC_r' title='_nC_r' class='latex' /> where <img src='http://s0.wp.com/latex.php?latex=1+%5Cle+n+%5Cle+100&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='1 &#92;le n &#92;le 100' title='1 &#92;le n &#92;le 100' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=1+%5Cle+r+%5Cle+n&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='1 &#92;le r &#92;le n' title='1 &#92;le r &#92;le n' class='latex' /> and count which ones are greater than 1,000,000. In Python:</p>
<p><pre class="brush: python;">
import operator

def factorial(n):
	return 1 if n == 0 else reduce(operator.mul, range(1, n + 1))

def combo(n, r):
	return factorial(n) / (factorial(r) * factorial(n - r))

count = 0

for n in range(1, 101):
	for r in range(1, n):
		if combo(n, r) &gt; 1000000:
			count += 1

print count
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codewords.wordpress.com/539/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codewords.wordpress.com/539/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codewords.wordpress.com/539/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codewords.wordpress.com/539/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codewords.wordpress.com/539/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codewords.wordpress.com/539/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codewords.wordpress.com/539/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codewords.wordpress.com/539/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codewords.wordpress.com/539/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codewords.wordpress.com/539/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codewords.wordpress.com/539/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codewords.wordpress.com/539/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codewords.wordpress.com/539/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codewords.wordpress.com/539/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=539&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codewords.wordpress.com/2011/02/06/project-euler-problem-53-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ca3fb44ca21bbb9f642fe6257ce2d81f?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">raphil</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler Problem #42 Solution</title>
		<link>http://codewords.wordpress.com/2011/02/05/project-euler-problem-42-solution/</link>
		<comments>http://codewords.wordpress.com/2011/02/05/project-euler-problem-42-solution/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 02:58:10 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://codewords.wordpress.com/?p=552</guid>
		<description><![CDATA[Read the file of words, calculate their scores, and see which are triangular numbers. Note the use of an iterator function on line 4:<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=552&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Read the file of words, calculate their scores, and see which are triangular numbers. Note the use of an iterator function on line 4:</p>
<p><pre class="brush: python; highlight: [4];">
CAPITAL_LETTER_A = 'A'
COMMA = &quot;,&quot;

def triangular_numbers():
	n = 1
	while True:
		yield n * (n + 1) / 2
		n += 1

def score_char(char):
	return ord(char) - ord(CAPITAL_LETTER_A) + 1

def score(word):
	return sum(map(score_char, word))

def is_triangular(num):
	for n in triangular_numbers():
		if n &gt; num:
			return False;
		else:
			if n == num:
				return True
	return False

with open(&quot;words.txt&quot;) as f:
	text = f.read()
	words = map(lambda word: word[1:-1], text.strip().split(COMMA))
	scores = map(lambda word: score(word), words)
	
	print len(filter(lambda score: is_triangular(score), scores))
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codewords.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codewords.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codewords.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codewords.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codewords.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codewords.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codewords.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codewords.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codewords.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codewords.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codewords.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codewords.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codewords.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codewords.wordpress.com/552/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=552&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codewords.wordpress.com/2011/02/05/project-euler-problem-42-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ca3fb44ca21bbb9f642fe6257ce2d81f?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">raphil</media:title>
		</media:content>
	</item>
		<item>
		<title>Project Euler Problem #45 Solution</title>
		<link>http://codewords.wordpress.com/2011/02/05/project-euler-problem-45-solution/</link>
		<comments>http://codewords.wordpress.com/2011/02/05/project-euler-problem-45-solution/#comments</comments>
		<pubDate>Sat, 05 Feb 2011 21:16:41 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://codewords.wordpress.com/?p=536</guid>
		<description><![CDATA[This program starts at n = 286 and calculates triangle numbers from that point, checking whether the triangle number is also pentagonal and hexagonal:<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=536&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This program starts at <em>n</em> = 286 and calculates triangle numbers from that point, checking whether the triangle number is also pentagonal and hexagonal:</p>
<p><pre class="brush: csharp;">
public static void Main(string[] args)
{
    for (var n = 286L; ; n++)
    {
        var triangleNumber = TriangleNumber(n);
        if (IsPentagonal(triangleNumber) &amp;&amp; IsHexagonal(triangleNumber))
        {
            Console.WriteLine(triangleNumber);
            break;
        }
    }
    Console.ReadKey();
}

public static bool IsPentagonal(long triangleNumber)
{
    var pentagonal = 0L;
    for (var n = 1L; pentagonal &lt;= triangleNumber; n++)
    {
        pentagonal = n * (3 * n - 1) / 2;
        if (pentagonal == triangleNumber)
            return true;
    }
    return false;
}

public static bool IsHexagonal(long triangleNumber)
{
    var hexagonal = 0L;
    for (var n = 1L; hexagonal &lt;= triangleNumber; n++)
    {
        hexagonal = n * (2 * n - 1);
        if (hexagonal == triangleNumber)
            return true;
    }
    return false;
}

private static long TriangleNumber(long n)
{
    return n * (n + 1) / 2;
}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codewords.wordpress.com/536/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codewords.wordpress.com/536/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codewords.wordpress.com/536/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codewords.wordpress.com/536/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codewords.wordpress.com/536/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codewords.wordpress.com/536/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codewords.wordpress.com/536/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codewords.wordpress.com/536/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codewords.wordpress.com/536/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codewords.wordpress.com/536/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codewords.wordpress.com/536/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codewords.wordpress.com/536/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codewords.wordpress.com/536/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codewords.wordpress.com/536/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codewords.wordpress.com&amp;blog=397773&amp;post=536&amp;subd=codewords&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codewords.wordpress.com/2011/02/05/project-euler-problem-45-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ca3fb44ca21bbb9f642fe6257ce2d81f?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">raphil</media:title>
		</media:content>
	</item>
	</channel>
</rss>
