<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;CUEERX8_fSp7ImA9WhRaFE8.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599</id><updated>2012-02-16T13:20:04.145-07:00</updated><category term="map/reduce" /><category term="feeds" /><category term="sapir-whorf" /><category term="javascript" /><category term="debugging" /><category term="web" /><category term="apple" /><category term="add" /><category term="firebug" /><category term="browser quirks" /><category term="web development" /><category term="adobe" /><category term="SQLite" /><category term="game theory" /><category term="distributed systems" /><category term="sed" /><category term="chrome" /><category term="c#" /><category term="firefox" /><category term="set" /><category term="csharp" /><category term="powershell" /><category term="js" /><category term="shell" /><category term="python" /><category term="steve jobs" /><category term="society" /><category term="software engineering" /><category term="haskell" /><category term="windows" /><category term=".net" /><category term="code" /><category term="probability" /><category term="opera" /><category term="rant" /><category term="asp.net mvc" /><category term="safari" /><category term="humor" /><category term="linux" /><category term="hashset" /><category term="scheme" /><category term="math" /><category term="scala" /><category term="linguistics" /><category term="uuid" /><category term="java" /><category term="internet explorer" /><category term="programming" /><category term="information" /><category term="lisp" /><category term="racket" /><category term="geek" /><category term="bash" /><category term="reddit" /><category term="pdf" /><category term="ie" /><category term="c" /><category term="geometry" /><category term="NServiceBus" /><category term="software" /><category term="languages" /><category term="functional programming" /><category term="project euler" /><category term="mathematics" /><category term="asp.net" /><category term="insanity" /><category term="fun" /><category term="statistics" /><category term="command line" /><category term="collections" /><title>CodeWords</title><subtitle type="html">Code, in words.</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://www.code-words.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://www.code-words.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>84</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/Codewords" /><feedburner:info uri="codewords" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><link rel="license" type="text/html" href="http://creativecommons.org/licenses/by/3.0/" /><logo>http://creativecommons.org/images/public/somerights20.gif</logo><feedburner:feedFlare href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.feedburner.com%2FCodewords" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2FCodewords" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.feedburner.com%2FCodewords" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare href="http://www.bloglines.com/sub/http://feeds.feedburner.com/Codewords" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Ffeeds.feedburner.com%2FCodewords" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2FCodewords" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2FCodewords" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><feedburner:feedFlare href="http://www.addtoany.com/?linkname=CodeWords&amp;linkurl=http%3A%2F%2Ffeeds.feedburner.com%2FCodewords&amp;type=feed" src="http://www.addtoany.com/addfr-b.gif">Add to Any Feed Reader</feedburner:feedFlare><entry gd:etag="W/&quot;Dk4FQX86fyp7ImA9WhdaFkQ.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-2638208585168398110</id><published>2011-10-26T23:55:00.000-06:00</published><updated>2011-10-26T23:55:10.117-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-26T23:55:10.117-06:00</app:edited><title>Transition to New Site</title><content type="html">This blog is now being retired in favor of the &lt;a href="http://jeffreypratt.net/notes/"&gt;notes&lt;/a&gt; on my new &lt;a href="http://jeffreypratt.net/"&gt;site&lt;/a&gt;, which has an &lt;a href="http://jeffreypratt.net/notes/2011/10/26/new-site.html"&gt;explanation&lt;/a&gt; of the transition. This blog will remain but will no longer be updated.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-2638208585168398110?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=l7w7MvJ0Qac:ZvBRvwRBMds:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=l7w7MvJ0Qac:ZvBRvwRBMds:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=l7w7MvJ0Qac:ZvBRvwRBMds:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=l7w7MvJ0Qac:ZvBRvwRBMds:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=l7w7MvJ0Qac:ZvBRvwRBMds:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=l7w7MvJ0Qac:ZvBRvwRBMds:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=l7w7MvJ0Qac:ZvBRvwRBMds:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=l7w7MvJ0Qac:ZvBRvwRBMds:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=l7w7MvJ0Qac:ZvBRvwRBMds:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=l7w7MvJ0Qac:ZvBRvwRBMds:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=l7w7MvJ0Qac:ZvBRvwRBMds:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=l7w7MvJ0Qac:ZvBRvwRBMds:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=l7w7MvJ0Qac:ZvBRvwRBMds:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/l7w7MvJ0Qac" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/10/transition-to-new-site.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/2638208585168398110?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/2638208585168398110?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/l7w7MvJ0Qac/transition-to-new-site.html" title="Transition to New Site" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/10/transition-to-new-site.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkQMRno7fCp7ImA9WhdUGEo.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-2208584968231705990</id><published>2011-10-05T22:13:00.001-06:00</published><updated>2011-10-05T22:13:07.404-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-05T22:13:07.404-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="steve jobs" /><category scheme="http://www.blogger.com/atom/ns#" term="apple" /><title>Steve Jobs, Rest in Peace</title><content type="html">&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://cdn.tipb.com/images/stories//2008/07/steve-jobs.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="200" src="http://cdn.tipb.com/images/stories//2008/07/steve-jobs.jpg" width="155" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Today a great entrepreneur, one of the great American capitalists, Steve Jobs, &lt;a href="http://www.cnn.com/2011/10/05/us/obit-steve-jobs/index.html?iref=BN1&amp;amp;hpt=hp_t1"&gt;passed away&lt;/a&gt; at the rather young age of 56. As others have said, we are all a bit poorer for it.&lt;/p&gt;
&lt;p&gt;I didn't always agree with his style, I wasn't always a fan of his company's products, and I certainly didn't like the personality cult that surrounded him, but I have great respect for the man who believed in what he did, thought big, sought to empower people, and did it with style and aesthetics. He is an inspiration to us all.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-2208584968231705990?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=vVLXg7r_a_A:WCyOCTXTQfU:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=vVLXg7r_a_A:WCyOCTXTQfU:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=vVLXg7r_a_A:WCyOCTXTQfU:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=vVLXg7r_a_A:WCyOCTXTQfU:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=vVLXg7r_a_A:WCyOCTXTQfU:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=vVLXg7r_a_A:WCyOCTXTQfU:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=vVLXg7r_a_A:WCyOCTXTQfU:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=vVLXg7r_a_A:WCyOCTXTQfU:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=vVLXg7r_a_A:WCyOCTXTQfU:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=vVLXg7r_a_A:WCyOCTXTQfU:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=vVLXg7r_a_A:WCyOCTXTQfU:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=vVLXg7r_a_A:WCyOCTXTQfU:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=vVLXg7r_a_A:WCyOCTXTQfU:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/vVLXg7r_a_A" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/10/steve-jobs-rest-in-peace.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/2208584968231705990?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/2208584968231705990?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/vVLXg7r_a_A/steve-jobs-rest-in-peace.html" title="Steve Jobs, Rest in Peace" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/10/steve-jobs-rest-in-peace.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUYHR3g8fip7ImA9WhdUEEo.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-8262582221519574315</id><published>2011-09-01T22:08:00.001-06:00</published><updated>2011-09-26T15:38:56.676-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-09-26T15:38:56.676-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="python" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="mathematics" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><category scheme="http://www.blogger.com/atom/ns#" term="geometry" /><title>Project Euler Problem #102 Solution</title><content type="html">&lt;p&gt;
A triangle in \( \mathbb{R}^2 \) contains the origin iff it has two edges such that
&lt;ol&gt;
&lt;li&gt;one edge intercepts the \( y \)-axis at \( b_1 &gt; 0 \), and&lt;/li&gt;
&lt;li&gt;the other edge intercepts the \( y \)-axis at \( b_2 &lt; 0 \).&lt;/li&gt;
&lt;/ol&gt;
Therefore, for each triangle specified in the provided file, we can perform that test:&lt;/p&gt;
&lt;script src="https://gist.github.com/1187887.js"&gt;&lt;noscript&gt;class Triangle(object):
    def __init__(self, line):
        split_line = line.split(',')
        self.vertices = (Point(int(split_line[0]), int(split_line[1])),
                         Point(int(split_line[2]), int(split_line[3])),
                         Point(int(split_line[4]), int(split_line[5])))

    def edge_lines(self):
        for i in range(len(self.vertices)):
            yield EdgeLine(self.vertices[i], self.vertices[(i + 1) % len(self.vertices)])

class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

class EdgeLine(object):
    def __init__(self, start, end):
        self.start = start
        self.end = end
        
    def slope(self):
        if self.start.x == self.end.x:
            return 1e10
        
        return (float(self.start.y) - self.end.y) / (self.start.x - self.end.x)

    def y_intercept(self):
        return self.start.y - self.slope() * self.start.x

    def __eq__(self, other):
        return self.start == other.start and self.end == other.end \
            or self.end == other.start and self.start == other.end

class TriangleFile(object):
    def __init__(self, file):
        self.file = file

    def triangles(self):
        for line in self.file:
            yield Triangle(line)

def y_intercept_is_on_segment(edge_line):
    y_intercept = edge_line.y_intercept()
    return y_intercept &gt;= edge_line.start.y and y_intercept &lt;= edge_line.end.y \
        or y_intercept &lt;= edge_line.start.y and y_intercept &gt;= edge_line.end.y

def contains_origin(triangle):
    edge_lines = list(triangle.edge_lines())
    return any(edge_line.y_intercept() &gt; 0 and y_intercept_is_on_segment(edge_line) for edge_line in edge_lines) \
        and any(edge_line.y_intercept() &lt; 0 and y_intercept_is_on_segment(edge_line) for edge_line in edge_lines)

