public bool IsPrimeOptimized(int number) { var timer = Stopwatch.StartNew(); try { if (number < 2) { return(false); } for (int i = 2; i <= Math.Sqrt(number); i++) { if (IsDivisibleBy(number, i)) { return(false); } } return(true); } finally { timer.Stop(); CountLogger.AddDuration(timer.ElapsedMilliseconds); } }
static void Main(string[] args) { int maxNumber = 100000; Console.WriteLine($"Testing primes up to {maxNumber}."); var calculator = new PrimeCalculator(); Console.WriteLine("Naive:"); for (int i = 2; i < maxNumber; i++) { calculator.IsPrimeNaive(i); } Console.WriteLine("Optimized:"); for (int i = 2; i < maxNumber; i++) { calculator.IsPrimeOptimized(i); } CountLogger.DumpResults(Console.WriteLine); }