inline void MarkMultiples(BoolArray<>& sieve, const size_t n, const size_t i) { size_t cur = i + ((i + 1) * n); // Start at the square of the number. while (cur < sieve.Size()) { // Since we're multiplying two odd numbers, the result is always odd, and thus in our sieve. sieve.ClearBit(cur); cur += n; } }
std::vector<size_t> PrimeFinder::FindPrimes(const size_t until) { // Find prime number using the sieve of Eratosthenes BoolArray<> sieve = GetSieve(until); size_t expected = GetExpectedPrimeCount(until); // Check the rest of the range (from sqrt(until) to until) for primes: std::vector<size_t> primes; primes.reserve(expected); primes.push_back(2); // Add 2, the only even prime number, and therefore the only one not in our sieve. for (size_t i = 0; i < sieve.Size(); i++) if (sieve[i]) primes.push_back(GetNumberAtIndex(i)); return primes; }