if __name__ == "__main__":
    with open("triangles.txt") as file:
        triangle_file = TriangleFile(file)
        count = 0
        for triangle in triangle_file.triangles():
            if contains_origin(triangle):
                count += 1
        print count&lt;/noscript&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-8262582221519574315?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=6yfDWrRA9Y8:OPQ-17Q9TPQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=6yfDWrRA9Y8:OPQ-17Q9TPQ:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=6yfDWrRA9Y8:OPQ-17Q9TPQ:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=6yfDWrRA9Y8:OPQ-17Q9TPQ:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=6yfDWrRA9Y8:OPQ-17Q9TPQ:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=6yfDWrRA9Y8:OPQ-17Q9TPQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=6yfDWrRA9Y8:OPQ-17Q9TPQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=6yfDWrRA9Y8:OPQ-17Q9TPQ:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=6yfDWrRA9Y8:OPQ-17Q9TPQ:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=6yfDWrRA9Y8:OPQ-17Q9TPQ:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=6yfDWrRA9Y8:OPQ-17Q9TPQ:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=6yfDWrRA9Y8:OPQ-17Q9TPQ:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=6yfDWrRA9Y8:OPQ-17Q9TPQ:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/6yfDWrRA9Y8" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/09/project-euler-problem-102-solution.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8262582221519574315?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8262582221519574315?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/6yfDWrRA9Y8/project-euler-problem-102-solution.html" title="Project Euler Problem #102 Solution" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/09/project-euler-problem-102-solution.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUUESXc6fSp7ImA9WhdXF04.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-8134246207794687561</id><published>2011-08-29T11:12:00.004-06:00</published><updated>2011-08-30T13:40:08.915-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-08-30T13:40:08.915-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="powershell" /><category scheme="http://www.blogger.com/atom/ns#" term="command line" /><category scheme="http://www.blogger.com/atom/ns#" term="windows" /><title>Finding Files with PowerShell, Part 2</title><content type="html">&lt;p&gt;This version (see &lt;a href="/2011/08/finding-files-with-powershell.html"&gt;Finding Files with PowerShell&lt;/a&gt;) gives the full path of the items:&lt;/p&gt;
&lt;script src="https://gist.github.com/1178852.js?file=find-files.ps1"&gt;&lt;noscript&gt;Get-ChildItem -Recurse -Filter *some*pattern* | ForEach-Object -Process { $_.FullName }&lt;/noscript&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-8134246207794687561?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=AI1k8grgEwo:KEZZfOJTiSQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=AI1k8grgEwo:KEZZfOJTiSQ:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=AI1k8grgEwo:KEZZfOJTiSQ:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=AI1k8grgEwo:KEZZfOJTiSQ:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=AI1k8grgEwo:KEZZfOJTiSQ:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=AI1k8grgEwo:KEZZfOJTiSQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=AI1k8grgEwo:KEZZfOJTiSQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=AI1k8grgEwo:KEZZfOJTiSQ:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=AI1k8grgEwo:KEZZfOJTiSQ:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=AI1k8grgEwo:KEZZfOJTiSQ:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=AI1k8grgEwo:KEZZfOJTiSQ:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=AI1k8grgEwo:KEZZfOJTiSQ:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=AI1k8grgEwo:KEZZfOJTiSQ:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/AI1k8grgEwo" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/08/finding-files-with-windows-powershell.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8134246207794687561?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8134246207794687561?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/AI1k8grgEwo/finding-files-with-windows-powershell.html" title="Finding Files with PowerShell, Part 2" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/08/finding-files-with-windows-powershell.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEMBRXo9fSp7ImA9WhdQF08.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-3013075385116745022</id><published>2011-08-18T22:00:00.000-06:00</published><updated>2011-08-18T22:00:54.465-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-08-18T22:00:54.465-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="c#" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><category scheme="http://www.blogger.com/atom/ns#" term="csharp" /><title>Project Euler Problem #92 Solution</title><content type="html">&lt;p&gt;A brute-force approach works well enough for &lt;a href="http://projecteuler.net/index.php?section=problems&amp;id=92"&gt;this problem&lt;/a&gt;, too. The code below has the idea:&lt;/p&gt;
&lt;script src="https://gist.github.com/1155989.js?file=solution.cs"&gt;&lt;noscript&gt;using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;

public class Solution
{
    private const int ChainEnd = 89;
    private const int Limit = 10000000;

    public static void Main(string[] args)
    {
        var stopwatch = Stopwatch.StartNew();

        var count = Enumerable.Range(1, Limit).Count(n =&gt; n.LeadsTo(ChainEnd));
        
        stopwatch.Stop();

        Console.WriteLine(count);
        Console.WriteLine("Took {0} ms", stopwatch.ElapsedMilliseconds);

        Console.ReadKey();
    }
}&lt;/noscript&gt;&lt;/script&gt;
&lt;p&gt;The &lt;code&gt;LeadsTo&lt;/code&gt; extension method on &lt;code&gt;Int32&lt;/code&gt; is where the real work happens:&lt;/p&gt;
&lt;script src="https://gist.github.com/1155993.js?file=extensions.cs"&gt;&lt;noscript&gt;using System;
using System.Collections.Generic;
using System.Linq;

static class Extensions
{
    private const char Zero = '0';
    private const int Base10Radix = 10;

    public static bool LeadsTo(this int number, int limit)
    {
        var chainLink = number;

        while (chainLink != 1 &amp;&amp; chainLink != limit)
            chainLink = chainLink.ToString().Select(digit =&gt; ParseInt(digit).Square()).Sum();

        if (chainLink == limit)
            return true;
            
        return false;
    }

    private static int ParseInt(char digit)
    {
        return digit - Zero;
    }

    private static int Square(this int number)
    {
        return number * number;
    }
}&lt;/noscript&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-3013075385116745022?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oyrsrqlARDw:Kjoazq2DbOk:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oyrsrqlARDw:Kjoazq2DbOk:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oyrsrqlARDw:Kjoazq2DbOk:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oyrsrqlARDw:Kjoazq2DbOk:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=oyrsrqlARDw:Kjoazq2DbOk:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oyrsrqlARDw:Kjoazq2DbOk:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=oyrsrqlARDw:Kjoazq2DbOk:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oyrsrqlARDw:Kjoazq2DbOk:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oyrsrqlARDw:Kjoazq2DbOk:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=oyrsrqlARDw:Kjoazq2DbOk:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oyrsrqlARDw:Kjoazq2DbOk:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oyrsrqlARDw:Kjoazq2DbOk:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=oyrsrqlARDw:Kjoazq2DbOk:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/oyrsrqlARDw" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/08/project-euler-problem-92-solution.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/3013075385116745022?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/3013075385116745022?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/oyrsrqlARDw/project-euler-problem-92-solution.html" title="Project Euler Problem #92 Solution" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/08/project-euler-problem-92-solution.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CU4FSXYzeyp7ImA9WhdWFUU.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-4704673474207808035</id><published>2011-08-16T22:12:00.003-06:00</published><updated>2011-09-09T09:58:38.883-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-09-09T09:58:38.883-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="c" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><title>Project Euler Problem #52 Solution</title><content type="html">&lt;p&gt;Brute-force search is quite effective for &lt;a href="http://projecteuler.net/index.php?section=problems&amp;id=52"&gt;this problem&lt;/a&gt;. Just go through the integers from \( 1 \) onward, and find the first one that satisfies the criteria. &lt;em&gt;Nota bene:&lt;/em&gt; I used &lt;a href="http://en.wikipedia.org/wiki/C_%28programming_language%29"&gt;C&lt;/a&gt; out of nostalgia, just for kicks.&lt;/p&gt;&lt;script src="https://gist.github.com/1150791.js?file=solution.c"&gt;&lt;noscript&gt;#include &lt;limits.h&gt;
#include &lt;stdbool.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

#define DIGITS 21
#define MIN_FACTOR 2
#define MAX_FACTOR 6

const bool has_same_digits(const unsigned long x, const unsigned long y) {
    char x_digits[DIGITS];
    char y_digits[DIGITS];

    sprintf(x_digits, "%d", x);
    x_digits[DIGITS - 1] = '\0';
    sprintf(y_digits, "%d", y);
    y_digits[DIGITS - 1] = '\0';

    if (strlen(x_digits) != strlen(y_digits))
        return false;

    int i;
    for (i = 0; i &lt; strlen(x_digits); i++)
        if (strchr(y_digits, x_digits[i]) == NULL)
            return false;
        
    for (i = 0; i &lt; strlen(y_digits); i++)
        if (strchr(x_digits, y_digits[i]) == NULL)
            return false;

    return true;
}

const bool is_same_digit_integer(const unsigned long x) {
    int factor;
    for (factor = MIN_FACTOR; factor &lt;= MAX_FACTOR; factor++)
        if (!has_same_digits(x, x * factor))
            return false;

    return true;
}

const unsigned long get_same_digit_integer(void) {
    unsigned long x;
    for (x = 1; x &lt; ULONG_MAX; x++) {
        if (is_same_digit_integer(x))
            return x;
    }
    return 0;
}

const int main(const int argc, const char *argv[]) {
    printf("%d\n", get_same_digit_integer());

    return EXIT_SUCCESS;
}&lt;/noscript&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-4704673474207808035?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oHEQY1cPYTA:-NXm6jF38r4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oHEQY1cPYTA:-NXm6jF38r4:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oHEQY1cPYTA:-NXm6jF38r4:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oHEQY1cPYTA:-NXm6jF38r4:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=oHEQY1cPYTA:-NXm6jF38r4:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oHEQY1cPYTA:-NXm6jF38r4:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=oHEQY1cPYTA:-NXm6jF38r4:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oHEQY1cPYTA:-NXm6jF38r4:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oHEQY1cPYTA:-NXm6jF38r4:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=oHEQY1cPYTA:-NXm6jF38r4:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oHEQY1cPYTA:-NXm6jF38r4:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=oHEQY1cPYTA:-NXm6jF38r4:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=oHEQY1cPYTA:-NXm6jF38r4:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/oHEQY1cPYTA" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/08/project-euler-problem-52-solution.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/4704673474207808035?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/4704673474207808035?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/oHEQY1cPYTA/project-euler-problem-52-solution.html" title="Project Euler Problem #52 Solution" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/08/project-euler-problem-52-solution.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEEHRn07fip7ImA9WhdQFU8.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-6243039966601628162</id><published>2011-08-16T14:22:00.003-06:00</published><updated>2011-08-16T14:30:37.306-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-08-16T14:30:37.306-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="powershell" /><category scheme="http://www.blogger.com/atom/ns#" term="command line" /><category scheme="http://www.blogger.com/atom/ns#" term="windows" /><title>Finding Files with PowerShell</title><content type="html">&lt;p&gt;I miss the UNIX &lt;code&gt;find&lt;/code&gt; command in Windows. I could get it with Cygwin or GNUWin32, but that requires littering my machine with extra stuff. So, PowerShell to the rescue:&lt;/p&gt;&lt;script src="https://gist.github.com/1150074.js?file=find.ps1"&gt;&lt;noscript&gt;ls -Recurse -Filter *some*pattern* | ft directory, name&lt;/noscript&gt;&lt;/script&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-6243039966601628162?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=jocdam3nYhE:Dp9k5dMRQ3c:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=jocdam3nYhE:Dp9k5dMRQ3c:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=jocdam3nYhE:Dp9k5dMRQ3c:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=jocdam3nYhE:Dp9k5dMRQ3c:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=jocdam3nYhE:Dp9k5dMRQ3c:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=jocdam3nYhE:Dp9k5dMRQ3c:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=jocdam3nYhE:Dp9k5dMRQ3c:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=jocdam3nYhE:Dp9k5dMRQ3c:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=jocdam3nYhE:Dp9k5dMRQ3c:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=jocdam3nYhE:Dp9k5dMRQ3c:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=jocdam3nYhE:Dp9k5dMRQ3c:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=jocdam3nYhE:Dp9k5dMRQ3c:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=jocdam3nYhE:Dp9k5dMRQ3c:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/jocdam3nYhE" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/08/finding-files-with-powershell.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/6243039966601628162?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/6243039966601628162?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/jocdam3nYhE/finding-files-with-powershell.html" title="Finding Files with PowerShell" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/08/finding-files-with-powershell.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0MBRXk5eSp7ImA9WhdTF0Q.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-6443594282059553396</id><published>2011-07-15T22:44:00.000-06:00</published><updated>2011-07-15T22:44:14.721-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-15T22:44:14.721-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="reddit" /><category scheme="http://www.blogger.com/atom/ns#" term="fun" /><category scheme="http://www.blogger.com/atom/ns#" term="humor" /><category scheme="http://www.blogger.com/atom/ns#" term="web" /><title>Reddit's Unique Web Server</title><content type="html">&lt;a href="http://www.reddit.com/"&gt;Reddit&lt;/a&gt; has quite a &lt;a href="http://whatsitrun.apphb.com/Query/?url=http%3A%2F%2Fwww.reddit.com%2F"&gt;unique&lt;/a&gt; web server.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-6443594282059553396?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=wG7DTrm7FgQ:6mErIslPgRE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=wG7DTrm7FgQ:6mErIslPgRE:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=wG7DTrm7FgQ:6mErIslPgRE:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=wG7DTrm7FgQ:6mErIslPgRE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=wG7DTrm7FgQ:6mErIslPgRE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=wG7DTrm7FgQ:6mErIslPgRE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=wG7DTrm7FgQ:6mErIslPgRE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=wG7DTrm7FgQ:6mErIslPgRE:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=wG7DTrm7FgQ:6mErIslPgRE:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=wG7DTrm7FgQ:6mErIslPgRE:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=wG7DTrm7FgQ:6mErIslPgRE:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=wG7DTrm7FgQ:6mErIslPgRE:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=wG7DTrm7FgQ:6mErIslPgRE:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/wG7DTrm7FgQ" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/07/reddits-unique-web-server.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/6443594282059553396?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/6443594282059553396?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/wG7DTrm7FgQ/reddits-unique-web-server.html" title="Reddit's Unique Web Server" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/07/reddits-unique-web-server.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQDQ3Y9fSp7ImA9WhdTFUo.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-8762036112837300120</id><published>2011-06-20T08:12:00.006-06:00</published><updated>2011-07-13T11:32:52.865-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-13T11:32:52.865-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="statistics" /><category scheme="http://www.blogger.com/atom/ns#" term="probability" /><category scheme="http://www.blogger.com/atom/ns#" term="game theory" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><title>How Likely Are You to Be Dealt a Royal Flush?</title><content type="html">&lt;p&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-9MN3GLVjBZA/Tf_MDetaruI/AAAAAAAAA-0/8xVyd94rdzA/s1600/flush.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"&gt;&lt;img border="0" height="200" src="http://1.bp.blogspot.com/-9MN3GLVjBZA/Tf_MDetaruI/AAAAAAAAA-0/8xVyd94rdzA/s200/flush.jpg" width="185" /&gt;&lt;/a&gt;&lt;/div&gt;
In the game of &lt;a href="http://en.wikipedia.org/wiki/Poker"&gt;poker&lt;/a&gt;, you have a &lt;a href="http://en.wikipedia.org/wiki/Royal_flush_(poker_hand)#Straight_flush"&gt;royal flush&lt;/a&gt; when you have a 10, J, Q, K, and A all of the same suit. Assuming a uniform random distribution of cards in the deck, what is the probability of being dealt a royal flush?&lt;/p&gt;

&lt;p&gt;A royal flush consists of a certain five cards of the same suit. Assume we are dealt one of these cards. Then the suit is determined, and we now need the remaining four cards of that suit. So, let's take the probability of being dealt one of the certain five cards (10, J, Q, K, A) of &lt;em&gt;any&lt;/em&gt; suit. Since there are five such cards per suit, and there are four suits (the set of suits is \( \{ \diamondsuit, \heartsuit, \clubsuit, \spadesuit \} \)), then we can get any one of those cards to start building a royal flush. Therefore, the probability of getting a starting card is
\[
P(\text{starting card}) = \frac{20}{52}.
\]
Once we have a starting card of a certain suit, our suit is restricted. That is, we may only get the remaining four cards from the same suit as our starting card. Then the probability of getting one of the four cards from the same suit is
\[
P(\text{one of the four from the same suit}) = \frac{4}{51}.
\]
Likewise, after we get the second card, there are three left to get, so the probabilities are \( 3/50 \), then \( 2/49 \), and then \( 1/48 \). What, then, is the probability that we will get &lt;em&gt;all&lt;/em&gt; of the necessary cards? It is the product of all these probabilities:
\[
P(\text{all}) = \frac{20}{52} \cdot \frac{4}{51} \cdot \frac{3}{50} \cdot \frac{2}{49} \cdot \frac{1}{48} = \frac{1}{649\,740} = 1.539\ldots \times 10^{-6} = 0.000\,153\,9\ldots \%.
\]
Not very good odds.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-8762036112837300120?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=c7R5DoiAOwM:Yi6w-wxCwiA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=c7R5DoiAOwM:Yi6w-wxCwiA:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=c7R5DoiAOwM:Yi6w-wxCwiA:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=c7R5DoiAOwM:Yi6w-wxCwiA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=c7R5DoiAOwM:Yi6w-wxCwiA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=c7R5DoiAOwM:Yi6w-wxCwiA:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=c7R5DoiAOwM:Yi6w-wxCwiA:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=c7R5DoiAOwM:Yi6w-wxCwiA:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=c7R5DoiAOwM:Yi6w-wxCwiA:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=c7R5DoiAOwM:Yi6w-wxCwiA:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=c7R5DoiAOwM:Yi6w-wxCwiA:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=c7R5DoiAOwM:Yi6w-wxCwiA:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=c7R5DoiAOwM:Yi6w-wxCwiA:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/c7R5DoiAOwM" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/06/how-likely-are-you-to-be-dealt-royal.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8762036112837300120?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8762036112837300120?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/c7R5DoiAOwM/how-likely-are-you-to-be-dealt-royal.html" title="How Likely Are You to Be Dealt a Royal Flush?" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-9MN3GLVjBZA/Tf_MDetaruI/AAAAAAAAA-0/8xVyd94rdzA/s72-c/flush.jpg" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/06/how-likely-are-you-to-be-dealt-royal.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0QNQHY6cCp7ImA9WhdXE0o.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-9169362029652611237</id><published>2011-06-18T21:54:00.007-06:00</published><updated>2011-08-26T11:23:11.818-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-08-26T11:23:11.818-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><title>Calculating the Number of Trailing Zeros in a Factorial</title><content type="html">&lt;p&gt;The factorial function, denoted \( n! \), where \( n \in \mathbb{Z}^* \), is defined as follows:
\[
n! =
\begin{cases}
1 &amp; \text{if $n = 0$,} \\
n(n - 1)! &amp; \text{otherwise}.
\end{cases}
\]
In the case of \( n = 10 \), \( 10! = 3\,628\,800 \). Note that that value has two trailing zeros. When \( n = 20 \), \( 20! = 2\,432\,902\,008\,176\,640\,000 \) and has four trailing zeros. Can we devise an algorithm to determine how many trailing zeroes there are in \( n! \) without calculating \( n! \)?&lt;/p&gt;

&lt;p&gt;Let us define a function \( z : \mathbb{N} \to \mathbb{N} \) that accepts an argument \( n \) that yields the number of trailing zeros in \( n! \). So, using our two examples above, we know that \( z(10) = 2 \) and \( z(20) = 4 \). Now, what is \( z(100) \)?&lt;/p&gt;

&lt;p&gt;An integer \( a \) has \( k \) trailing zeros iff \( a = b \cdot 10^k \) for some integer \( b \). Now, the prime factors of \( 10 \) are \( 2 \) and \( 5 \). So, what if we consider the prime factors of each factor in a factorial product? For example, consider the first six terms (after \( 1 \)) of \( 100! \) and their prime factors. We omit \( 1 \) because of its &lt;a href="http://en.wikipedia.org/wiki/Idempotence"&gt;idempotence&lt;/a&gt;:
\[
\begin{align}
2 &amp;amp;= 2 \\
3 &amp;amp;= 3 \\
4 &amp;amp;= 2^2 \\
5 &amp;amp;= 5 \\
6 &amp;amp;= 2 \cdot 3 \\
7 &amp;amp;= 7
\end{align}
\]
We see that \( 7! \) contains one \( 5 \) and (more than) one \( 2 \). Therefore, \( 7! \) contains a factor of \( 10 \), so we should expect there to be one trailing zero in \( 7! \). And we see that \( 7! = 5040 \), which does indeed have one trailing zero. This suggests that we could, for any integer \( n \) in \( n! \), cycle through the integers from \( 2 \) to \( n \), finding the prime factorization of each integer, and counting pairs of \( 2 \) and \( 5 \) that we find.&lt;/p&gt;

