Example #1
0
        private static void BenchmarkPrimalityTests()
        {
            var limit = (int)1e6;

            var sw   = Stopwatch.StartNew();
            var last = PrimeNumberUtil.GetPrimes(new IterativePrimalityTest()).First(x => x > limit);

            sw.Stop();
            var timeIterativePrimalityTest = sw.Elapsed;

            Console.WriteLine($"Iterative test took    {timeIterativePrimalityTest} for {limit} iterations.");

            sw   = Stopwatch.StartNew();
            last = PrimeNumberUtil.GetPrimes(new FermatPrimalityTest()).First(x => x > limit);
            sw.Stop();
            var timeFermatPrimalityTest = sw.Elapsed;

            Console.WriteLine($"Fermat test took       {timeFermatPrimalityTest} for {limit} iterations.");

            sw   = Stopwatch.StartNew();
            last = PrimeNumberUtil.GetPrimes(new MillerRabinPrimalityTest()).First(x => x > limit);
            sw.Stop();
            var timeMillerRabinPrimalityTest = sw.Elapsed;

            Console.WriteLine($"Miller-Rabin test took {timeMillerRabinPrimalityTest} for {limit} iterations.");
        }
Example #2
0
 private static void GetPQ(long minP, long minQ, out long p, out long q)
 {
     p = PrimeNumberUtil.GetPrimes(new FermatPrimalityTest())
         .Where(x => x % 4 == 3)
         .Where(x => x > minP)
         .First();
     q = PrimeNumberUtil.GetPrimes(new FermatPrimalityTest())
         .Where(x => x % 4 == 3)
         .Where(x => x > minQ)
         .First();
 }
Example #3
0
        private static void ComparePrimalityTests()
        {
            const int limit = (int)1e3;

            var iterativeResult = PrimeNumberUtil.GetPrimes(new IterativePrimalityTest())
                                  .TakeWhile(x => x < limit).ToList();
            var fermatResult = PrimeNumberUtil.GetPrimes(new FermatPrimalityTest())
                               .TakeWhile(x => x < limit).ToList();
            var millerRabinResult = PrimeNumberUtil.GetPrimes(new MillerRabinPrimalityTest())
                                    .TakeWhile(x => x < limit).ToList();

            Console.WriteLine($"Iterative Method yielded    {iterativeResult.Count} primes < {limit}");
            Console.WriteLine(String.Join(", ", iterativeResult));
            Console.WriteLine($"Fermat Method yielded       {fermatResult.Count} primes < {limit}");
            Console.WriteLine(String.Join(", ", fermatResult));
            Console.WriteLine($"Miller-Rabin Method yielded {millerRabinResult.Count} primes < {limit}");
            Console.WriteLine(String.Join(", ", millerRabinResult));
            Console.WriteLine("Miller-Rabin is missing: " + String.Join(", ", iterativeResult.Except(millerRabinResult)));
        }