pal*_*asb 10 .net c# algorithm performance bitarray
我实际上有一个问题的答案,但它没有并行化,所以我对改进算法的方法很感兴趣.无论如何,它对某些人来说可能是有用的.
int Until = 20000000;
BitArray PrimeBits = new BitArray(Until, true);
/*
* Sieve of Eratosthenes
* PrimeBits is a simple BitArray where all bit is an integer
* and we mark composite numbers as false
*/
PrimeBits.Set(0, false); // You don't actually need this, just
PrimeBits.Set(1, false); // remindig you that 2 is the smallest prime
for (int P = 2; P < (int)Math.Sqrt(Until) + 1; P++)
if (PrimeBits.Get(P))
// These are going to be the multiples of P if it is a prime
for (int PMultiply = P * 2; PMultiply < Until; PMultiply += P)
PrimeBits.Set(PMultiply, false);
// We use this to store the actual prime numbers
List<int> Primes = new List<int>();
for (int i = 2; i < Until; i++)
if (PrimeBits.Get(i))
Primes.Add(i);
Run Code Online (Sandbox Code Playgroud)
也许我可以将多个BitArray
s和BitArray.And()一起使用?
归档时间: |
|
查看次数: |
8456 次 |
最近记录: |