&lt;p&gt;You might have noticed from our example of \( 7! \) that there are four \( 2 \)s and only one \( 5 \). Consider that there are \( n/2 \) even factors in \( n! \)—that is, every other integer factor of \( n! \), starting at \(2\), is even. So there are \( n/2 \) even factors. Each of these has at least one \( 2 \) in its prime factorization, and many of them have more than one. Now consider how many factors of \( 5 \) there are in \( n! \): there are, in fact, \( n / 5 \). So, we can see that, for every \( 5 \) we find in \( n! \), there is a \( 2 \) we can match with it. This suggests that we only need count the \( 5 \)s and not the pairs of \( 2 \) and \( 5 \).&lt;/p&gt;

&lt;p&gt;This now suggests an algorithm, presented here in Java:
&lt;script src="https://gist.github.com/1033739.js"&gt;&lt;/script&gt;
&lt;noscript&gt;
public class Solution {
    public static void main(String[] args) {
        System.out.println(countTrailingZerosOfFactorial(100));
    }
    
    private static final int countTrailingZerosOfFactorial(final int n) {
        int count = 0;
        for (int factor = 5; factor &amp;amp;amp;amp;lt;= n; factor++)
            count += countFives(factor);
        return count;
    }

    private static final int countFives(int factor) {
        if (!divides(5, factor))
            return 0;

        return 1 + countFives(factor / 5);
    }
    
    private static final boolean divides(final int d, final int n) {
        return n % d == 0;
    }
}
&lt;/noscript&gt;&lt;/p&gt;

&lt;p&gt;Using this algorithm, we find that \( z(100) = 24 \).&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-9169362029652611237?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=2I_rCfMDk3w:zc23jktqRTg:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=2I_rCfMDk3w:zc23jktqRTg:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=2I_rCfMDk3w:zc23jktqRTg:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=2I_rCfMDk3w:zc23jktqRTg:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=2I_rCfMDk3w:zc23jktqRTg:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=2I_rCfMDk3w:zc23jktqRTg:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=2I_rCfMDk3w:zc23jktqRTg:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=2I_rCfMDk3w:zc23jktqRTg:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=2I_rCfMDk3w:zc23jktqRTg:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=2I_rCfMDk3w:zc23jktqRTg:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=2I_rCfMDk3w:zc23jktqRTg:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=2I_rCfMDk3w:zc23jktqRTg:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=2I_rCfMDk3w:zc23jktqRTg:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/2I_rCfMDk3w" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/06/calculating-number-of-trailing-zeros-in.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/9169362029652611237?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/9169362029652611237?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/2I_rCfMDk3w/calculating-number-of-trailing-zeros-in.html" title="Calculating the Number of Trailing Zeros in a Factorial" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/06/calculating-number-of-trailing-zeros-in.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUcAQX86fCp7ImA9WhZUF0s.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-1612014773501711921</id><published>2011-06-10T21:30:00.000-06:00</published><updated>2011-06-10T21:30:40.114-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-10T21:30:40.114-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><title>Project Euler Problem #37 Solution</title><content type="html">&lt;p&gt;Here I use the trusted Fermat primality tester. Given the knowledge that there are eleven "truncatable primes," we can stop after we've found those eleven.&lt;/p&gt;

&lt;p&gt;Note that at no time does my solution turn candidate numbers into strings.&lt;/p&gt;

&lt;script src="https://gist.github.com/1020213.js"&gt; &lt;/script&gt;
&lt;noscript&gt;public class Solution {
    private static FermatPrimalityTester tester = new FermatPrimalityTester(20);
    
    public static void main(final String[] args) {
        int sum = 0;
        int count = 0;
        
        final long start = System.currentTimeMillis();

        for (int n = 11; count &lt; 11; n++)
            if (isTruncatablePrime(n)) {
                sum += n;
                count++;
            }

        System.out.println(sum);
        System.out.println(System.currentTimeMillis() - start);
    }

    private static boolean isPrime(final int candidate) {
        if (candidate &lt; 2)
            return false;
        
        if (candidate == 2 || candidate == 3 || candidate == 5 || candidate == 7)
            return true;
        
        return tester.isPrime(candidate);
    }

    private static boolean isTruncatablePrime(final int candidate) {
        return isRightTruncatablePrime(candidate) &amp;&amp; isLeftTruncatablePrime(candidate);
    }

    private static boolean isRightTruncatablePrime(int candidate) {
        while (candidate &gt; 0) {
            if (!isPrime(candidate))
                return false;
            candidate /= 10;
        }
        return true;
    }

    private static boolean isLeftTruncatablePrime(int candidate) {
        while (candidate &gt; 0) {
            if (!isPrime(candidate))
                return false;
            candidate = candidate % pow(10, floorLog10(candidate));
        }
        return true;
    }

    private static int pow(final int base, final int exponent) {
        int result = 1;
        for (int i = 0; i &lt; exponent; i++)
            result *= base;
        return result;
    }
    
    private static int floorLog10(int n) {
        int floorLog = 0;
        while (n &gt; 10) {
            floorLog++;
            n /= 10;
        }
        return floorLog;
    }
}&lt;/noscript&gt;

&lt;p&gt;And I used Java, for old time's sake.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-1612014773501711921?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ukOg5fL36xc:33YOcwrQJFw:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ukOg5fL36xc:33YOcwrQJFw:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ukOg5fL36xc:33YOcwrQJFw:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ukOg5fL36xc:33YOcwrQJFw:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=ukOg5fL36xc:33YOcwrQJFw:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ukOg5fL36xc:33YOcwrQJFw:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=ukOg5fL36xc:33YOcwrQJFw:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ukOg5fL36xc:33YOcwrQJFw:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ukOg5fL36xc:33YOcwrQJFw:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=ukOg5fL36xc:33YOcwrQJFw:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ukOg5fL36xc:33YOcwrQJFw:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ukOg5fL36xc:33YOcwrQJFw:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=ukOg5fL36xc:33YOcwrQJFw:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/ukOg5fL36xc" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/06/project-euler-problem-37-solution.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/1612014773501711921?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/1612014773501711921?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/ukOg5fL36xc/project-euler-problem-37-solution.html" title="Project Euler Problem #37 Solution" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/06/project-euler-problem-37-solution.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Dk8AQX0yfSp7ImA9WhZUF0k.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-2322109131543753076</id><published>2011-06-10T16:27:00.000-06:00</published><updated>2011-06-10T16:27:20.395-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-10T16:27:20.395-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="software" /><category scheme="http://www.blogger.com/atom/ns#" term="pdf" /><category scheme="http://www.blogger.com/atom/ns#" term="adobe" /><title>Adobe Reader's Big Problem</title><content type="html">&lt;p&gt;I use PDFs often. Nothing excels them in fidelity of design to documents for which design is important. The Adobe Reader, in its 5.5 days and earlier, was the standard PDF reader of choice. But with version 6, serious bloat occurred, and Adobe Reader is only now, in its latest versions, recovering from that problem. I have a new problem with Adobe Reader, though. Take a look:&lt;/p&gt;

&lt;div style="text-align: center;"&gt;
&lt;a href="http://www.flickr.com/photos/clementi/5819437518/" title="pdf-problem by clementi, on Flickr"&gt;&lt;img src="http://farm3.static.flickr.com/2420/5819437518_9276183ca4_m.jpg" width="240" height="197" alt="pdf-problem"&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;This is a document that appears to be made of scanned images placed in text pages, produced by \( \mathrm{\TeX} \). Note how the scanned images appear as completely useless gray boxes. Here is how the document should look, and does look, in other PDF viewers:&lt;/p&gt;

&lt;div style="text-align: center;"&gt;
&lt;a href="http://www.flickr.com/photos/clementi/5819441358/" title="pdf-good by clementi, on Flickr"&gt;&lt;img src="http://farm3.static.flickr.com/2786/5819441358_a251d01103_m.jpg" width="223" height="240" alt="pdf-good"&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;I have searched and searched on this matter, but the results are few and old, and whenever I try to employ a solution I do find, it doesn't work.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-2322109131543753076?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lBAJj4heNKM:-6D8j_RTvzQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lBAJj4heNKM:-6D8j_RTvzQ:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lBAJj4heNKM:-6D8j_RTvzQ:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lBAJj4heNKM:-6D8j_RTvzQ:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=lBAJj4heNKM:-6D8j_RTvzQ:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lBAJj4heNKM:-6D8j_RTvzQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=lBAJj4heNKM:-6D8j_RTvzQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lBAJj4heNKM:-6D8j_RTvzQ:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lBAJj4heNKM:-6D8j_RTvzQ:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=lBAJj4heNKM:-6D8j_RTvzQ:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lBAJj4heNKM:-6D8j_RTvzQ:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lBAJj4heNKM:-6D8j_RTvzQ:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=lBAJj4heNKM:-6D8j_RTvzQ:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/lBAJj4heNKM" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/06/adobe-readers-big-problem.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/2322109131543753076?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/2322109131543753076?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/lBAJj4heNKM/adobe-readers-big-problem.html" title="Adobe Reader's Big Problem" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://farm3.static.flickr.com/2420/5819437518_9276183ca4_t.jpg" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/06/adobe-readers-big-problem.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEcBRnY7eSp7ImA9WhZUFUU.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-8789979680774849534</id><published>2011-06-08T20:20:00.000-06:00</published><updated>2011-06-08T20:20:57.801-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-08T20:20:57.801-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="python" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><title>Project Euler Problem #33 Solution</title><content type="html">&lt;p&gt;In this one, I take advantage of Python's &lt;code&gt;tuple&lt;/code&gt; type:&lt;/p&gt;
&lt;script src="https://gist.github.com/1015911.js"&gt; &lt;/script&gt;
&lt;noscript&gt;def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)

