Click here to Skip to main content
14,092,036 members
Click here to Skip to main content
Alternatives (4)
Add your own
alternative version

Tagged as

Stats

54K views
8 bookmarked
Posted 19 Jul 2011
Licenced CPOL

Find Prime Numbers in C# Using the Sieve of Eratosthenes

, 3 Oct 2011
This tip illustrates a simple C# console program that uses the Sieve of Eratosthenes algorithm to quickly find prime numbers.

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

12 Sep 2011
Bill Anderson
Here's an alternative. This one uses the BitArray class in C# and does not use the % operator.static List SeiveWithoutMod(int candidate){ BitArray sieveContainer = new BitArray(candidate + 1, true); int marker = 2; //start int factor = 2; //start. sieveContainer[0]...
30 Sep 2011
Bill Anderson
Hi,Thanks for the feedback for alternative #1 from above (and thank you, Jurgen Rohr for your suggestion! I replaced the "reset" line with:while (!sieveContainer.Get(++marker));factor = marker;Here is another algorithm that's slightly different. It's not as elegant an approach, but...
20 Sep 2011
Dennis_E
This solution uses half the space by simply ignoring all even numbers. It will require a little index calculation. Also, primes are generated one by one instead of returning a list.Another optimization can be made by starting the crossing out of multiples of a prime p at p2. Any smaller number...
12 Sep 2011
George Swan
The following method starts by selecting the number 2 and eliminates each multiple of 2 up to N. Then the next valid number is selected and each multiple of it is eliminated. The process is repeated until all valid numbers have been tested. So the first three multiples to be eliminated are...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Brian C Hart
Software Developer (Senior) xyLOGIX, LLC
United States United States
No Biography provided

Pro
| | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190518.1 | Last Updated 3 Oct 2011
Article Copyright 2011 by Brian C Hart
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid