public void LargeSieveIntegerTest(IPrimalityTest p) { for (int i = 0; i <= pg.largestPrime(); i++) { Assert.AreEqual(pg.IsPrime(i), p.IsPrime(i)); } }
public void TestSmallIntegers(IPrimalityTest p) { for (int i = 0; i <= liPrimes[liPrimes.Count - 1]; i++) { Assert.AreEqual(liPrimes.Contains(i), p.IsPrime(i)); Assert.AreEqual(!liPrimes.Contains(i), p.IsComposite(i)); } }
public Program() { fermatTest = new FermatTest(10); millerRabinTest = new MillerRabinTest(10); aksTest = new AKSTest(); fermatPseudoprimeTest = new FermatPseudoprimeTest(2); alreadyCheckedNumbers = new HashSet <int>(); }
private void IntegerTest(IPrimalityTest p) { bool b; for (int i = 0; i < 10000; i++) { b = p.IsPrime(i); } }
private void BigIntegerTest(IPrimalityTest p) { bool bIsPrime; BigInteger bStop = bStop = BigInteger.Pow(10, 30); for (BigInteger b = bStop - 10000; b < bStop; b++) { bIsPrime = p.IsPrime(b); } }
public void ScatteredLongTest(IPrimalityTest p) { foreach (long lTestValue in laScatteredLongPrimes) { Assert.IsTrue(p.IsPrime(lTestValue)); Assert.IsFalse(p.IsComposite(lTestValue)); Assert.IsTrue(p.IsComposite(lTestValue + 1)); Assert.IsFalse(p.IsPrime(lTestValue + 1)); } }
private void PrimeBigIntegerTest(IPrimalityTest p) { bool bIsPrime; BigInteger bPrime; foreach (string s in saTestPrimes) { bPrime = BigInteger.Parse(s); bIsPrime = p.IsPrime(bPrime); } }
private void LongTest(IPrimalityTest p) { bool b; long lStop = int.MaxValue; lStop += 10000; for (long l = lStop - 10000; l < lStop; l++) { b = p.IsPrime(l); } }
public static IEnumerable <long> GetPrimes(IPrimalityTest test) { yield return(2); for (long i = 3; i < Int64.MaxValue; i += 2) { if (test.Test(i)) { yield return(i); } } }
public static Task <bool> IsPrime(this BigInteger source, IPrimalityTest primalityTest) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (primalityTest == null) { throw new ArgumentNullException(nameof(primalityTest)); } return(primalityTest.TestAsync(source)); }
public void FullBattery(IPrimalityTest p) { Ticker t = new Ticker(); Debug.WriteLine("#######"); Debug.WriteLine(p.GetType().Name); Debug.WriteLine("-------"); TestSmallIntegers(p); t.Tick("Small ints"); ScatteredLongTest(p); t.Tick("Scattered Longs"); LargeSieveIntegerTest(p); t.Tick("LargeSieve"); ScatteredBigIntegerTest(p); t.Tick("Scattered Big"); Assert.IsTrue(p.IsPrime(2147483647)); }
public void ScatteredBigIntegerTest(IPrimalityTest p) { BigInteger b; for (int i = 0; i < saTestPrimes.Length; i++) { b = BigInteger.Parse(saTestPrimes[i]); Assert.IsTrue(p.IsPrime(b)); Assert.IsFalse(p.IsComposite(b)); Assert.IsFalse(p.IsPrime(b + 1)); Assert.IsTrue(p.IsComposite(b + 1)); } for (int i = 0; i < saTestPseudoPrimes.Length - 1; i++) { b = BigInteger.Parse(saTestPseudoPrimes[i]); Assert.IsTrue(p.IsComposite(b)); } }
public static async Task <BigInteger> GeneratePrime(uint bitLength, IRandomProvider randomProvider, IPrimalityTest primalityTest) { if (randomProvider == null) { throw new ArgumentNullException(nameof(randomProvider)); } if (bitLength < 8) { throw new ArgumentOutOfRangeException(nameof(bitLength), "Bit length must be at least of length 8."); } var bytes = bitLength / 8; var randomNumberBytes = new byte[bytes]; randomProvider.NextBytes(randomNumberBytes); var result = new BigInteger(randomNumberBytes); if (result.Sign < 0) { result = BigInteger.Negate(result); } if (result.IsEven) { result = result + 1; } while (!await primalityTest.TestAsync(result)) { result = result + 2; } return(result); }
public SieveOrTest(PrimeSieve pg, IPrimalityTest pt) { _pg = pg; _pt = pt; }
public void PrimalityPerformance() { IPrimalityTest[] paTesters = new IPrimalityTest[] { new BailliePSW(), new MillerRabin() }; Stopwatch stopWatch = new Stopwatch(); TimeSpan ts; stopWatch.Start(); long lCount = 0; if (!bTestPerf) { return; } foreach (IPrimalityTest p in paTesters) { Debug.WriteLine("#######"); Debug.WriteLine(p.GetType().Name); Debug.WriteLine("-------"); Debug.Write("Testing Integers"); ts = stopWatch.Elapsed; lCount = 0; while (ts.Seconds < 5) { IntegerTest(p); ts = stopWatch.Elapsed; lCount++; } Debug.WriteLine(": Iterated " + lCount + " times in " + String.Format("{0:00}:{1:00}.{2:00}s", ts.Minutes, ts.Seconds, ts.Milliseconds / 10)); stopWatch.Restart(); Debug.WriteLine("-------"); Debug.Write("Testing Longs"); ts = stopWatch.Elapsed; lCount = 0; while (ts.Seconds < 5) { LongTest(p); ts = stopWatch.Elapsed; lCount++; } Debug.WriteLine(": Iterated " + lCount + " times in " + String.Format("{0:00}:{1:00}.{2:00}s", ts.Minutes, ts.Seconds, ts.Milliseconds / 10)); stopWatch.Restart(); Debug.WriteLine("-------"); Debug.Write("Testing BigIntegers"); ts = stopWatch.Elapsed; lCount = 0; while (ts.Seconds < 5) { BigIntegerTest(p); ts = stopWatch.Elapsed; lCount++; } Debug.WriteLine(": Iterated " + lCount + " times in " + String.Format("{0:00}:{1:00}.{2:00}s", ts.Minutes, ts.Seconds, ts.Milliseconds / 10)); stopWatch.Restart(); Debug.WriteLine("-------"); Debug.Write("Testing BigInteger primes"); ts = stopWatch.Elapsed; lCount = 0; while (ts.Seconds < 5) { PrimeBigIntegerTest(p); ts = stopWatch.Elapsed; lCount++; } Debug.WriteLine(": Iterated " + lCount + " times in " + String.Format("{0:00}:{1:00}.{2:00}s", ts.Minutes, ts.Seconds, ts.Milliseconds / 10)); stopWatch.Restart(); } ts = stopWatch.Elapsed; Debug.Print(String.Format("{0:00}:{1:00}.{2:00}s", ts.Minutes, ts.Seconds, ts.Milliseconds / 10)); stopWatch.Restart(); }
/// <summary> /// Create a primality test using the supplied table of primes and the designated fallback algorithm /// </summary> /// <param name="algorithm">The fallback algorithm to use</param> /// <param name="table">A table of primes</param> public PrecomputedTablePrimalityTest(IPrimalityTest algorithm, PrimeTable table) { m_fallbackAlgorithm = algorithm; m_primeTable = table; }