def reduce_fraction(num, den):
    _gcd = gcd(num, den)
    return (num / _gcd, den / _gcd)

def multiply_fraction(x, y):
    return (x[0] * y[0], x[1] * y[1])

def are_equal(frac1, frac2):
    return frac1[0] * frac2[1] == frac1[1] * frac2[0]

def cancel_digits(num, den):
    numstr = str(num)
    denstr = str(den)
    for i in range(len(numstr)):
        for j in range(len(denstr)):
            if numstr[i] == denstr[j] and numstr[i] != '0':
                return (int(numstr[0:i] + numstr[i + 1:]),
                        int(denstr[0:j] + denstr[j + 1:]))

    return (int(numstr), int(denstr))

unorthodox_fractions = set()

for den in range(10, 100):
    for num in range(10, den):
        canceled = cancel_digits(num, den)
        if canceled != (num, den) and are_equal(canceled, (num, den)):
            unorthodox_fractions.add(canceled)

product = reduce(multiply_fraction, unorthodox_fractions)
print reduce_fraction(product[0], product[1])[1]&lt;/noscript&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-8789979680774849534?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=QJxLbRe_Y1c:1NgBiXTqTDY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=QJxLbRe_Y1c:1NgBiXTqTDY:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=QJxLbRe_Y1c:1NgBiXTqTDY:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=QJxLbRe_Y1c:1NgBiXTqTDY:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=QJxLbRe_Y1c:1NgBiXTqTDY:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=QJxLbRe_Y1c:1NgBiXTqTDY:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=QJxLbRe_Y1c:1NgBiXTqTDY:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=QJxLbRe_Y1c:1NgBiXTqTDY:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=QJxLbRe_Y1c:1NgBiXTqTDY:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=QJxLbRe_Y1c:1NgBiXTqTDY:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=QJxLbRe_Y1c:1NgBiXTqTDY:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=QJxLbRe_Y1c:1NgBiXTqTDY:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=QJxLbRe_Y1c:1NgBiXTqTDY:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/QJxLbRe_Y1c" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/06/project-euler-problem-33-solution.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8789979680774849534?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8789979680774849534?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/QJxLbRe_Y1c/project-euler-problem-33-solution.html" title="Project Euler Problem #33 Solution" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/06/project-euler-problem-33-solution.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEMDRHo9eSp7ImA9WhZUFEw.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-5004570727755179287</id><published>2011-06-06T16:51:00.001-06:00</published><updated>2011-06-06T21:14:35.461-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-06T21:14:35.461-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="uuid" /><category scheme="http://www.blogger.com/atom/ns#" term="bash" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="shell" /><category scheme="http://www.blogger.com/atom/ns#" term="sed" /><title>Generate a Zero-Valued UUID</title><content type="html">&lt;p&gt;Say you want a UUID of &lt;code&gt;00000000-0000-0000-0000-000000000000&lt;/code&gt;, but you just want to make one fast, without having to find out the number and grouping of digits. Here's a way to do it:&lt;/p&gt;

&lt;script src="https://gist.github.com/1011287.js"&gt; &lt;/script&gt;
&lt;noscript&gt;uuidgen | sed 's/[0-9a-f]/0/g'&lt;/noscript&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-5004570727755179287?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=W3agFtat7p4:3BcgAq8MrRo:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=W3agFtat7p4:3BcgAq8MrRo:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=W3agFtat7p4:3BcgAq8MrRo:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=W3agFtat7p4:3BcgAq8MrRo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=W3agFtat7p4:3BcgAq8MrRo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=W3agFtat7p4:3BcgAq8MrRo:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=W3agFtat7p4:3BcgAq8MrRo:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=W3agFtat7p4:3BcgAq8MrRo:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=W3agFtat7p4:3BcgAq8MrRo:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=W3agFtat7p4:3BcgAq8MrRo:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=W3agFtat7p4:3BcgAq8MrRo:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=W3agFtat7p4:3BcgAq8MrRo:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=W3agFtat7p4:3BcgAq8MrRo:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/W3agFtat7p4" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/06/generate-zero-valued-uuid.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/5004570727755179287?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/5004570727755179287?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/W3agFtat7p4/generate-zero-valued-uuid.html" title="Generate a Zero-Valued UUID" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/06/generate-zero-valued-uuid.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEIBRH0zfCp7ImA9WhZVFUw.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-2664012282642909038</id><published>2011-05-27T11:14:00.001-06:00</published><updated>2011-05-27T11:15:55.384-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-27T11:15:55.384-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="lisp" /><category scheme="http://www.blogger.com/atom/ns#" term="functional programming" /><category scheme="http://www.blogger.com/atom/ns#" term="racket" /><category scheme="http://www.blogger.com/atom/ns#" term="code" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="scheme" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><title>The Beauty of Scheme</title><content type="html">&lt;p&gt;
Bertrand Russell once &lt;a href="http://books.google.com/books?id=zwMQAAAAYAAJ&amp;pg=PA60&amp;dq=Mathematics+rightly+viewed+possesses+not+only+truth+but+supreme+beauty+a+beauty+cold+and+austere+like+that+of+sculpture+without+appeal+to+any+part+of+our+weaker+nature+without+the+gorgeous+trappings+inauthor:Russell&amp;hl=en#v=onepage&amp;q=Mathematics%20rightly%20viewed%20possesses%20not%20only%20truth%20but%20supreme%20beauty%20a%20beauty%20cold%20and%20austere%20like%20that%20of%20sculpture%20without%20appeal%20to%20any%20part%20of%20our%20weaker%20nature%20without%20the%20gorgeous%20trappings%20inauthor%3ARussell&amp;f=false"&gt;noted&lt;/a&gt; that
&lt;blockquote&gt;Mathematics, rightly viewed, possesses not only truth, but supreme beauty&amp;mdash;a beauty cold and austere, like that of sculpture, without appeal to any part of our weaker nature, without the gorgeous trappings of painting or music, yet sublimely pure, and capable of a stern perfection such as only the greatest art can show. The true spirit of delight, the exaltation, the sense of being more than Man, which is the touchstone of the highest excellence, is to be found in mathematics as surely as poetry.&lt;/blockquote&gt;
I think much the same can be said of code, especially code written in certain languages. There is something so elegantly simple and beautiful about &lt;a href="http://www.r6rs.org/"&gt;Scheme&lt;/a&gt; (and &lt;a href="http://racket-lang.org/"&gt;Racket&lt;/a&gt;), for example, that shares the same aesthetics of mathematics.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-2664012282642909038?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ENMKCf7xhVQ:HbmtbFYJPeU:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ENMKCf7xhVQ:HbmtbFYJPeU:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ENMKCf7xhVQ:HbmtbFYJPeU:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ENMKCf7xhVQ:HbmtbFYJPeU:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=ENMKCf7xhVQ:HbmtbFYJPeU:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ENMKCf7xhVQ:HbmtbFYJPeU:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=ENMKCf7xhVQ:HbmtbFYJPeU:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ENMKCf7xhVQ:HbmtbFYJPeU:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ENMKCf7xhVQ:HbmtbFYJPeU:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=ENMKCf7xhVQ:HbmtbFYJPeU:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ENMKCf7xhVQ:HbmtbFYJPeU:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ENMKCf7xhVQ:HbmtbFYJPeU:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=ENMKCf7xhVQ:HbmtbFYJPeU:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/ENMKCf7xhVQ" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/05/beauty-of-scheme.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/2664012282642909038?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/2664012282642909038?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/ENMKCf7xhVQ/beauty-of-scheme.html" title="The Beauty of Scheme" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/05/beauty-of-scheme.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C04FQ3k_eip7ImA9WhZaFkg.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-1349189978112775431</id><published>2011-05-26T20:38:00.013-06:00</published><updated>2011-07-02T18:11:52.742-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-02T18:11:52.742-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="functional programming" /><category scheme="http://www.blogger.com/atom/ns#" term="racket" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="scheme" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><title>Project Euler Problem #55 Solution</title><content type="html">&lt;p&gt;"A &lt;em&gt;Lychrel number&lt;/em&gt; is a natural number which cannot form a palindrome through the iterative process of repeatedly reversing its base 10 digits and adding the resulting numbers" (&lt;a href="http://en.wikipedia.org/wiki/Lychrel_number"&gt;Wikipedia&lt;/a&gt;). We are given in the problem description that, for the purposes of the problem, any natural number \( n \in \mathbb{N} \cap [0, 10\,000) \) that produces no palindrome in 50 iterations is Lychrel. With that knowledge, a simple recursive test is in order:&lt;/p&gt;

&lt;script src="https://gist.github.com/994527.js"&gt; &lt;/script&gt;
&lt;noscript&gt;
#lang racket

(define (lychrel? n)
  (letrec ((lychrel-aux? (lambda (n count)
                           (if (= count 0)
                               #t
                               (let ((sum (+ n (flip n))))
                                 (if (palindromic? sum)
                                     #f
                                     (lychrel-aux? sum (- count 1))))))))
    (lychrel-aux? n 50)))

(define (string-reverse str)
  (if (equal? str "")
      ""
      (let ((strlen (string-length str)))
        (string-append (substring str (- strlen 1) strlen)
                       (string-reverse (substring str 0 (- strlen 1)))))))

(define (flip n)
  (string-&amp;amp;amp;amp;gt;number (string-reverse (number-&amp;amp;amp;amp;gt;string n))))

(define (palindromic? n)
  (= n (flip n)))

(display (length (filter lychrel? (build-list 10000 values))))
(newline)
&lt;/noscript&gt;

