예제 #1
0
uint32_t Sieve(uint32_t PrimeCeiling, uint32_t OutPrimesList[], size_t ArraySizeBytes)
{
    uint32_t NextPrime = 2;
    uint32_t PrimeCount = 0;
    const size_t ArrayElements = ArraySizeBytes / sizeof(uint32_t);
    uint32_t *NumberList = malloc(PrimeCeiling * sizeof(uint32_t));

    if(NumberList == NULL)
    {
        printf("Memory error!\n");
        return 0;
    }

    memset(OutPrimesList,0,ArraySizeBytes);
    memset(NumberList,0,ArraySizeBytes);

    do
    {
        OutPrimesList[PrimeCount] = NextPrime;
        MarkMultiples(NumberList, ArrayElements, NextPrime);
        NextPrime = FindNextPrime(NumberList, ArrayElements);
        PrimeCount++;
    }while(NextPrime > 0);

    free(NumberList);

    return PrimeCount;
}
예제 #2
0
BoolArray<> GetSieve(const size_t until)
{
	// Initialize the sieve assuming all numbers are prime
	BoolArray<> sieve(GetIndex(until) + 1, true);

	size_t sqrtUntil = SqrtHelper::isqrt(until);
	size_t iSqrtUntil = GetIndex(sqrtUntil);
	for (size_t i = 0; i <= iSqrtUntil; i++)
	{
		// If the current number is prime, mark all multiples of it as being not prime:
		if (sieve[i]) {
			// n is the current number that is considered
			size_t n = GetNumberAtIndex(i);
			//MarkMultiples(sieve, n, until);
			MarkMultiples(sieve, n, i);
		}
	}
	return sieve;
}