<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>A Little Off &#187; functional programming</title>
	<atom:link href="http://www.bemasher.net/archives/tag/functional-programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bemasher.net</link>
	<description>Code, Computers, Photography and Guns</description>
	<lastBuildDate>Mon, 21 Nov 2011 06:38:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Programming for *wait for it* the fun of it</title>
		<link>http://www.bemasher.net/archives/40?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=programming-for-wait-for-it-the-fun-of-it</link>
		<comments>http://www.bemasher.net/archives/40#comments</comments>
		<pubDate>Thu, 19 Feb 2009 11:44:57 +0000</pubDate>
		<dc:creator>bemasher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Eratosthenes]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Primes]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Sieve]]></category>

		<guid isPermaLink="false">http://bemasher.net/?p=40</guid>
		<description><![CDATA[Most of my friends think that there's something wrong with me, most of them have had programming experience before with at least one class, some of them more programming than that, but the real reason they think there's something wrong with me is that I enjoy programming. Occasionally they'll ask me what I'm up to [...]]]></description>
			<content:encoded><![CDATA[<p>Most of my friends think that there's something wrong with me, most of them have had programming experience before with at least one class, some of them more programming than that, but the real reason they think there's something wrong with me is that I enjoy programming. Occasionally they'll ask me what I'm up to and when I tell them that I'm programming, they assume it's for a class of some kind but recently it's only been for entertainment//fun. None of them seem to understand how I could possibly enjoy programming. To be completely honest I don't really know myself why I enjoy it so much.</p>
<p>Lately I've been fooling around with Python's functional programming which is pretty limited in terms of pure functional programming but is still interesting. Take the following code for example:</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> GetPrimes<span style="color: black;">&#40;</span>p<span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">=</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> i**<span style="color: #ff4500;">2</span> <span style="color: #66cc66;">&lt;</span> p<span style="color: black;">&#91;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; p<span style="color: black;">&#91;</span>i:<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">filter</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: x % p<span style="color: black;">&#91;</span>i-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">!=</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> p<span style="color: black;">&#91;</span>i:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> GetPrimes<span style="color: black;">&#40;</span>p<span style="color: #66cc66;">,</span> i + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> p<br />
<br />
<span style="color: #ff7700;font-weight:bold;">print</span> GetPrimes<span style="color: black;">&#40;</span><span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">51</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">#[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]</span></div></td></tr></tbody></table></div>
<p>This was an example I spent about an hour one night coding. It was brought about by a friend of mine that is in a physics computation course that introduces students to the C programming language and it's applications in science. One of the first problems they were given was to determine whether an integer was a prime number or not.</p>
<p>The python blurb I came up with is a short implementation of the <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a>. The basic principle of the Sieve of Eratosthenes is that given a list of integers between 2 and the number you wish, you can find all the primes by simply crossing out multiples.</p>
<ul>
<li>Start with 2, remove all multiples of 2 from the list.</li>
<li>Move to the next item in the list, this must be a prime</li>
<li>Starting on this number's square (since all of the numbers between this number and it's square have already been crossed out) cross out all multiples of this number.</li>
<li>Continue until we reach a number in the list such that it's square is greater than the largest integer in this list.</li>
</ul>
<p>According to the Wikipedia article on this method for finding primes it's useful for "small primes" such as all primes below 10,000,000. The main point of this is just that I found it fascinating and wasted enough time on it to produce a working function to do this...</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bemasher.net/archives/40/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

