예제 #1
0
        public void TestFlip()
        {
            BetterBitArray bitArray = new BetterBitArray(100000);

            for (UInt32 i = 0; i < bitArray.bitLength; i++)
            {
                Assert.IsFalse(bitArray.Get(i));
            }

            for (UInt32 i = 0; i < bitArray.bitLength; i++)
            {
                bitArray.Flip(i);
            }

            for (UInt32 i = 0; i < bitArray.bitLength; i++)
            {
                Assert.IsTrue(bitArray.Get(i));
            }

            for (UInt32 i = 0; i < bitArray.bitLength; i++)
            {
                bitArray.Flip(i);
            }

            for (UInt32 i = 0; i < bitArray.bitLength; i++)
            {
                Assert.IsFalse(bitArray.Get(i));
            }
        }
예제 #2
0
        public static UInt32[] GeneratePrimes(UInt32 max, out UInt32 outPrimeCount)
        {
            if (max < 5)
            {
                throw new ArgumentOutOfRangeException("max", "max can't be too small");
            }
            if (max == UInt32.MaxValue)
            {
                throw new ArgumentOutOfRangeException("max", "max cannot be UInt32.MaxValue");
            }

            UInt32[] primes = new UInt32[PrimeCount.UpperBound(max)];
            UInt32   primeCount;

            UInt32         maxSquareRoot = (UInt32)Math.Sqrt(max);
            BetterBitArray isPrime       = new BetterBitArray(max + 1);

            // put in candidate primes
            for (UInt32 i = 1; i <= maxSquareRoot; i++)
            {
                for (UInt32 j = 1; j <= maxSquareRoot; j++)
                {
                    UInt32 n;

                    n = 4 * i + j;
                    //if (n > max) throw new InvalidOperationException();
                    if (n <= max) // Should always be true for large enough max (maybe take this out0
                    {
                        UInt32 nMod12 = n % 12;
                        if (nMod12 == 1 || nMod12 == 5)
                        {
                            isPrime.Flip(n);
                        }
                    }

                    n = 3 * i + j;
                    //if (n > max) throw new InvalidOperationException();
                    if (n <= max) // Should always be true for large enough max (maybe take this out0
                    {
                        UInt32 nMod12 = n % 12;
                        if (nMod12 == 7)
                        {
                            isPrime.Flip(n);
                        }
                    }

                    if (i > j)
                    {
                        n = 3 * i - j;
                        //if (n > max) throw new InvalidOperationException();
                        if (n <= max) // Should always be true for large enough max (maybe take this out0
                        {
                            UInt32 nMod12 = n % 12;
                            if (nMod12 == 11)
                            {
                                isPrime.Flip(n);
                            }
                        }
                    }
                }
            }

            primes[0]  = 2;
            primes[1]  = 3;
            primeCount = 2;

            UInt32 candidatePrime;

            for (candidatePrime = 5; candidatePrime <= maxSquareRoot; candidatePrime += 2)
            {
                if (isPrime.Get(candidatePrime))
                {
                    //
                    // Mark all multiples of its square as composite
                    //
                    //
                    UInt32 candidatePrimeSquared = candidatePrime * candidatePrime;
                    //UInt32 candidatePrimeSquaredThenDoubled = iSquared * 2;
                    for (UInt32 j = candidatePrimeSquared; j <= max; j += candidatePrimeSquared)
                    {
                        isPrime.Deassert(j);
                    }

                    //
                    // Add to the primes
                    //
                    primes[primeCount++] = candidatePrime;
                }
            }

            // Add the rest of the primes
            for (; candidatePrime <= max; candidatePrime += 2)
            {
                if (isPrime.Get(candidatePrime))
                {
                    primes[primeCount++] = candidatePrime;
                }
            }

            outPrimeCount = primeCount;
            return(primes);
        }