2 // Mono.Math.Prime.Generator.SequentialSearchPrimeGeneratorBase.cs - Prime Generator
7 // Copyright (c) 2003 Ben Maurer. All rights reserved
11 using Mono.Math.Prime;
13 namespace Mono.Math.Prime.Generator {
21 class SequentialSearchPrimeGeneratorBase : PrimeGeneratorBase {
23 protected virtual BigInteger GenerateSearchBase (int bits, object Context)
25 BigInteger ret = BigInteger.genRandom (bits);
31 public override BigInteger GenerateNewPrime (int bits)
33 return GenerateNewPrime (bits, null);
37 public virtual BigInteger GenerateNewPrime (int bits, object Context)
40 // STEP 1. Find a place to do a sequential search
42 BigInteger curVal = GenerateSearchBase (bits, Context);
44 const uint primeProd1 = 3u* 5u * 7u * 11u * 13u * 17u * 19u * 23u * 29u;
46 uint pMod1 = curVal % primeProd1;
48 int DivisionBound = TrialDivisionBounds;
49 uint[] SmallPrimes = BigInteger.smallPrimes;
50 PrimalityTest PostTrialDivisionTest = this.PrimalityTest;
52 // STEP 2. Search for primes
57 // STEP 2.1 Sieve out numbers divisible by the first 9 primes
59 if (pMod1 % 3 == 0) goto biNotPrime;
60 if (pMod1 % 5 == 0) goto biNotPrime;
61 if (pMod1 % 7 == 0) goto biNotPrime;
62 if (pMod1 % 11 == 0) goto biNotPrime;
63 if (pMod1 % 13 == 0) goto biNotPrime;
64 if (pMod1 % 17 == 0) goto biNotPrime;
65 if (pMod1 % 19 == 0) goto biNotPrime;
66 if (pMod1 % 23 == 0) goto biNotPrime;
67 if (pMod1 % 29 == 0) goto biNotPrime;
70 // STEP 2.2 Sieve out all numbers divisible by the primes <= DivisionBound
72 for (int p = 9; p < SmallPrimes.Length && SmallPrimes [p] <= DivisionBound; p++) {
73 if (curVal % SmallPrimes [p] == 0)
78 // STEP 2.3 Is the potential prime acceptable?
80 if (!IsPrimeAcceptable (curVal, Context)) goto biNotPrime;
83 // STEP 2.4 Filter out all primes that pass this step with a primality test
85 if (PrimalityTest (curVal, Confidence)) return curVal;
93 if (pMod1 >= primeProd1) pMod1 -= primeProd1;
98 protected virtual bool IsPrimeAcceptable (BigInteger bi, object Context)