private static void TestGenerator(IPrimeGenerator generator, int testSize) { List <long> correctPrimes = PrimeGeneratorUtility.GetPrimesSimple(testSize); generator.Begin(); while (generator.LastPrime < testSize) { Thread.Sleep(33); } generator.End(); Debug.Assert(correctPrimes.Count <= generator.Primes.Count); for (int i = 0; i < correctPrimes.Count; i++) { Debug.Assert(correctPrimes[i] == generator.Primes[i]); } }
// TODO: Could Skip even numbers private void GenFunction() { bool[] window = new bool[mWindowsSize]; long maximumPrimeinArraySquared = -1; long windowOffset = mWindowsSize; // Use the windowed method to calculate a starting point // TODO: 10 is arbitrary would be better calculate an appropriate window inside the function // Could also consider using a variable sized window that grows as the windowOffset increases mPrimes = PrimeGeneratorUtility.GetPrimesWindowed(mWindowsSize, mWindowsSize / 10); mPrimeCount = mPrimes.Count; while (!mShouldExit) { int maxPrime = (int)Math.Sqrt(windowOffset + mWindowsSize) + 1; Array.Clear(window, 0, mWindowsSize); foreach (long prime in mPrimes) { // don't check primes that have no chance of affecting this window if (prime > maxPrime) { break; } long startIdx = (windowOffset / prime) * prime; if (startIdx < windowOffset) { startIdx += prime; } startIdx -= windowOffset; Debug.Assert(startIdx >= 0); while (startIdx < mWindowsSize) { window[startIdx] = true; startIdx += prime; } } for (int i = 0; i < mWindowsSize; i++) { if (!window[i]) { mLastPrime = windowOffset + i; if (maximumPrimeinArraySquared == -1) { try { mPrimes.Add(mLastPrime); } catch (OutOfMemoryException exception) { // if we can add anymore, our algorithm won't work after this number squared // but we can keep going maximumPrimeinArraySquared = mLastPrime * mLastPrime; } } else if (mLastPrime >= maximumPrimeinArraySquared) { return; } mPrimeCount++; } } windowOffset += mWindowsSize; } }