Exemplo n.º 1
0
        public SegmentedWheel23(long length)
        {
            Length = length;
            int firstChunkLength            = (int)Math.Sqrt(length) + 1;
            SieveOfEratosthenes sieve       = new SieveOfEratosthenes(firstChunkLength);
            List <long>         firstPrimes = new List <long>();

            sieve.ListPrimes(firstPrimes.Add);
            FirstPrimes            = firstPrimes.Skip(2).ToArray();
            PrimeMultiples_6kPlus1 = new long[FirstPrimes.Length];
            PrimeMultiples_6kPlus5 = new long[FirstPrimes.Length];
            for (int j = 0; j < FirstPrimes.Length; j++)
            {
                long prime = FirstPrimes[j];
                long val1  = prime * prime;
                while (val1 % 6 != 1)
                {
                    val1 += 2 * prime;
                }
                PrimeMultiples_6kPlus1[j] = (val1 - 1) / 6;
                long val2 = prime * prime;
                while (val2 % 6 != 5)
                {
                    val2 += 2 * prime;
                }
                PrimeMultiples_6kPlus5[j] = (val2 - 5) / 6;
            }
        }
Exemplo n.º 2
0
        public SegmentedWheel235(long length)
        {
            Length = length;
            int firstChunkLength            = (int)Math.Sqrt(length) + 1;
            SieveOfEratosthenes sieve       = new SieveOfEratosthenes(firstChunkLength);
            List <long>         firstPrimes = new List <long>();

            sieve.ListPrimes(firstPrimes.Add);
            FirstPrimes    = firstPrimes.Skip(WHEEL_PRIMES_COUNT).ToArray();
            PrimeMultiples = new long[WheelRemainders.Length][];
            for (int i = 0; i < WheelRemainders.Length; i++)
            {
                PrimeMultiples[i] = new long[FirstPrimes.Length];
                for (int j = 0; j < FirstPrimes.Length; j++)
                {
                    long prime = FirstPrimes[j];
                    long val   = prime * prime;
                    while (val % WHEEL != WheelRemainders[i])
                    {
                        val += 2 * prime;
                    }
                    PrimeMultiples[i][j] = (val - WheelRemainders[i]) / WHEEL;
                }
            }
        }
Exemplo n.º 3
0
        public SegmentedSieveOfEratosthenes(long length)
        {
            Length           = length;
            FirstChunkLength = (int)Math.Sqrt(length) + 1;
            SieveOfEratosthenes sieve       = new SieveOfEratosthenes(FirstChunkLength);
            List <long>         firstPrimes = new List <long>();

            sieve.ListPrimes(firstPrimes.Add);
            FirstPrimes = firstPrimes.ToArray();
        }
Exemplo n.º 4
0
        public SegmentedWheel2(long length)
        {
            Length = length;
            int firstChunkLength            = (int)Math.Sqrt(length) + 1;
            SieveOfEratosthenes sieve       = new SieveOfEratosthenes(firstChunkLength);
            List <long>         firstPrimes = new List <long>();

            sieve.ListPrimes(firstPrimes.Add);
            FirstPrimes    = firstPrimes.Skip(1).ToArray();
            PrimeMultiples = FirstPrimes.Select(p => (p * p - 1) / 2).ToArray();
        }
Exemplo n.º 5
0
        public OptimizedSegmentedSieve(long length)
        {
            Length = length;
            int firstChunkLength            = (int)Math.Sqrt(length) + 1;
            SieveOfEratosthenes sieve       = new SieveOfEratosthenes(firstChunkLength);
            List <long>         firstPrimes = new List <long>();

            sieve.ListPrimes(firstPrimes.Add);
            FirstPrimes    = firstPrimes.ToArray();
            PrimeMultiples = FirstPrimes.Select(p => p * p).ToArray();
        }