私は実際に私の質問に答えているが、並列化されていないので、アルゴリズムを改善する方法に興味がある。とにかく、それはある人にとっては現状のままで役に立つかもしれません。C#で素数を計算する最速の方法は?
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);
たぶん私はそれらを一緒に複数のBitArray
Sを使用してBitArray.And()だろうか?
これは更新されたコードですか? – Crash893
私がC#でマルチプロセッシングを使用することを知っている最速の方法は、別の質問への回答として提出されたコードです:http://stackoverflow.com/a/18885065/549617。それは約0.32秒で10億の総数、約1.29秒で32ビットの数の範囲内の素数の数、素数の数は約3秒で100億になる**インテル®i7-2700K(3.5GHz、4コア/ハイパースレッディングを含む8スレッド)で列挙します。これより速く結果を出すには、https://code.google.com/p/primesieve/のようにCコードを使用する必要があります。 – GordonBGood
私は上記のソリューションを試しましたが、例外があります。「算術演算でオーバーフローが発生しました」。 Listはリストである必要があります。 –
Devid