<br><br><div class="gmail_quote">On Mon, Nov 2, 2009 at 1:59 PM, Will Ness <span dir="ltr">&lt;<a href="mailto:will_n48@yahoo.com">will_n48@yahoo.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">Will Ness &lt;will_n48 &lt;at&gt; <a href="http://yahoo.com" target="_blank">yahoo.com</a>&gt; writes:<br>
<br>
</div><div class="im">&gt; primes   = 2: 3: sieve 0 primes&#39; 5<br>
&gt; primes&#39;  = tail primes<br>
&gt; sieve k (p:ps) x<br>
&gt;          = [x | x &lt;- [x,x+2..p*p-2],<br>
&gt;                  and [(x`rem`p)/=0 | p &lt;- take k primes&#39;]]<br>
&gt;            ++ sieve (k+1) ps (p*p+2)<br>
&gt;<br>
&gt; (thanks to Leon P.Smith for his brilliant idea of directly generating<br>
&gt; the spans of odds instead of calling &#39;span&#39; on the infinite odds list).<br>
&gt;<br>
</div><div class="im">&gt; The relative performance vs the PQ-version is:<br>
&gt;<br>
&gt;   100,000   300,000   1,000,000  primes produced<br>
&gt;    0.6       0.75      0.97<br>
&gt;<br>
<br>
</div>One _crucial_ tidbit I&#39;ve left out: _type_signature_.<br>
<br>
Adding (:: [Int]) speeds this code up more than TWICE!<br>
<br>
:) :)<br></blockquote><div><br></div><div>If you are okay with Int, then maybe you&#39;re also happy with Int32 or Word32.  If so, why don&#39;t you use template haskell to build the list at compile time?  If you do that, then getting the kth prime at run-time is O(k).  Take that AKS!  :)</div>
<div><br></div><div>Jason</div></div>