<?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>PatternExon &#187; programming</title>
	<atom:link href="http://www.patternexon.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.patternexon.com</link>
	<description>is an anagram of Not An Expert</description>
	<lastBuildDate>Mon, 24 May 2010 03:36:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Sieve of Eratosthenes</title>
		<link>http://www.patternexon.com/sieve-of-eratosthenes/</link>
		<comments>http://www.patternexon.com/sieve-of-eratosthenes/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 06:12:52 +0000</pubDate>
		<dc:creator>akshat</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[prime number]]></category>
		<category><![CDATA[sieve]]></category>

		<guid isPermaLink="false">http://www.patternexon.com/?p=345</guid>
		<description><![CDATA[To find all primes up to a limit or to check if a number is prime or not, the sieve of eratosthenes is famously used. There are many known optimisations to the basic algorithm, also should check for simple things like if number is even or not. Maybe not just if the number is divisible [...]]]></description>
			<content:encoded><![CDATA[<p>To find all primes up to a limit or to check if a number is prime or not, the <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">sieve of eratosthenes</a> is famously used. There are many known optimisations to the basic algorithm, also should check for simple things like if number is even or not. Maybe not just if the number is divisible by 2 but also 3, 5, 7, 11, etc. I remember there is a graph that points this out. Namely, that when there are multiple algorithms each with different time complexities, the algorithm that has average best performance may not have best performance especially for smaller datasets. I cant find that graph right now, but I am looking.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/ruby -w</span>
<span style="color:#008000; font-style:italic;"># Tue Nov 17 00:06:30 EST 2009</span>
<span style="color:#008000; font-style:italic;"># patternexon AT gmail</span>
<span style="color:#008000; font-style:italic;"># Under GPL v3.0</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Integer</span>
<span style="color:#006600; font-weight:bold;">/*</span>
Sift the Twos <span style="color:#9966CC; font-weight:bold;">and</span> sift the Threes,
The Sieve of Eratosthenes.
<span style="color:#9966CC; font-weight:bold;">When</span> the multiples sublime,
The numbers that remain are Prime.
<span style="color:#006600; font-weight:bold;">*/</span>
<span style="color:#9966CC; font-weight:bold;">def</span> primes_upto<span style="color:#006600; font-weight:bold;">&#40;</span>limit<span style="color:#006600; font-weight:bold;">&#41;</span>
  primes = <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span>
  putative = <span style="color:#CC0066; font-weight:bold;">Array</span><span style="color:#006600; font-weight:bold;">&#40;</span>2..<span style="color:#9900CC;">limit</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  putative.<span style="color:#9900CC;">each_index</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
    primes.<span style="color:#9900CC;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>putative<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>putative.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>j<span style="color:#006600; font-weight:bold;">|</span>
      putative<span style="color:#006600; font-weight:bold;">&#91;</span>j<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> putative<span style="color:#006600; font-weight:bold;">&#91;</span>j<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">%</span> putative<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#006666;">0</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
   putative.<span style="color:#9900CC;">compact</span>!
 <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#0000FF; font-weight:bold;">return</span> primes
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">def</span> prime?
  <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span> == <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">primes_upto</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">last</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">true</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
1.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{i} is #{i.prime?}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.patternexon.com/sieve-of-eratosthenes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler &#8211; Problem 14</title>
		<link>http://www.patternexon.com/project-euler-problem-14/</link>
		<comments>http://www.patternexon.com/project-euler-problem-14/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 06:03:44 +0000</pubDate>
		<dc:creator>akshat</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.patternexon.com/?p=304</guid>
		<description><![CDATA[
Why is this an image?
]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-306" title="Project Euler - Problem 14" src="http://www.patternexon.com/wp-content/uploads/euler14.png" alt="Project Euler - Problem 14" width="492" height="722" /></p>
<p><a href="http://www.patternexon.com/why-is-this-an-image/" target="_self">Why is this an image?</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.patternexon.com/project-euler-problem-14/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler &#8211; Problem 10</title>
		<link>http://www.patternexon.com/project-euler-problem-10/</link>
		<comments>http://www.patternexon.com/project-euler-problem-10/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 05:59:44 +0000</pubDate>
		<dc:creator>akshat</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.patternexon.com/?p=300</guid>
		<description><![CDATA[Why is this an image?
]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-286" title="Project Euler Problem 10" src="http://www.patternexon.com/wp-content/uploads/euler101.png" alt="Project Euler Problem 10" width="444" height="569" /><a href="http://www.patternexon.com/why-is-this-an-image/" target="_self">Why is this an image?</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.patternexon.com/project-euler-problem-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 9</title>
		<link>http://www.patternexon.com/project-euler-problem-9/</link>
		<comments>http://www.patternexon.com/project-euler-problem-9/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 05:57:58 +0000</pubDate>
		<dc:creator>akshat</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.patternexon.com/?p=297</guid>
		<description><![CDATA[
Why is this an image?
]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-285" title="Project Euler Problem 9" src="http://www.patternexon.com/wp-content/uploads/euler91.png" alt="Project Euler Problem 9" width="444" height="569" /></p>
<p><a href="http://www.patternexon.com/why-is-this-an-image/" target="_self">Why is this an image?</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.patternexon.com/project-euler-problem-9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler &#8211; Problem 8</title>
		<link>http://www.patternexon.com/project-euler-problem-8/</link>
		<comments>http://www.patternexon.com/project-euler-problem-8/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 05:50:28 +0000</pubDate>
		<dc:creator>akshat</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.patternexon.com/?p=292</guid>
		<description><![CDATA[Why is this an image?
]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-284" title="Project Euler Problem 8" src="http://www.patternexon.com/wp-content/uploads/euler81.png" alt="Project Euler Problem 8" width="460" height="603" /><a href="http://www.patternexon.com/why-is-this-an-image/" target="_self">Why is this an image?</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.patternexon.com/project-euler-problem-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler &#8211; Problem 7</title>
		<link>http://www.patternexon.com/project-euler-problem-7/</link>
		<comments>http://www.patternexon.com/project-euler-problem-7/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 05:48:43 +0000</pubDate>
		<dc:creator>akshat</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.patternexon.com/?p=290</guid>
		<description><![CDATA[
Why is this an image?
]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-283" title="Project Euler Problem 7" src="http://www.patternexon.com/wp-content/uploads/euler71.png" alt="Project Euler Problem 7" width="444" height="603" /></p>
<p><a href="http://www.patternexon.com/why-is-this-an-image/" target="_self">Why is this an image?</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.patternexon.com/project-euler-problem-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler &#8211; Problem 3</title>
		<link>http://www.patternexon.com/project-euler-problem-3/</link>
		<comments>http://www.patternexon.com/project-euler-problem-3/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 02:11:35 +0000</pubDate>
		<dc:creator>akshat</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://patternexon.wordpress.com/?p=79</guid>
		<description><![CDATA[
Why is this an image?
]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-80" title="euler3" src="http://patternexon.files.wordpress.com/2008/12/euler3.png" alt="euler3" width="420" height="519" /></p>
<p><a href="http://www.patternexon.com/why-is-this-an-image/" target="_blank">Why is this an image?</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.patternexon.com/project-euler-problem-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler problem 2</title>
		<link>http://www.patternexon.com/project-euler-problem-2/</link>
		<comments>http://www.patternexon.com/project-euler-problem-2/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 01:59:46 +0000</pubDate>
		<dc:creator>akshat</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://patternexon.wordpress.com/?p=65</guid>
		<description><![CDATA[And heres the second one
Why is this an image?
]]></description>
			<content:encoded><![CDATA[<p>And heres the second one</p>
<p><a href="http://patternexon.files.wordpress.com/2008/12/euler2.png"><img class="aligncenter size-full wp-image-66" title="Project Euler problem 2" src="http://patternexon.files.wordpress.com/2008/12/euler2.png" alt="Project Euler problem 2" width="372" height="434" /></a><a href="http://www.patternexon.com/why-is-this-an-image/" target="_self"><span id="sample-permalink">Why is this an image?</span></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.patternexon.com/project-euler-problem-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 1</title>
		<link>http://www.patternexon.com/project-euler-problem-1/</link>
		<comments>http://www.patternexon.com/project-euler-problem-1/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 01:26:00 +0000</pubDate>
		<dc:creator>akshat</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[project euler]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://patternexon.wordpress.com/?p=51</guid>
		<description><![CDATA[I am trying to learn ruby, so I thought I&#8217;ll start solving the problems at project euler.
The first few are very easy, heres the first one.


Why is this an image ?
]]></description>
			<content:encoded><![CDATA[<p style="text-align:left;">I am trying to learn ruby, so I thought I&#8217;ll start solving the problems at project euler.</p>
<p style="text-align:left;">The first few are very easy, heres the first one.</p>
<p style="text-align:left;">
<p style="text-align:center;"><a href="http://patternexon.files.wordpress.com/2008/12/euler1rb1.png"><img class="size-full wp-image-61 aligncenter" title="Euler 1" src="http://patternexon.files.wordpress.com/2008/12/euler1rb1.png" alt="Euler 1" width="340" height="417" /></a></p>
<p style="text-align: left;"><a href="http://www.patternexon.com/why-is-this-an-image/" target="_self">Why is this an image ?</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.patternexon.com/project-euler-problem-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
