<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Gideon Dsouza - math</title><link>http://gideondsouza.com:80/Tags/math</link><description>Gideon Dsouza - math</description><item><title>a superfast way to search for prime numbers [sieve of eratosthenes]</title><link>http://gideondsouza.com:80/blog/a-superfast-way-to-search-for-prime-numbers</link><description>&lt;p&gt;I've been dabbling a little lately on &lt;em&gt;pure&lt;/em&gt; computer science stuff like computational complexity, algorithm design and math. (And interestingly I failed high school math quite badly once)&lt;/p&gt;

&lt;p&gt;Project Euler has been an interesting learning/practice ground : &lt;a href="http://projecteuler.net/about"&gt;http://projecteuler.net/about&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The problems here involve some math, but also deal with time complexity. If done wrong, some of the solutions to their problems could take days or longer (maybe years) to complete.&lt;/p&gt;

&lt;p&gt;Today at &lt;a href="http://projecteuler.net/problem=10"&gt;problem 10&lt;/a&gt; this was the case:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Find the sum of all the primes below two million.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A normal &lt;em&gt;brute-force&lt;/em&gt; method just took way too long. On a relatively fast machine using the following method for checking for primes took nearly 22 minutes to run:&lt;/p&gt;

&lt;script src="https://gist.github.com/1978856.js"&gt; &lt;/script&gt;

&lt;p&gt;&lt;strong&gt;Enter-&lt;/strong&gt; Sieve of Eratosthenes : An interestingly fast way to find primes. &lt;/p&gt;

&lt;p&gt;You make a list of all your target numbers, in our case 1 to 2 million, then remove (or filter) all the multiples of numbers starting with two. You keep going removing multiples and your list ends up being only &lt;strong&gt;prime numbers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here's a visualization from the &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_eratosthenes"&gt;wikipedia article&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/Sieve_of_Eratosthenes_animation.gif" alt="Visualization of Sieve of Eratosthenes"&gt;&lt;/p&gt;

&lt;p&gt;Here is a snippet of how I implemented it for the problem. See my whole problem solved &lt;a href="https://github.com/gideondsouza/ProjectEuler_CSharp/blob/master/ProjectEuler/Problems/Problem10.cs"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;script src="https://gist.github.com/1978926.js"&gt; &lt;/script&gt;

&lt;p&gt;&lt;strong&gt;Drum roll!&lt;/strong&gt; It took ~80 millisconds to run!&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;I'm aching to sneak in the time to get to &lt;code&gt;Level 1&lt;/code&gt; which is 25 problems solved! I just got my decathlete badge today.  Unfortunately the badges don't show in the little flair, they only show in your statistics page:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://projecteuler.net/profile/gideondsouza.png" alt="Project Euler Badge"&gt;&lt;/p&gt;

&lt;p&gt;I'm put up all my problems as I go on github. Check it out here:&lt;br&gt;
&lt;a href="https://github.com/gideondsouza/ProjectEuler_CSharp"&gt;https://github.com/gideondsouza/ProjectEuler_CSharp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hopefully this was somewhat insightful. &lt;/p&gt;
</description><pubDate>Mon, 05 Mar 2012 15:52:00 GMT</pubDate><guid isPermaLink="true">http://gideondsouza.com:80/blog/a-superfast-way-to-search-for-prime-numbers</guid></item></channel></rss>