&lt;p&gt;&lt;strike&gt;The &lt;code&gt;flip&lt;/code&gt; function is goofy, I know; I'm probably not familiar enough with Racket to reverse a string the "right" way, but it does work.&lt;/strike&gt;&lt;/p&gt;
&lt;p&gt;UPDATE: I've improved the &lt;code&gt;flip&lt;/code&gt; function to be more Scheme-y.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-1349189978112775431?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=PvxqhmeMvZw:vLcPaY1cfJQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=PvxqhmeMvZw:vLcPaY1cfJQ:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=PvxqhmeMvZw:vLcPaY1cfJQ:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=PvxqhmeMvZw:vLcPaY1cfJQ:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=PvxqhmeMvZw:vLcPaY1cfJQ:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=PvxqhmeMvZw:vLcPaY1cfJQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=PvxqhmeMvZw:vLcPaY1cfJQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=PvxqhmeMvZw:vLcPaY1cfJQ:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=PvxqhmeMvZw:vLcPaY1cfJQ:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=PvxqhmeMvZw:vLcPaY1cfJQ:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=PvxqhmeMvZw:vLcPaY1cfJQ:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=PvxqhmeMvZw:vLcPaY1cfJQ:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=PvxqhmeMvZw:vLcPaY1cfJQ:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/PvxqhmeMvZw" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/05/project-euler-problem-55-solution.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/1349189978112775431?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/1349189978112775431?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/PvxqhmeMvZw/project-euler-problem-55-solution.html" title="Project Euler Problem #55 Solution" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/05/project-euler-problem-55-solution.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUMCSHs4eyp7ImA9WhZVEko.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-7176622376644928946</id><published>2011-05-24T16:44:00.002-06:00</published><updated>2011-05-24T16:51:09.533-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-24T16:51:09.533-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="python" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><title>A Human-Usable URL Parser for Python</title><content type="html">&lt;p&gt;Yes, there is the &lt;code&gt;urlparse&lt;/code&gt; module in Python 2.7 and the &lt;code&gt;urllib.parse&lt;/code&gt; function in Python 3, but each of them just creates a 6-tuple of URL parts, and it's up to you to know that the &lt;code&gt;query&lt;/code&gt; part, for example, is at position 4.&lt;/p&gt;

&lt;p&gt;So here's a neat little wrapper around that functionality which gives named fields to each part:&lt;/p&gt;

&lt;script src="https://gist.github.com/989916.js"&gt; &lt;/script&gt;
&lt;noscript&gt;
import urlparse

class Url(object):
    def __init__(self, url):
        self._original = url
        
        self._parts = urlparse.urlparse(url)

        self.scheme = self._parts[0]
        self.netloc = self._parts[1]
        self.path = self._parts[2]
        self.parameters = self._parts[3]
        self.query = self._parts[4]
        self.fragment = self._parts[5]

    def __str__(self):
        return self._original
&lt;/noscript&gt;

&lt;p&gt;So, to get the &lt;code&gt;query&lt;/code&gt; part, you just do &lt;code&gt;parsed_url.query&lt;/code&gt; instead of &lt;code&gt;parsed_url[4]&lt;/code&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-7176622376644928946?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=bSZpq5YtrAU:aYr-IBzeiwk:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=bSZpq5YtrAU:aYr-IBzeiwk:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=bSZpq5YtrAU:aYr-IBzeiwk:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=bSZpq5YtrAU:aYr-IBzeiwk:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=bSZpq5YtrAU:aYr-IBzeiwk:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=bSZpq5YtrAU:aYr-IBzeiwk:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=bSZpq5YtrAU:aYr-IBzeiwk:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=bSZpq5YtrAU:aYr-IBzeiwk:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=bSZpq5YtrAU:aYr-IBzeiwk:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=bSZpq5YtrAU:aYr-IBzeiwk:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=bSZpq5YtrAU:aYr-IBzeiwk:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=bSZpq5YtrAU:aYr-IBzeiwk:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=bSZpq5YtrAU:aYr-IBzeiwk:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/bSZpq5YtrAU" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/05/human-usable-url-parser-for-python.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/7176622376644928946?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/7176622376644928946?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/bSZpq5YtrAU/human-usable-url-parser-for-python.html" title="A Human-Usable URL Parser for Python" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/05/human-usable-url-parser-for-python.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkcHRH4_cSp7ImA9WhZWGEs.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-7153694685624715274</id><published>2011-05-19T22:00:00.001-06:00</published><updated>2011-05-19T22:00:35.049-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-19T22:00:35.049-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="rant" /><category scheme="http://www.blogger.com/atom/ns#" term="society" /><title>How I Saved My Friends Lots of Money</title><content type="html">&lt;p&gt;Last night I got a phone call from one of my neighbors. She told me that she needed to use some courseware for a class at her university and said that she was unable to use it, and that she was told by someone that she needed to upgrade Windows to use it. They further said that, since a Windows license costs on the order of $200, she should just &lt;em&gt;buy a new laptop&lt;/em&gt;. Now, this courseware is a web application—&lt;em&gt;just a web site&lt;/em&gt;—yet she was told to &lt;em&gt;get a new laptop&lt;/em&gt;. Naturally, my BS-meter went off, and I asked if I could come take a look.&lt;/p&gt; &lt;p&gt;I went over and looked, and saw that the real problem was not that the web site didn’t work—it worked just fine—but that the course documents were Microsoft Word .docx files. Not having Office or any other office apps, she had nothing with which to open them. So, I told her the nature of the problem and installed &lt;a href="http://www.libreoffice.org/"&gt;LibreOffice&lt;/a&gt;. Problem solved for $0.&lt;/p&gt; &lt;p&gt;The moral of this story? These points:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;“Computer literacy,” so called, is woefully scarce in our society. I don’t really fault my neighbor so much as the yahoo she spoke with who told her such outrageous nonsense.  &lt;li&gt;Professors should make their course documents available as PDFs, which is the proper interchange format for important documents. (What’s interesting about this is that Microsoft Word 2007 and 2010, which use the .docx format natively, make producing PDFs from Word easier than ever before.)&lt;/li&gt;&lt;/ul&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-7153694685624715274?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=eQVjE2dvuJQ:GA_KCbyOF8w:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=eQVjE2dvuJQ:GA_KCbyOF8w:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=eQVjE2dvuJQ:GA_KCbyOF8w:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=eQVjE2dvuJQ:GA_KCbyOF8w:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=eQVjE2dvuJQ:GA_KCbyOF8w:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=eQVjE2dvuJQ:GA_KCbyOF8w:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=eQVjE2dvuJQ:GA_KCbyOF8w:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=eQVjE2dvuJQ:GA_KCbyOF8w:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=eQVjE2dvuJQ:GA_KCbyOF8w:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=eQVjE2dvuJQ:GA_KCbyOF8w:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=eQVjE2dvuJQ:GA_KCbyOF8w:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=eQVjE2dvuJQ:GA_KCbyOF8w:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=eQVjE2dvuJQ:GA_KCbyOF8w:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/eQVjE2dvuJQ" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/05/how-i-saved-my-friends-lots-of-money.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/7153694685624715274?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/7153694685624715274?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/eQVjE2dvuJQ/how-i-saved-my-friends-lots-of-money.html" title="How I Saved My Friends Lots of Money" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/05/how-i-saved-my-friends-lots-of-money.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CU4DQng8fyp7ImA9WhdTFkw.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-8941159405647608852</id><published>2011-05-19T21:28:00.006-06:00</published><updated>2011-07-13T21:26:13.677-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-13T21:26:13.677-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><category scheme="http://www.blogger.com/atom/ns#" term="scala" /><title>Project Euler Problem #35 Solution</title><content type="html">&lt;p&gt;Using &lt;a href="http://en.wikipedia.org/wiki/Scala_(programming_language)"&gt;Scala&lt;/a&gt; this time, I tackled the problem of finding all of the &lt;a href="http://en.wikipedia.org/wiki/Circular_prime"&gt;circular primes&lt;/a&gt; less than \( 10^6 \).&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;circular prime&lt;/em&gt; is a prime number such that every "circular" permutation of its digits is also prime. For example, 197, 971, and 719 are circular primes. The single-digit primes 2, 3, 5 and 7 are considered circular primes for the purposes of this problem. Now, some interesting observations about circular primes:&lt;/p&gt;

&lt;ol&gt;
    &lt;li&gt;&lt;strong&gt;No circular prime greater than 5 may contain the digits 0, 2, 4, 5, 6, or 8.&lt;/strong&gt; If some prime \( p &gt; 5 \) were to contain any of these digits, then it would have a circular permutation \( m \) of the form \( 10^n \times d_0 + 10^{n - 1} \times d_1 + \cdots + 10^1 \times d_{n-1} + 10^0 \times d_n \), where \( d_n \) is the offending digit. If the offending digit is 0, 2, 4, 6, or 8, then \( m \) is even and therefore not prime. If the offending digit is 5, then \( m \) is divisible by 5 and therefore not prime. Therefore, only those primes greater than 5 whose digits consist solely of 1, 3, 7, or 9 may be circular primes.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;The number 11 is the only two-digit circular prime with one and only one permutation.&lt;/strong&gt; That is, if you rotate the digits of 11, you get&amp;hellip;11. So 11 is a circular prime.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Observation 2 is mainly a curiosity. But Observation 1 allows us to narrow down the numbers we have to examine.&lt;/p&gt;

&lt;p&gt;To determine the primality of a number, I use the &lt;a href="http://en.wikipedia.org/wiki/Fermat_primality_test"&gt;Fermat Primality Test&lt;/a&gt; used previously. Note that this is a &lt;a href="http://en.wikipedia.org/wiki/Probabilistic_algorithm"&gt;probabilistic algorithm&lt;/a&gt;, which is to say that it can only tell us whether a number is &lt;em&gt;probably&lt;/em&gt; prime. But we can tune it to any degree of certainty (less than 1) by increasing the number \( k \) of rounds. When we use this algorithm, the probability that it falsely tells us that a number is prime is as follows:
\[
P(\text{false positive}) &lt; \frac{1}{2^k}.
\]
In my use of the algorithm here, I chose \( k = 20 \). This yields a probability of correctness of:
\[
P(\text{true positive}) &gt; 1 - \frac{1}{2^{20}} = 0.9999990463256836\ldots.
\]
Good enough for me. So, remembering that I'm new to Scala, here's the code:&lt;/p&gt;

&lt;script src="https://gist.github.com/982250.js"&gt; &lt;/script&gt;
&lt;noscript&gt;
import scala.collection.immutable._

