<?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; ruby</title>
	<atom:link href="http://www.patternexon.com/category/ruby/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 6</title>
		<link>http://www.patternexon.com/project-euler-problem-6/</link>
		<comments>http://www.patternexon.com/project-euler-problem-6/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 16:32:53 +0000</pubDate>
		<dc:creator>akshat</dc:creator>
				<category><![CDATA[project euler]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.patternexon.com/?p=200</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-282" title="Project Euler 6" src="http://www.patternexon.com/wp-content/uploads/euler61.png" alt="Project Euler 6" width="396" height="501" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.patternexon.com/project-euler-problem-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler &#8211; Problem 5</title>
		<link>http://www.patternexon.com/project-euler-problem-5/</link>
		<comments>http://www.patternexon.com/project-euler-problem-5/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 15:38:54 +0000</pubDate>
		<dc:creator>akshat</dc:creator>
				<category><![CDATA[project euler]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.patternexon.com/?p=186</guid>
		<description><![CDATA[Ok. I have to admit this took more time in getting there.
I did not want to brute force the answer and I did not remember how GCD is calculated.
I do like that looping &#8211; its sort of fancy &#8211; but am afraid I wont be using it any time soon, if ever.
]]></description>
			<content:encoded><![CDATA[<div id="attachment_190" class="wp-caption aligncenter" style="width: 374px"><img class="size-full wp-image-190" title="euler5" src="http://www.patternexon.com/wp-content/uploads/euler5.png" alt="Project Euler 5" width="364" height="508" /><p class="wp-caption-text">Project Euler 5</p></div>
<p>Ok. I have to admit this took more time in getting there.</p>
<p>I did not want to brute force the answer and I did not remember how GCD is calculated.</p>
<p>I do like that looping &#8211; its sort of fancy &#8211; but am afraid I wont be using it any time soon, if ever.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.patternexon.com/project-euler-problem-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler &#8211; Problem 4</title>
		<link>http://www.patternexon.com/project-euler-problem-4/</link>
		<comments>http://www.patternexon.com/project-euler-problem-4/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 16:01:27 +0000</pubDate>
		<dc:creator>akshat</dc:creator>
				<category><![CDATA[project euler]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.patternexon.com/?p=183</guid>
		<description><![CDATA[After a while I had time to play around with Project Euler again.
This is again a pretty straightforward solution. The ruby value from this exercise is that I found ruby does not have increment or decrement operator, which is a departure from C and Java. The argument for this design decision is here.
The only hint [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_198" class="wp-caption aligncenter" style="width: 406px"><img class="size-full wp-image-198" title="euler4.rb" src="http://www.patternexon.com/wp-content/uploads/euler4.rb1.png" alt="Project Euler 4" width="396" height="644" /><p class="wp-caption-text">Project Euler 4</p></div>
<p>After a while I had time to play around with Project Euler again.</p>
<p>This is again a pretty straightforward solution. The ruby value from this exercise is that I found ruby does not have increment or decrement operator, which is a departure from C and Java. The argument for this design decision is <a href="http://redmine.ruby-lang.org/issues/show/1432" target="_blank">here</a>.</p>
<p>The only hint I think may be useful for someone just stuck at what looks like a pretty good palindrome is that look at your loop again may be there is a greater number that you never reached because you broke the loop.</p>
<p>By the way, <a href="http://www.ruby-doc.org/docs/Newcomers/ruby.html" target="_blank">this</a> is a great resource for ruby newbies like me!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.patternexon.com/project-euler-problem-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
