<?xml version="1.0" encoding="windows-1252"?>
<node id="81667" title="Re: RE: sieve of Eratosthenes" created="2001-05-19 00:23:39" updated="2005-07-19 14:08:39">
<type id="11">
note</type>
<author id="79471">
cforde</author>
<data>
<field name="doctext">
In the pursuit of efficiency I have come up with a slightly different implementation: Carl1.
In Carl2 I tried to optimize at the expense of a few more characters.
&lt;p&gt;
On my WinNT PIII 800Mhz machine with ActiveState 522 both of these are faster than ase's Bit Vector implementation.
&lt;p&gt;

&lt;code&gt;
  Carl1:
    @p=(1);for(2..10000){if(!$n[$_]){push@p,$_;for($k=$_*$_;$k&lt;=10000;$n[$k]=1,$k+=$_){}}}

  Carl2:
    @p=(1,2);for($_=3;$_&lt;=10000;$_+=2){if(!$n[$_]){push@p,$_;for($k=$_*$_;$k&lt;=10000;$n[$k]=1,$k+=$_){}}}
&lt;/code&gt;

One thing that I found interesting is that for n less than about 10,000, Carl1 is significantly faster than Carl2.
The loop control overhead must be pretty significant.
&lt;p&gt;
Have fun,&lt;br&gt;
Carl Forde</field>
<field name="root_node">
20633</field>
<field name="parent_node">
20730</field>
</data>
</node>
