<?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/"
> <channel><title>Comments on: Sieve of Eratosthenes in C#</title> <atom:link href="http://digitalbush.com/2010/02/26/sieve-of-eratosthenes-in-csharp/feed/" rel="self" type="application/rss+xml" /><link>http://digitalbush.com/2010/02/26/sieve-of-eratosthenes-in-csharp/</link> <description>Tales of a Tormented Software Developer</description> <lastBuildDate>Thu, 19 Apr 2012 15:22:59 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.2.1</generator> <item><title>By: Atomic Paradox</title><link>http://digitalbush.com/2010/02/26/sieve-of-eratosthenes-in-csharp/comment-page-1/#comment-3262</link> <dc:creator>Atomic Paradox</dc:creator> <pubDate>Sat, 10 Dec 2011 15:24:24 +0000</pubDate> <guid
isPermaLink="false">http://digitalbush.com/?p=643#comment-3262</guid> <description>I&#039;m back! I had another look to see if I could optimise it any more. The speed is not markedly quicker but I think the optimisations will improve as max increases. The approach I took was as follows:I split the outer for loop into two while loops. The first while loop deals with all values of i &lt; = maxSquareRoot. This way you can remove the test before the inner for loop of your original code.The second while loop then just harvests the flagged primes remaining in the sieve.In addition I calculated i2 = 2 x i outside the for loop rather than recalculate every time we go around the inner for loops.
///
///	Use the Sieve of Eratosthenes to compile and return a list of prime numbers up to max
///	Inspired by Josh Bush
///	http://digitalbush.com/2010/02/26/sieve-of-eratosthenes-in-csharp/comment-page-1/#comment-3261
///
/// Maximum prime number in the list.
/// List of prime numbers in the range [2..max].
static public IList CachePrimesE( int max )
{
IList vals = new List( (int)(max / (Math.Log( max ) - 1.08366)) );  // Create the list to hold the primes
int maxSquareRoot = (int)Math.Sqrt( max );
BitArray eliminated = new BitArray( max + 2 );									// Create the bit array for our sieve// Add the first two prime numbers
vals.Add( 2 );
vals.Add( 3 );// All primes greater than 3 can be written in the form  6k +/- 1.
// If we make i = 6k-1 then we process i and i + 2 through each iteration of the while loops!
int i = 5;
while(i &lt;= maxSquareRoot)
{
// Process i
if ( !eliminated[i] )
{
for ( int j = i * i, i2 = i + i; j &lt;= max; j += i2 )
eliminated[j] = true;
vals.Add( (uint)i );	// Add i to the prime number list
}
// Process i + 2
i += 2;
if ( !eliminated[i] )
{
for ( int j = i * i, i2 = i + i; j = SQR(max) so simply harvest the remaining primes in the sieve
while(i &lt;= max)
{
if ( !eliminated[i] )
vals.Add( (uint)i );	// Add i to the prime number list
i += 2;
if ( !eliminated[i] )
vals.Add( (uint)i );	// Add i to the prime number list
i += 4;
}
Console.WriteLine( vals.Count );
return vals;
}</description> <content:encoded><![CDATA[<p>I&#8217;m back! I had another look to see if I could optimise it any more. The speed is not markedly quicker but I think the optimisations will improve as max increases. The approach I took was as follows:</p><p>I split the outer for loop into two while loops. The first while loop deals with all values of i &lt; = maxSquareRoot. This way you can remove the test before the inner for loop of your original code.</p><p>The second while loop then just harvests the flagged primes remaining in the sieve.</p><p>In addition I calculated i2 = 2 x i outside the for loop rather than recalculate every time we go around the inner for loops.</p><p>///<br
/> ///	Use the Sieve of Eratosthenes to compile and return a list of prime numbers up to max<br
/> ///	Inspired by Josh Bush<br
/> /// <a
href="http://digitalbush.com/2010/02/26/sieve-of-eratosthenes-in-csharp/comment-page-1/#comment-3261" rel="nofollow">http://digitalbush.com/2010/02/26/sieve-of-eratosthenes-in-csharp/comment-page-1/#comment-3261</a><br
/> ///<br
/> /// Maximum prime number in the list.<br
/> /// List of prime numbers in the range [2..max].<br
/> static public IList CachePrimesE( int max )<br
/> {<br
/> IList vals = new List( (int)(max / (Math.Log( max ) &#8211; 1.08366)) );  // Create the list to hold the primes<br
/> int maxSquareRoot = (int)Math.Sqrt( max );<br
/> BitArray eliminated = new BitArray( max + 2 );									// Create the bit array for our sieve</p><p> // Add the first two prime numbers<br
/> vals.Add( 2 );<br
/> vals.Add( 3 );</p><p> // All primes greater than 3 can be written in the form  6k +/- 1.<br
/> // If we make i = 6k-1 then we process i and i + 2 through each iteration of the while loops!<br
/> int i = 5;<br
/> while(i &lt;= maxSquareRoot)<br
/> {<br
/> // Process i<br
/> if ( !eliminated[i] )<br
/> {<br
/> for ( int j = i * i, i2 = i + i; j &lt;= max; j += i2 )<br
/> eliminated[j] = true;<br
/> vals.Add( (uint)i );	// Add i to the prime number list<br
/> }</p><p> // Process i + 2<br
/> i += 2;<br
/> if ( !eliminated[i] )<br
/> {<br
/> for ( int j = i * i, i2 = i + i; j = SQR(max) so simply harvest the remaining primes in the sieve<br
/> while(i &lt;= max)<br
/> {<br
/> if ( !eliminated[i] )<br
/> vals.Add( (uint)i );	// Add i to the prime number list<br
/> i += 2;<br
/> if ( !eliminated[i] )<br
/> vals.Add( (uint)i );	// Add i to the prime number list<br
/> i += 4;<br
/> }<br
/> Console.WriteLine( vals.Count );<br
/> return vals;<br
/> }</p> ]]></content:encoded> </item> <item><title>By: Atomic Paradox</title><link>http://digitalbush.com/2010/02/26/sieve-of-eratosthenes-in-csharp/comment-page-1/#comment-3261</link> <dc:creator>Atomic Paradox</dc:creator> <pubDate>Sat, 10 Dec 2011 13:35:48 +0000</pubDate> <guid
isPermaLink="false">http://digitalbush.com/?p=643#comment-3261</guid> <description>Hi Josh,Nice bit of coding! :) Taking up the challenge  I added another optimisation that you might want to play around with. On my PC it went about 20% faster caching all primes &lt;= 100M.Another area I looked at was the maxSquareRoot assignment which I believe compiles to a double. Any later comparisons will take that lil&#039; bit longer to process than if you use an int.Here is the revised code:static public IList CachePrimesE( int max )
{
List vals = new List( (int)(max / (Math.Log( max ) - 1.08366)) );
int maxSquareRoot = (int)Math.Sqrt( max );
BitArray eliminated = new BitArray( max + 2 );// Add the first prime number
vals.Add( 2 );
vals.Add( 3 );
vals.Add( 5 );// All primes greater than 3 can be written in the form  6k +/- 1. If we make i = 6k-1 then we process i and i + 2!
for ( int i = 5; i &lt;= max; i += 4 )
{
// Process i
if ( !eliminated[i] )
{
if ( i &lt; maxSquareRoot )
{
for ( int j = i * i; j &lt;= max; j += 2 * i )
eliminated[j] = true;
}
vals.Add( (uint)i );
}
// Process i + 2
i += 2;
if ( !eliminated[i] )
{
if ( i &lt; maxSquareRoot )
{
for ( int j = i * i; j &lt;= max; j += 2 * i )
eliminated[j] = true;
}
vals.Add( (uint)i );
}}
return vals;
}</description> <content:encoded><![CDATA[<p>Hi Josh,</p><p>Nice bit of coding! <img
src='http://digitalbush.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> Taking up the challenge  I added another optimisation that you might want to play around with. On my PC it went about 20% faster caching all primes &lt;= 100M.</p><p>Another area I looked at was the maxSquareRoot assignment which I believe compiles to a double. Any later comparisons will take that lil&#039; bit longer to process than if you use an int.</p><p>Here is the revised code:</p><p>static public IList CachePrimesE( int max )<br
/> {<br
/> List vals = new List( (int)(max / (Math.Log( max ) &#8211; 1.08366)) );<br
/> int maxSquareRoot = (int)Math.Sqrt( max );<br
/> BitArray eliminated = new BitArray( max + 2 );</p><p> // Add the first prime number<br
/> vals.Add( 2 );<br
/> vals.Add( 3 );<br
/> vals.Add( 5 );</p><p> // All primes greater than 3 can be written in the form  6k +/- 1. If we make i = 6k-1 then we process i and i + 2!<br
/> for ( int i = 5; i &lt;= max; i += 4 )<br
/> {<br
/> // Process i<br
/> if ( !eliminated[i] )<br
/> {<br
/> if ( i &lt; maxSquareRoot )<br
/> {<br
/> for ( int j = i * i; j &lt;= max; j += 2 * i )<br
/> eliminated[j] = true;<br
/> }<br
/> vals.Add( (uint)i );<br
/> }</p><p> // Process i + 2<br
/> i += 2;<br
/> if ( !eliminated[i] )<br
/> {<br
/> if ( i &lt; maxSquareRoot )<br
/> {<br
/> for ( int j = i * i; j &lt;= max; j += 2 * i )<br
/> eliminated[j] = true;<br
/> }<br
/> vals.Add( (uint)i );<br
/> }</p><p> }</p><p> return vals;<br
/> }</p> ]]></content:encoded> </item> <item><title>By: SMM</title><link>http://digitalbush.com/2010/02/26/sieve-of-eratosthenes-in-csharp/comment-page-1/#comment-3259</link> <dc:creator>SMM</dc:creator> <pubDate>Fri, 09 Dec 2011 16:59:53 +0000</pubDate> <guid
isPermaLink="false">http://digitalbush.com/?p=643#comment-3259</guid> <description>@Mike @ August 27th, 2010:You may want to test more thoroughly before you stick your foot in your mouth.  Your algorithm was slightly faster with smaller values of &#039;target&#039;, but as that number grew, your &#039;superior&#039; version was slower.  This was tested on multiple boxes by a few people.How do you like &#039;dem apples?</description> <content:encoded><![CDATA[<p>@Mike @ August 27th, 2010:</p><p>You may want to test more thoroughly before you stick your foot in your mouth.  Your algorithm was slightly faster with smaller values of &#8216;target&#8217;, but as that number grew, your &#8216;superior&#8217; version was slower.  This was tested on multiple boxes by a few people.</p><p>How do you like &#8216;dem apples?</p> ]]></content:encoded> </item> <item><title>By: Mike</title><link>http://digitalbush.com/2010/02/26/sieve-of-eratosthenes-in-csharp/comment-page-1/#comment-3003</link> <dc:creator>Mike</dc:creator> <pubDate>Fri, 27 Aug 2010 13:51:29 +0000</pubDate> <guid
isPermaLink="false">http://digitalbush.com/?p=643#comment-3003</guid> <description>Nice work, Josh, but my version is consistently faster. For example, to calculate all primes to 1,000,000,000 your version takes about 36 seconds, mine around 31. Owned!public static List Sieve2(int target)
{
var isPrime = new BitArray(target, true);
var primesList = new List { 2 };
var squareRoot = Math.Sqrt(target);for (int candidate = 3; candidate &lt; target; candidate +=2)
{
if (!isPrime[candidate]) continue;if (candidate &lt; squareRoot)
{
for (int multiple = candidate*candidate; multiple &lt; target; multiple += 2*candidate)
{
isPrime[multiple] = false;
}
}
primesList.Add(candidate);
}
return primesList;
}</description> <content:encoded><![CDATA[<p>Nice work, Josh, but my version is consistently faster. For example, to calculate all primes to 1,000,000,000 your version takes about 36 seconds, mine around 31. Owned!</p><p>public static List Sieve2(int target)<br
/> {<br
/> var isPrime = new BitArray(target, true);<br
/> var primesList = new List { 2 };<br
/> var squareRoot = Math.Sqrt(target);</p><p> for (int candidate = 3; candidate &lt; target; candidate +=2)<br
/> {<br
/> if (!isPrime[candidate]) continue;</p><p> if (candidate &lt; squareRoot)<br
/> {<br
/> for (int multiple = candidate*candidate; multiple &lt; target; multiple += 2*candidate)<br
/> {<br
/> isPrime[multiple] = false;<br
/> }<br
/> }<br
/> primesList.Add(candidate);<br
/> }<br
/> return primesList;<br
/> }</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 5/11 queries in 0.006 seconds using apc

Served from: _ @ 2012-05-21 12:51:58 -->
