<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
    <title>JavaScript Rules - sieve</title>
    <link href="http://javascriptrules.com/tag/sieve.xml" rel="self"/>
    <link href="http://javascriptrules.com"/>
    <updated>2012-03-08T18:28:28-08:00</updated>
    <id>http://javascriptrules.com/tag/sieve</id>
    <author>
        <name>Marcel Duran</name>
        <email>marcelduran@gmail.com</email>
    </author>
 
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    <entry>
        <title>Finding prime numbers with Javascript</title>
        <link href="http://javascriptrules.com/2009/10/16/finding-prime-numbers-with-javascript"/>
        <updated>2009-10-16T00:00:00-07:00</updated>
        <id>/2009/10/16/finding-prime-numbers-with-javascript</id>
        <content type="html">&lt;p&gt;A few days ago I introduced a friend of mine Jorge Rocha to &lt;a href='https://www.spoj.pl/'&gt;SPOJ&lt;/a&gt; an online judge system for user-submitted programs, one of the first problems that he tried was the &lt;a href='https://www.spoj.pl/problems/PRIME1/'&gt;Prime Generator&lt;/a&gt; it consisted in finding all primes in a given range of numbers, after some time and few different algorithms he asked me if I had any tips to help him, although he had the correct algorithm &lt;a href='http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes'&gt;(Sieve of Eratosthenes)&lt;/a&gt; something was clearly missing, the solution wasn&amp;#8217;t fast enough and I had no clue where to go from there, so as usual when a question like that comes up I resort to &lt;a href='http://fzort.org/mpr/'&gt;Mauro Persano&lt;/a&gt;&amp;#8230; and obviously he knew the answer, he taught us how to apply a heuristic to the algorithm to help solve the problem and after doing so the code worked and the solution was approved.&lt;/p&gt;

&lt;p&gt;Although I&amp;#8217;m not really into crazy algorithms challenges I thought I&amp;#8217;d give it a try just to see how fast the solution would be in JS, note that most of the solutions submitted are in C/C++ or Java, much faster than JS and to make things worst the interpreter was &lt;a href='http://www.mozilla.org/rhino/'&gt;Rhino&lt;/a&gt;, anyways we went ahead and recreated the solution in Javascript, so now it was time to test it&amp;#8230; a little more tweaking to make it receive inputs via console and we had it running, right away we realized that our output via &lt;a href='http://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29'&gt;stdout&lt;/a&gt; was slowing us down, printing 10 times in a row all primes until 1 billion in the shell is not really fast but it was required to, I knew we had little or no chances to get approved but anyways we tried&amp;#8230; and as I suspected we didn&amp;#8217;t get approved :(, but I decided to test locally in different browsers/engines with and without output.&lt;/p&gt;

&lt;p&gt;Here are the results, I also built a &lt;a href='http://sandbox.javascriptrules.com/prime1/'&gt;test runner&lt;/a&gt; so you can test on your own:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chrome is usually the best, &lt;a href='http://code.google.com/p/v8/'&gt;V8&lt;/a&gt; is blazing fast but it chokes when outputting the results through &lt;a href='http://webkit.org/'&gt;WebKit&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;Rhino does a great job specially with a high number of runs because of &lt;a href='http://en.wikipedia.org/wiki/Java_Virtual_Machine'&gt;JVM&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;Firefox runs nicely with short ranges but stops the script because of the &lt;a href='http://www.nczonline.net/blog/2009/01/05/what-determines-that-a-script-is-long-running/'&gt;long time running&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;Opera wasn&amp;#8217;t fast but it never crashed or stopped&lt;/li&gt;
&lt;/ul&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Browser/Engine&lt;/th&gt;&lt;th&gt;# of runs&lt;/th&gt;&lt;th&gt;Avg(ms) 0 - 100 thousand&lt;/th&gt;&lt;th&gt;Avg(ms) 0 - 1 million&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style='text-align: left;'&gt;Rhino 1.7 *&lt;/td&gt;&lt;td style='text-align: center;'&gt;10&lt;/td&gt;&lt;td style='text-align: right;'&gt;250&lt;/td&gt;&lt;td style='text-align: right;'&gt;2330&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;&lt;td style='text-align: left;'&gt;Firefox 3.5.3 *&lt;/td&gt;&lt;td style='text-align: center;'&gt;10&lt;/td&gt;&lt;td style='text-align: right;'&gt;670&lt;/td&gt;&lt;td style='text-align: right;'&gt;Stopped&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;&lt;td style='text-align: left;'&gt;Chrome 4.0 *&lt;/td&gt;&lt;td style='text-align: center;'&gt;10&lt;/td&gt;&lt;td style='text-align: right;'&gt;32&lt;/td&gt;&lt;td style='text-align: right;'&gt;3050&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;&lt;td style='text-align: left;'&gt;Opera 10 *&lt;/td&gt;&lt;td style='text-align: center;'&gt;10&lt;/td&gt;&lt;td style='text-align: right;'&gt;540&lt;/td&gt;&lt;td style='text-align: right;'&gt;4956&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;&lt;td style='text-align: left;'&gt;Rhino 1.7&lt;/td&gt;&lt;td style='text-align: center;'&gt;100&lt;/td&gt;&lt;td style='text-align: right;'&gt;34&lt;/td&gt;&lt;td style='text-align: right;'&gt;304&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;&lt;td style='text-align: left;'&gt;Firefox 3.5.3&lt;/td&gt;&lt;td style='text-align: center;'&gt;100&lt;/td&gt;&lt;td style='text-align: right;'&gt;20&lt;/td&gt;&lt;td style='text-align: right;'&gt;Stopped&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;&lt;td style='text-align: left;'&gt;Chrome 4.0&lt;/td&gt;&lt;td style='text-align: center;'&gt;100&lt;/td&gt;&lt;td style='text-align: right;'&gt;9&lt;/td&gt;&lt;td style='text-align: right;'&gt;128&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;&lt;td style='text-align: left;'&gt;Opera 10&lt;/td&gt;&lt;td style='text-align: center;'&gt;100&lt;/td&gt;&lt;td style='text-align: right;'&gt;100&lt;/td&gt;&lt;td style='text-align: right;'&gt;1213&lt;/td&gt;
&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;em&gt;* With output (tested under &lt;a href='http://www.ubuntu.com/'&gt;Ubuntu 9.04 Jaunty&lt;/a&gt;)&lt;/em&gt;&lt;/p&gt;</content>
    </entry>
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
 
</feed>

