<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Sieve of Eratosthenes</title>
	<atom:link href="http://programmingpraxis.com/2009/02/19/sieve-of-eratosthenes/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2009/02/19/sieve-of-eratosthenes/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Mon, 28 May 2012 03:30:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: j0sejuan</title>
		<link>http://programmingpraxis.com/2009/02/19/sieve-of-eratosthenes/#comment-4657</link>
		<dc:creator><![CDATA[j0sejuan]]></dc:creator>
		<pubDate>Sat, 07 Apr 2012 16:53:51 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=21#comment-4657</guid>
		<description><![CDATA[[sourcecode lang=&quot;perl&quot;]
package main

import &quot;fmt&quot;

func PrimesLessEqual(n int) [] int {
	c, p, w, l := 0, 2, make([] bool, n + 1), make([] int, n)
	for ; p &lt;= n; p++ {
		for p &lt;= n &amp;&amp; w[p] { p++ }
		if p &lt;= n {
			l[c] = p; c++
			for k := p; k &lt;= n; k += p { w[k] = true }
		}
	}
	return l[0:c]
}

func main() {
	fmt.Println(len(PrimesLessEqual(15485863)))
}
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<pre class="brush: perl;">
package main

import &quot;fmt&quot;

func PrimesLessEqual(n int) [] int {
	c, p, w, l := 0, 2, make([] bool, n + 1), make([] int, n)
	for ; p &lt;= n; p++ {
		for p &lt;= n &amp;&amp; w[p] { p++ }
		if p &lt;= n {
			l[c] = p; c++
			for k := p; k &lt;= n; k += p { w[k] = true }
		}
	}
	return l[0:c]
}

func main() {
	fmt.Println(len(PrimesLessEqual(15485863)))
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: gary capell</title>
		<link>http://programmingpraxis.com/2009/02/19/sieve-of-eratosthenes/#comment-4336</link>
		<dc:creator><![CDATA[gary capell]]></dc:creator>
		<pubDate>Sat, 11 Feb 2012 23:27:49 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=21#comment-4336</guid>
		<description><![CDATA[https://github.com/gcapell/ProgrammingPraxis/blob/master/02_sieve_of_eratosthenes/sieve.go]]></description>
		<content:encoded><![CDATA[<p><a href="https://github.com/gcapell/ProgrammingPraxis/blob/master/02_sieve_of_eratosthenes/sieve.go" rel="nofollow">https://github.com/gcapell/ProgrammingPraxis/blob/master/02_sieve_of_eratosthenes/sieve.go</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ikenna</title>
		<link>http://programmingpraxis.com/2009/02/19/sieve-of-eratosthenes/#comment-4125</link>
		<dc:creator><![CDATA[Ikenna]]></dc:creator>
		<pubDate>Sat, 31 Dec 2011 00:16:18 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=21#comment-4125</guid>
		<description><![CDATA[^
&#124;
&#124;

Didn&#039;t post correctly]]></description>
		<content:encoded><![CDATA[<p>^<br />
|<br />
|</p>
<p>Didn&#8217;t post correctly</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ikenna</title>
		<link>http://programmingpraxis.com/2009/02/19/sieve-of-eratosthenes/#comment-4124</link>
		<dc:creator><![CDATA[Ikenna]]></dc:creator>
		<pubDate>Sat, 31 Dec 2011 00:15:07 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=21#comment-4124</guid>
		<description><![CDATA[Using python:

from math import *

def sieve2(n):
    index = 0
    numberList = range(3,n+1,2)
    while numberList[index] = sqrt(n):
            break
    return [2] + numberList]]></description>
		<content:encoded><![CDATA[<p>Using python:</p>
<p>from math import *</p>
<p>def sieve2(n):<br />
    index = 0<br />
    numberList = range(3,n+1,2)<br />
    while numberList[index] = sqrt(n):<br />
            break<br />
    return [2] + numberList</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias Altmann</title>
		<link>http://programmingpraxis.com/2009/02/19/sieve-of-eratosthenes/#comment-4048</link>
		<dc:creator><![CDATA[Matthias Altmann]]></dc:creator>
		<pubDate>Fri, 16 Dec 2011 17:15:52 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=21#comment-4048</guid>
		<description><![CDATA[I tried another solution in C++ using 2 threads, one running upwards on the numbers, the other running downwards. They stop if the reach another.
The solution is correct, but the performance decreases severly.

Here is my code:

#include 
#include 
#include 
#include 
#include 
using namespace std;

const unsigned long  MAX_NUMBER = 15485863;
vector numbers(MAX_NUMBER+1);
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
unsigned long l1,l2;

// thread specific arguments
struct thread_data
{
  unsigned int thread_id;
  unsigned long n; // data to sieve
  bool bw; // backward or forward search
};

struct thread_data thread_data_array[2];

void init_numbers(vector  * numbers);

void *prime_sieve(void *arg);

void init_numbers(vector  * numbers)
{
  unsigned long i;
  (*numbers)[2]=true;
  for (i=3;ibw))
    {
      while (l1 &lt;= l2)
        {
          if (numbers[l1] != false)
            {
              c = l1*l1;
              pos_move = (l1 &lt;&lt; 1);
              while (c n)
                {
                  pthread_mutex_lock(&amp;mutex1);
                  numbers[c]=false;
                  pthread_mutex_unlock(&amp;mutex1);
                  c += pos_move;
                }
            }
          pthread_mutex_lock(&amp;mutex1);
          l1++;
          pthread_mutex_unlock(&amp;mutex1);
        }
    }
  else
    {
      while(l2 &gt;= l1)
        {
          if (numbers[l2] != false)
            {
              c = l2*l2;
              pos_move = (l2 &lt;&lt; 1);
              while (c n)
                {
                  pthread_mutex_lock(&amp;mutex1);
                  numbers[c]=false;
                  pthread_mutex_unlock(&amp;mutex1);
                  c += pos_move;
                }
            }
          pthread_mutex_lock(&amp;mutex1);
          l2--;
          pthread_mutex_unlock(&amp;mutex1);
        }
    }
  pthread_exit(NULL);
}

unsigned long get_primes(vector  numbers, unsigned long n)
{
  unsigned long primes=0;
  for(unsigned int i=0;i&lt;=n;i++)
    {
      primes+=numbers[i];
    }
  return primes;
}

int main(int argc, char *argv[])
{
  pthread_t threads[2];
  int rc;
  unsigned long n = MAX_NUMBER;
  init_numbers(&amp;numbers);

  l1 = 2;
  l2 = (int)(sqrt(n))+1;

  thread_data_array[0].thread_id = 0;
  thread_data_array[0].n = MAX_NUMBER;
  thread_data_array[0].bw = false;
  rc = pthread_create(&amp;threads[0], NULL, prime_sieve, (void *) &amp;thread_data_array[0]);

  thread_data_array[1].thread_id = 1;
  thread_data_array[1].n = MAX_NUMBER;
  thread_data_array[1].bw = true;
  rc = pthread_create(&amp;threads[1], NULL, prime_sieve, (void *) &amp;thread_data_array[1]);

  void* retval;
  pthread_join(threads[0], &amp;retval);
  pthread_join(threads[1], &amp;retval);

  cout &lt;&lt; get_primes(numbers, n) &lt;&lt; endl;
}]]></description>
		<content:encoded><![CDATA[<p>I tried another solution in C++ using 2 threads, one running upwards on the numbers, the other running downwards. They stop if the reach another.<br />
The solution is correct, but the performance decreases severly.</p>
<p>Here is my code:</p>
<p>#include<br />
#include<br />
#include<br />
#include<br />
#include<br />
using namespace std;</p>
<p>const unsigned long  MAX_NUMBER = 15485863;<br />
vector numbers(MAX_NUMBER+1);<br />
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;<br />
unsigned long l1,l2;</p>
<p>// thread specific arguments<br />
struct thread_data<br />
{<br />
  unsigned int thread_id;<br />
  unsigned long n; // data to sieve<br />
  bool bw; // backward or forward search<br />
};</p>
<p>struct thread_data thread_data_array[2];</p>
<p>void init_numbers(vector  * numbers);</p>
<p>void *prime_sieve(void *arg);</p>
<p>void init_numbers(vector  * numbers)<br />
{<br />
  unsigned long i;<br />
  (*numbers)[2]=true;<br />
  for (i=3;ibw))<br />
    {<br />
      while (l1 &lt;= l2)<br />
        {<br />
          if (numbers[l1] != false)<br />
            {<br />
              c = l1*l1;<br />
              pos_move = (l1 &lt;&lt; 1);<br />
              while (c n)<br />
                {<br />
                  pthread_mutex_lock(&amp;mutex1);<br />
                  numbers[c]=false;<br />
                  pthread_mutex_unlock(&amp;mutex1);<br />
                  c += pos_move;<br />
                }<br />
            }<br />
          pthread_mutex_lock(&amp;mutex1);<br />
          l1++;<br />
          pthread_mutex_unlock(&amp;mutex1);<br />
        }<br />
    }<br />
  else<br />
    {<br />
      while(l2 &gt;= l1)<br />
        {<br />
          if (numbers[l2] != false)<br />
            {<br />
              c = l2*l2;<br />
              pos_move = (l2 &lt;&lt; 1);<br />
              while (c n)<br />
                {<br />
                  pthread_mutex_lock(&amp;mutex1);<br />
                  numbers[c]=false;<br />
                  pthread_mutex_unlock(&amp;mutex1);<br />
                  c += pos_move;<br />
                }<br />
            }<br />
          pthread_mutex_lock(&amp;mutex1);<br />
          l2&#8211;;<br />
          pthread_mutex_unlock(&amp;mutex1);<br />
        }<br />
    }<br />
  pthread_exit(NULL);<br />
}</p>
<p>unsigned long get_primes(vector  numbers, unsigned long n)<br />
{<br />
  unsigned long primes=0;<br />
  for(unsigned int i=0;i&lt;=n;i++)<br />
    {<br />
      primes+=numbers[i];<br />
    }<br />
  return primes;<br />
}</p>
<p>int main(int argc, char *argv[])<br />
{<br />
  pthread_t threads[2];<br />
  int rc;<br />
  unsigned long n = MAX_NUMBER;<br />
  init_numbers(&amp;numbers);</p>
<p>  l1 = 2;<br />
  l2 = (int)(sqrt(n))+1;</p>
<p>  thread_data_array[0].thread_id = 0;<br />
  thread_data_array[0].n = MAX_NUMBER;<br />
  thread_data_array[0].bw = false;<br />
  rc = pthread_create(&amp;threads[0], NULL, prime_sieve, (void *) &amp;thread_data_array[0]);</p>
<p>  thread_data_array[1].thread_id = 1;<br />
  thread_data_array[1].n = MAX_NUMBER;<br />
  thread_data_array[1].bw = true;<br />
  rc = pthread_create(&amp;threads[1], NULL, prime_sieve, (void *) &amp;thread_data_array[1]);</p>
<p>  void* retval;<br />
  pthread_join(threads[0], &amp;retval);<br />
  pthread_join(threads[1], &amp;retval);</p>
<p>  cout &lt;&lt; get_primes(numbers, n) &lt;&lt; endl;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: willy1234x1</title>
		<link>http://programmingpraxis.com/2009/02/19/sieve-of-eratosthenes/#comment-4005</link>
		<dc:creator><![CDATA[willy1234x1]]></dc:creator>
		<pubDate>Sat, 10 Dec 2011 10:07:33 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=21#comment-4005</guid>
		<description><![CDATA[A rather simple version of a Python solution.
&lt;code&gt;
def primes_sieve(limit):
    limit += 1
    not_prime = [False] * limit
    primes = []

    for i in xrange(2, limit):
        if not_prime[i]:
            continue
        for j in xrange(i*2, limit, i):
            not_prime[j] = True
        
        primes.append(i)
    
    return primes


if __name__ == &quot;__main__&quot;:
    print len(primes_sieve(15485863))
&lt;/code&gt;]]></description>
		<content:encoded><![CDATA[<p>A rather simple version of a Python solution.<br />
<code><br />
def primes_sieve(limit):<br />
    limit += 1<br />
    not_prime = [False] * limit<br />
    primes = []</p>
<p>    for i in xrange(2, limit):<br />
        if not_prime[i]:<br />
            continue<br />
        for j in xrange(i*2, limit, i):<br />
            not_prime[j] = True</p>
<p>        primes.append(i)</p>
<p>    return primes</p>
<p>if __name__ == "__main__":<br />
    print len(primes_sieve(15485863))<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: moink</title>
		<link>http://programmingpraxis.com/2009/02/19/sieve-of-eratosthenes/#comment-3809</link>
		<dc:creator><![CDATA[moink]]></dc:creator>
		<pubDate>Sat, 29 Oct 2011 15:55:38 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=21#comment-3809</guid>
		<description><![CDATA[Here&#039;s a Matlab solution:

[sourcecode lang=&quot;cpp&quot;]
function primes=sieve(maxnum)
%Sieve of Erasthenes for finding primes less than or equal to the input
%argument
mask=ones(ceil((maxnum-1)/2),1);
for i=3:2:floor(sqrt(maxnum))
   if mask((i-1)/2)
        mask((i^2-1)/2:i:end)=0;
   end
end
primes=[2;2*find(mask)+1];
[/sourcecode]

It&#039;s a tiny bit faster than the nearly-identical Matlab implementation, called primes.]]></description>
		<content:encoded><![CDATA[<p>Here&#8217;s a Matlab solution:</p>
<pre class="brush: cpp;">
function primes=sieve(maxnum)
%Sieve of Erasthenes for finding primes less than or equal to the input
%argument
mask=ones(ceil((maxnum-1)/2),1);
for i=3:2:floor(sqrt(maxnum))
   if mask((i-1)/2)
        mask((i^2-1)/2:i:end)=0;
   end
end
primes=[2;2*find(mask)+1];
</pre>
<p>It&#8217;s a tiny bit faster than the nearly-identical Matlab implementation, called primes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: CyberSpace17</title>
		<link>http://programmingpraxis.com/2009/02/19/sieve-of-eratosthenes/#comment-3774</link>
		<dc:creator><![CDATA[CyberSpace17]]></dc:creator>
		<pubDate>Sat, 22 Oct 2011 02:08:46 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=21#comment-3774</guid>
		<description><![CDATA[My C++ solution where a &quot;crossing out&quot; means &quot;made 0&quot;
http://ideone.com/fdmux]]></description>
		<content:encoded><![CDATA[<p>My C++ solution where a &#8220;crossing out&#8221; means &#8220;made 0&#8243;<br />
<a href="http://ideone.com/fdmux" rel="nofollow">http://ideone.com/fdmux</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matías Giovannini</title>
		<link>http://programmingpraxis.com/2009/02/19/sieve-of-eratosthenes/#comment-3637</link>
		<dc:creator><![CDATA[Matías Giovannini]]></dc:creator>
		<pubDate>Thu, 22 Sep 2011 16:42:50 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=21#comment-3637</guid>
		<description><![CDATA[An imperative OCaml Batteries &lt;code&gt;BitSet&lt;/code&gt;-based version, without multiplications, only additions, subtractions and shifts:

[sourcecode lang=&quot;fsharp&quot;]
let sieve_of_eratosthenes (proc : int -&gt; unit) n =
  let limit = (n - 3) asr 1 in
  let sieve = BitSet.create_full (limit + 1) in
  let i = ref 0
  and p = ref 3
  and q = ref 9 in
  while !q &lt; n do
    if BitSet.is_set sieve !i then begin
      let j = ref ((!q - 3) asr 1) in
      while !j &lt;= limit do
        BitSet.unset sieve !j;
        j := !j + !p
      done
    end;
    incr i;
    p := !p + 2;
    q := !q + (!i + 1) lsl 3
  done;
  proc 2;
  foreach (BitSet.enum sieve) (fun i -&gt; if i &lt;= limit then proc (i lsl 1 + 3))
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>An imperative OCaml Batteries <code>BitSet</code>-based version, without multiplications, only additions, subtractions and shifts:</p>
<pre class="brush: fsharp;">
let sieve_of_eratosthenes (proc : int -&gt; unit) n =
  let limit = (n - 3) asr 1 in
  let sieve = BitSet.create_full (limit + 1) in
  let i = ref 0
  and p = ref 3
  and q = ref 9 in
  while !q &lt; n do
    if BitSet.is_set sieve !i then begin
      let j = ref ((!q - 3) asr 1) in
      while !j &lt;= limit do
        BitSet.unset sieve !j;
        j := !j + !p
      done
    end;
    incr i;
    p := !p + 2;
    q := !q + (!i + 1) lsl 3
  done;
  proc 2;
  foreach (BitSet.enum sieve) (fun i -&gt; if i &lt;= limit then proc (i lsl 1 + 3))
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: kawas</title>
		<link>http://programmingpraxis.com/2009/02/19/sieve-of-eratosthenes/#comment-3545</link>
		<dc:creator><![CDATA[kawas]]></dc:creator>
		<pubDate>Tue, 06 Sep 2011 22:01:06 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.wordpress.com/?p=21#comment-3545</guid>
		<description><![CDATA[Another clojure version,
because the first one die on me with OutOfMemoryError to compute (eratosthenes 15485863)

[sourcecode lang=&quot;css&quot;]
; we work only with odd numbers till N
(defn odds [n] (vec (range 3 (inc n) 2)))

; it&#039;s easy to calculate index of a value in our vector of odds
(defn odds-idx [v] (/ (- v 3) 2))

; just sieve the vector from start=(index of v²)
(defn sieve [max-idx nums v]
  (let [start (odds-idx (* v v))]
    (reduce #(assoc %1 %2 nil) nums (range start max-idx v))))

; get primes equal or less than N
(defn primes [n]
  (let [max-i (Math/floor (odds-idx n))]
    (loop [nums (odds n) i 0]
      (if-let [v (nums i)]
        (if (&gt; (* v v) n)
          (cons 2 (remove nil? nums))
          (recur (sieve max-i nums v) (inc i)))
        (recur nums (inc i))))))

(count (primes 15485863))
1000000
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Another clojure version,<br />
because the first one die on me with OutOfMemoryError to compute (eratosthenes 15485863)</p>
<pre class="brush: css;">
; we work only with odd numbers till N
(defn odds [n] (vec (range 3 (inc n) 2)))

; it's easy to calculate index of a value in our vector of odds
(defn odds-idx [v] (/ (- v 3) 2))

; just sieve the vector from start=(index of v²)
(defn sieve [max-idx nums v]
  (let [start (odds-idx (* v v))]
    (reduce #(assoc %1 %2 nil) nums (range start max-idx v))))

; get primes equal or less than N
(defn primes [n]
  (let [max-i (Math/floor (odds-idx n))]
    (loop [nums (odds n) i 0]
      (if-let [v (nums i)]
        (if (&gt; (* v v) n)
          (cons 2 (remove nil? nums))
          (recur (sieve max-i nums v) (inc i)))
        (recur nums (inc i))))))

(count (primes 15485863))
1000000
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