object solution extends App {
  override def main(args: Array[String]) = {
    val tester = new FermatPrimalityTester(20)

    var circularPrimes = new HashSet[Int]

    2.until(1000000).filter(n =&gt; this.isPotentialCandidate(n))
      .foreach(potentialCandidate =&gt; {
        if (!circularPrimes.contains(potentialCandidate) 
          &amp;&amp; tester.isPrime(potentialCandidate)) {
          val circulars = this.getCirculars(potentialCandidate)

          if (circulars.forall(circular =&gt; tester.isPrime(circular))) {
            circularPrimes += potentialCandidate
            circularPrimes ++= circulars
          }
        }
      })

    println(circularPrimes.size)
  }

  private def getCirculars(candidate: Int): Iterable[Int] = {
    var circulars = new HashSet[Int]
    val stringRepresentation = candidate.toString

    for (i &lt;- List.range(1, stringRepresentation.length)) {
      circulars += 
        (stringRepresentation.substring(i, stringRepresentation.length)
          ++ stringRepresentation.substring(0, i)).toInt
    }

    return circulars
  }

  private def isPotentialCandidate(n: Int): Boolean = {
    val stringRepresentation = n.toString

    return n == 2 || n == 5 || 
      (!stringRepresentation.contains('2') 
      &amp;&amp; !stringRepresentation.contains('4') 
      &amp;&amp; !stringRepresentation.contains('5') 
      &amp;&amp; !stringRepresentation.contains('6') 
      &amp;&amp; !stringRepresentation.contains('8'))
  }
}
&lt;/noscript&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-8941159405647608852?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=YVBkOrc06BE:wEw8pUcVApo:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=YVBkOrc06BE:wEw8pUcVApo:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=YVBkOrc06BE:wEw8pUcVApo:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=YVBkOrc06BE:wEw8pUcVApo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=YVBkOrc06BE:wEw8pUcVApo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=YVBkOrc06BE:wEw8pUcVApo:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=YVBkOrc06BE:wEw8pUcVApo:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=YVBkOrc06BE:wEw8pUcVApo:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=YVBkOrc06BE:wEw8pUcVApo:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=YVBkOrc06BE:wEw8pUcVApo:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=YVBkOrc06BE:wEw8pUcVApo:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=YVBkOrc06BE:wEw8pUcVApo:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=YVBkOrc06BE:wEw8pUcVApo:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/YVBkOrc06BE" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/05/project-euler-problem-35-solution.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8941159405647608852?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8941159405647608852?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/YVBkOrc06BE/project-euler-problem-35-solution.html" title="Project Euler Problem #35 Solution" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/05/project-euler-problem-35-solution.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEQMQn89fCp7ImA9WhZTGEQ.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-6857772938483346391</id><published>2011-03-22T21:39:00.000-06:00</published><updated>2011-03-23T08:59:43.164-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-23T08:59:43.164-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="python" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><title>Project Euler Problem #34 Solution</title><content type="html">I used an approach similar to that used in &lt;a href="http://code-words.com/2011/03/project-euler-problem-30-solution.html"&gt;Problem 30&lt;/a&gt;, as shown in the code below.
&lt;br /&gt;&lt;br /&gt;

&lt;script src="https://gist.github.com/882571.js"&gt;
 
&lt;/script&gt;
&lt;noscript&gt;
def digits(n):
    result = []
    while n &amp;gt; 0:
        result = [n % 10] + result
        n /= 10
    if not result:
        return [0]
    return result

def factorial(n):
    return 1 if n == 0 else reduce(lambda x, y: x * y, range(1, n + 1))

def digital_factorial_sum(n):
    return sum(map(lambda d: factorial(d), digits(n)))

def digital_factorial_sums():
    for n in range(1, digital_factorial_sum(9999999) + 1):
        if n &amp;gt;= 10 and digital_factorial_sum(n) == n:
            yield n

print sum(digital_factorial_sums())
&lt;/noscript&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-6857772938483346391?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ORyalzyS2CQ:9u-fK0f6asQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ORyalzyS2CQ:9u-fK0f6asQ:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ORyalzyS2CQ:9u-fK0f6asQ:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ORyalzyS2CQ:9u-fK0f6asQ:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=ORyalzyS2CQ:9u-fK0f6asQ:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ORyalzyS2CQ:9u-fK0f6asQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=ORyalzyS2CQ:9u-fK0f6asQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ORyalzyS2CQ:9u-fK0f6asQ:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ORyalzyS2CQ:9u-fK0f6asQ:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=ORyalzyS2CQ:9u-fK0f6asQ:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ORyalzyS2CQ:9u-fK0f6asQ:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=ORyalzyS2CQ:9u-fK0f6asQ:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=ORyalzyS2CQ:9u-fK0f6asQ:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/ORyalzyS2CQ" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/03/project-euler-problem-34-solution.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/6857772938483346391?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/6857772938483346391?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/ORyalzyS2CQ/project-euler-problem-34-solution.html" title="Project Euler Problem #34 Solution" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/03/project-euler-problem-34-solution.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkICRH8zeSp7ImA9WhZTGEk.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-8100707392073282671</id><published>2011-03-22T20:46:00.000-06:00</published><updated>2011-03-22T20:49:25.181-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-22T20:49:25.181-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="python" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><title>Project Euler Problem #30 Solution</title><content type="html">Let \( S : \mathbb{N} \to \mathbb{N} \) be a function that takes a natural number \( n \) and returns the sum of the fifth powers of its digits. Now, let’s consider some properties of this function. Suppose we limit ourselves to four-digit numbers; what is the maximum value of \( S \)? It is \( S(9999) = 9^5 + 9^5 + 9^5 + 9^5 = 4 \cdot 9^5 = 236\,196 \). What is the maximum of \( S \) over five-digit numbers? It’s \( S(99\,999) = 5 \cdot 9^5 = 295\,245 \). How about six-digit numbers? \( S(999\,999) = 6 \cdot 9^5 = 354\,294 \). Note that \( S(9999) &gt; 9999 \), \( S(99\,999) &gt; 99\,999 \), but \( S(999\,999) &lt; 999\,999 \). Thus the largest number \( n \) that is equal to the sum of the fifth powers of its digits cannot be more than \( S(999\,999) = 354\,294 \). So, we iterate through the naturals from \( 1 \) to \( 354\,294 \) and add the ones in which \( S(n) = n \), as in the code below.
&lt;br /&gt;&lt;br /&gt;

&lt;script src="https://gist.github.com/882513.js"&gt; &lt;/script&gt;
&lt;noscript&gt;
def digits(n):
result = []
while n &gt; 0:
result = [n % 10] + result
n /= 10

if not result:
return [0]

return result

def digital_power_sum(n_digits, p):
return sum(map(lambda x: x ** p, n_digits))


def digital_power_sums(p):
for n in range(digital_power_sum(digits(999999), 5) + 1):
n_digits = digits(n)
if len(n_digits) &gt; 1 and digital_power_sum(n_digits, 5) == n:
yield n

print sum(digital_power_sums(5))
&lt;/noscript&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-8100707392073282671?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=rpiW8BIIFfY:3smFsHRibAA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=rpiW8BIIFfY:3smFsHRibAA:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=rpiW8BIIFfY:3smFsHRibAA:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=rpiW8BIIFfY:3smFsHRibAA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=rpiW8BIIFfY:3smFsHRibAA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=rpiW8BIIFfY:3smFsHRibAA:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=rpiW8BIIFfY:3smFsHRibAA:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=rpiW8BIIFfY:3smFsHRibAA:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=rpiW8BIIFfY:3smFsHRibAA:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=rpiW8BIIFfY:3smFsHRibAA:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=rpiW8BIIFfY:3smFsHRibAA:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=rpiW8BIIFfY:3smFsHRibAA:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=rpiW8BIIFfY:3smFsHRibAA:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/rpiW8BIIFfY" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/03/project-euler-problem-30-solution.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8100707392073282671?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8100707392073282671?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/rpiW8BIIFfY/project-euler-problem-30-solution.html" title="Project Euler Problem #30 Solution" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/03/project-euler-problem-30-solution.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0IGRHo9eip7ImA9Wx9aF0U.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-7143166650565363153</id><published>2011-03-08T19:56:00.000-07:00</published><updated>2011-03-10T13:38:45.462-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-10T13:38:45.462-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="python" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><title>Project Euler Problem #39 Solution</title><content type="html">A variant of Euclid’s formula for generating Pythagorean triples is&lt;br /&gt;
\[&lt;br /&gt;
\begin{align}&lt;br /&gt;
a &amp;= k(m^2 - n^2) \\&lt;br /&gt;
b &amp;= 2kmn \\&lt;br /&gt;
c &amp;= k(m^2 + n^2),&lt;br /&gt;
\end{align}&lt;br /&gt;
\]where \( m \), \( n \), and \( k \) are positive integers and \( m &gt; n \). This will generate all triples, primitive or otherwise, but not uniquely. So, using this formula, I find all of the triples that sum to \( p \in [1, 1000] \cap \mathbb{N} \), count them, and return the value of \( p \) that has the greatest number of solutions.&lt;br /&gt;
&lt;br /&gt;
&lt;script src="https://gist.github.com/861594.js"&gt; &lt;/script&gt;&lt;br /&gt;
&lt;noscript&gt;&lt;pre&gt;import itertools


class PythagoreanTriple(object):
    def __init__(self, a, b, c):
        self._a = a
        self._b = b
        self._c = c
    def __hash__(self):
        return hash(self._a) * hash(self._b) - hash(self._c)
    def __eq__(self, other):
        return (self._a == other._a and self._b == other._b) \
            or (self._a == other._b and self._b == other._a) and self._c == other._c
    def __mul__(self, scalar):
        return PythagoreanTriple(self._a * scalar, self._b * scalar, self._c *scalar)
    def sum(self):
        return self._a + self._b + self._c
    def __str__(self):
        return "({0}, {1}, {2})".format(self._a, self._b, self._c)

def primitive_pythagorean_triples():
    for m in itertools.count(1):
        for n in range(1, m):
            yield PythagoreanTriple(m ** 2 - n ** 2, 2 * m * n, m ** 2 + n ** 2)

