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; } }
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; } } }
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(); }
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(); }
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(); }