best_p = 0
best_count = 0

for p in range(1, 1001):
    solutions = set()
    for triple in primitive_pythagorean_triples():
        for scalar in itertools.count(1):
            scaled = triple * scalar
            perimeter = scaled.sum()
            if perimeter == p:
                solutions.add(scaled)
            elif perimeter &gt; p:
                break
        if triple.sum() &gt; p:
            break
    if len(solutions) &gt; best_count:
        best_count = len(solutions)
        best_p = p

print best_p&lt;/pre&gt;&lt;/noscript&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-7143166650565363153?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=-XWdx6iYiVA:b5_WkCkwqT8:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=-XWdx6iYiVA:b5_WkCkwqT8:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=-XWdx6iYiVA:b5_WkCkwqT8:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=-XWdx6iYiVA:b5_WkCkwqT8:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=-XWdx6iYiVA:b5_WkCkwqT8:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=-XWdx6iYiVA:b5_WkCkwqT8:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=-XWdx6iYiVA:b5_WkCkwqT8:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=-XWdx6iYiVA:b5_WkCkwqT8:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=-XWdx6iYiVA:b5_WkCkwqT8:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=-XWdx6iYiVA:b5_WkCkwqT8:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=-XWdx6iYiVA:b5_WkCkwqT8:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=-XWdx6iYiVA:b5_WkCkwqT8:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=-XWdx6iYiVA:b5_WkCkwqT8:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/-XWdx6iYiVA" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/03/project-euler-problem-39-solution.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/7143166650565363153?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/7143166650565363153?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/-XWdx6iYiVA/project-euler-problem-39-solution.html" title="Project Euler Problem #39 Solution" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/03/project-euler-problem-39-solution.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEIBSH8-eSp7ImA9Wx9aEUQ.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-8754698826957612012</id><published>2011-02-13T22:11:00.000-07:00</published><updated>2011-03-03T15:49:19.151-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-03T15:49:19.151-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="python" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><title>Project Euler Problem #12 Solution</title><content type="html">By the use of a theorem and a (relatively) efficient factorization algorithm, we can solve this problem fairly quickly. First, the theorem:&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Theorem.&lt;/strong&gt; If \( n = p_1^{\alpha_1} p_2^{\alpha_2} \cdots p_s^{\alpha_s} \), with each \( p_s \) prime, then the number of divisors of \( n \), \( d(n) \), is&lt;br /&gt;
\[&lt;br /&gt;
d(n) = (\alpha_1 + 1)(\alpha_2 + 1) \cdots (\alpha_s + 1) = \prod_{i=1}^s (\alpha_i + 1).&lt;br /&gt;
\]This theorem tells us that if we can decompose \( n \) into its prime factors, we can efficiently calculate the number of divisors of \( n \). It remains, therefore, for us to find a suitably efficient factorization algorithm. How about this:&lt;br /&gt;
&lt;br /&gt;
&lt;script src="https://gist.github.com/825507.js"&gt; &lt;/script&gt;&lt;noscript&gt;&lt;pre&gt;def prime_factorization(n):
    candidate = 2
    while n &gt; 1:
        factors = []
        while n % candidate == 0:
            factors.append(candidate)
            n /= candidate
        if factors:
            yield factors
        candidate += 1&lt;/pre&gt;&lt;/noscript&gt;This algorithm starts with a divisor of &lt;code&gt;2&lt;/code&gt; and, if &lt;code&gt;n&lt;/code&gt; is divisible by the divisor, it divides &lt;code&gt;n&lt;/code&gt; by the divisor as long as &lt;code&gt;n&lt;/code&gt; is still divisible by the divisor. When &lt;code&gt;n&lt;/code&gt; no longer is, it increments the divisor and tries again until &lt;code&gt;n&lt;/code&gt; is reduced to &lt;code&gt;1&lt;/code&gt;. Furthermore, each of the prime factors of &lt;code&gt;n&lt;/code&gt; that equal each other are grouped into subsequences. That is, for &lt;code&gt;n = 1701&lt;/code&gt;, we get &lt;code&gt;[[3, 3, 3, 3, 3], [7]]&lt;/code&gt;. This makes it easier to use the theorem above.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&lt;script src="https://gist.github.com/825510.js"&gt; &lt;/script&gt;&lt;noscript&gt;&lt;pre&gt;def triangle_numbers():
    n = 1
    while True:
        yield n * (n + 1) / 2
        n += 1

def prime_factorization(n):
    candidate = 2
    while n &gt; 1:
        factors = []
        while n % candidate == 0:
            factors.append(candidate)
            n /= candidate
        if factors:
            yield factors
        candidate += 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&lt;/pre&gt;&lt;/noscript&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-8754698826957612012?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=5mfkTqC0VIU:M9Lij3Phf-M:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=5mfkTqC0VIU:M9Lij3Phf-M:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=5mfkTqC0VIU:M9Lij3Phf-M:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=5mfkTqC0VIU:M9Lij3Phf-M:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=5mfkTqC0VIU:M9Lij3Phf-M:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=5mfkTqC0VIU:M9Lij3Phf-M:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=5mfkTqC0VIU:M9Lij3Phf-M:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=5mfkTqC0VIU:M9Lij3Phf-M:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=5mfkTqC0VIU:M9Lij3Phf-M:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=5mfkTqC0VIU:M9Lij3Phf-M:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=5mfkTqC0VIU:M9Lij3Phf-M:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=5mfkTqC0VIU:M9Lij3Phf-M:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=5mfkTqC0VIU:M9Lij3Phf-M:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/5mfkTqC0VIU" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/02/project-euler-problem-12-solution.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8754698826957612012?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/8754698826957612012?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/5mfkTqC0VIU/project-euler-problem-12-solution.html" title="Project Euler Problem #12 Solution" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/02/project-euler-problem-12-solution.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DU8FSXs5cCp7ImA9Wx9aEE0.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-2851050023667503764</id><published>2011-02-11T15:21:00.000-07:00</published><updated>2011-03-01T12:30:18.528-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-01T12:30:18.528-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="python" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><title>Project Euler Problem #14 Solution</title><content type="html">&lt;p&gt;We calculate the length of each Collatz sequence with starting number&lt;br /&gt;
&lt;span class="MathJax_Preview"&gt;&lt;img src="http://mathurl.com/4qoy9h3.png"/&gt;&lt;/span&gt;&lt;script type="math/tex"&gt;n \in [1, 10^6) \cap \mathbb{N}&lt;/script&gt; and find the value of \( n \) for which the length is maximum.&lt;/p&gt;&lt;p&gt;Note that we actually calculate one less than the length of each sequence, but since that subtraction is applied to each length, it doesn't matter.&lt;/p&gt;&lt;script src="https://gist.github.com/828757.js"&gt; &lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-2851050023667503764?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=z2wDEXyIDjw:fedGVTkL230:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=z2wDEXyIDjw:fedGVTkL230:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=z2wDEXyIDjw:fedGVTkL230:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=z2wDEXyIDjw:fedGVTkL230:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=z2wDEXyIDjw:fedGVTkL230:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=z2wDEXyIDjw:fedGVTkL230:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=z2wDEXyIDjw:fedGVTkL230:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=z2wDEXyIDjw:fedGVTkL230:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=z2wDEXyIDjw:fedGVTkL230:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=z2wDEXyIDjw:fedGVTkL230:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=z2wDEXyIDjw:fedGVTkL230:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=z2wDEXyIDjw:fedGVTkL230:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=z2wDEXyIDjw:fedGVTkL230:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/z2wDEXyIDjw" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/02/project-euler-problem-14-solution.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/2851050023667503764?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/2851050023667503764?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/z2wDEXyIDjw/project-euler-problem-14-solution.html" title="Project Euler Problem #14 Solution" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/02/project-euler-problem-14-solution.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkYBSH85fip7ImA9Wx9UGE8.&quot;"><id>tag:blogger.com,1999:blog-1850404271021106599.post-864453262432023173</id><published>2011-02-08T13:56:00.000-07:00</published><updated>2011-02-15T19:42:39.126-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-02-15T19:42:39.126-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="python" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="math" /><title>Project Euler Problem #24 Solution</title><content type="html">&lt;p&gt;This code gets the millionth lexicographic permutation of \( (0, 1, 2, \dots, 9) \):
&lt;script src="https://gist.github.com/828764.js"&gt; &lt;/script&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1850404271021106599-864453262432023173?l=www.code-words.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lr3DyE3aauI:ERgcDd2aorI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lr3DyE3aauI:ERgcDd2aorI:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lr3DyE3aauI:ERgcDd2aorI:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lr3DyE3aauI:ERgcDd2aorI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=lr3DyE3aauI:ERgcDd2aorI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lr3DyE3aauI:ERgcDd2aorI:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=lr3DyE3aauI:ERgcDd2aorI:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lr3DyE3aauI:ERgcDd2aorI:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lr3DyE3aauI:ERgcDd2aorI:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=lr3DyE3aauI:ERgcDd2aorI:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lr3DyE3aauI:ERgcDd2aorI:YwkR-u9nhCs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?d=YwkR-u9nhCs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/Codewords?a=lr3DyE3aauI:ERgcDd2aorI:-BTjWOF_DHI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/Codewords?i=lr3DyE3aauI:ERgcDd2aorI:-BTjWOF_DHI" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Codewords/~4/lr3DyE3aauI" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.code-words.com/2011/02/project-euler-problem-24-solution.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/864453262432023173?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1850404271021106599/posts/default/864453262432023173?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Codewords/~3/lr3DyE3aauI/project-euler-problem-24-solution.html" title="Project Euler Problem #24 Solution" /><author><name>Jeff Pratt</name><uri>http://www.blogger.com/profile/17371535059565065892</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://4.bp.blogspot.com/_nqRS3KKVEnE/SiIE_orBzMI/AAAAAAAAAaA/DmVV0sRzJ3k/S220/2e86eff.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.code-words.com/2011/02/project-euler-problem-24-solution.html</feedburner:origLink></entry></feed>

