/// <summary> /// /// </summary> /// <param name="endNumber"></param> /// <param name="primeChecker"></param> /// <param name="primeFinder"></param> /// <param name="primeFileManagement"></param> public FindPrimesToGenerator(int endNumber, IIsPrime primeChecker, IFindPrimesTo primeFinder, IFileManager primeFileManagement) : base(0, (ulong)endNumber) { this.PrimeChecker = primeChecker; this.PrimeFinder = primeFinder; this.PrimeFileManagement = primeFileManagement; }
public void FindPrimesTo(IFindPrimesTo finder, IIsPrime primeChecker, int maxNumber) { Stopwatch sw = new Stopwatch(); sw.Restart(); // Generating prime numbers. finder.Execute(maxNumber, primeChecker); ICollection <int> primeNumbers = new List <int>(); sw.Stop(); // Verifying the passed back numbers are prime. if ((primeNumbers != null) && (primeNumbers.Any())) { StringBuilder result = new StringBuilder(); string message = string.Empty; sw.Restart(); message = $"Verification started on {primeNumbers.Count.ToString("#,##0")}"; result.AppendLine(message); bool nonPrimeFound = false; foreach (int prime in primeNumbers) { for (int i = 0; i <= prime; i++) { if ((prime % i) == 0) { break; } } if (nonPrimeFound) { message = $"{prime.ToString("#,##0")} was found to be a non prime number during the verification process."; result.AppendLine(message); //Log(message); //throw new Exception(); break; } } sw.Stop(); message = $"Verification {(nonPrimeFound ? "Failed" : "Passed")} taking {Utilities.ElaspedTimeFormatter(sw)}"; result.AppendLine(message); //Log(message); } }
/// <summary> /// /// </summary> /// <param name="findPrimesToTester"></param> /// <param name="isPrimeTester"></param> /// <param name="numberOfPrimesToFind"></param> /// <returns></returns> protected static ICollection <int> FindPrimesToTimer(IFindPrimesTo findPrimesToTester, IIsPrime isPrimeTester, int numberOfPrimesToFind) { ICollection <int> result = new List <int>(); System.Diagnostics.Debug.WriteLine($"{findPrimesToTester.GetType().Name} : {isPrimeTester.GetType().Name}"); Stopwatch sw = new Stopwatch(); sw.Restart(); findPrimesToTester.Execute(numberOfPrimesToFind, isPrimeTester); result = findPrimesToTester.PrimeNumbers; sw.Stop(); Console.WriteLine($"{DateTime.Now.ToString()} : {findPrimesToTester.GetType().Name} : {isPrimeTester.GetType().Name} : Result = {result.Count.ToString("#,##0")} : Time Taken : {PrimeNumbers.Model.Utilities.ElaspedTimeFormatter(sw.Elapsed)}"); return(result); }
/// <summary> /// /// </summary> /// <param name="numberOfPrimesToFind"></param> /// <param name="isPrimeTester"></param> /// <param name="allowedVersionNumbers"></param> protected static void TestFindPrimesToPerformance(int numberOfPrimesToFind, IIsPrime isPrimeTester, int[] allowedVersionNumbers = null) { Type[] findPrimesToTypes = PerformanceTests.GetTestingTypes("PrimeNumbers.Model", "FindPrimesToV", allowedVersionNumbers); if ((findPrimesToTypes != null) && (findPrimesToTypes.Any())) { Console.WriteLine($"FindPrimesTo Testing with {numberOfPrimesToFind.ToString("#,##0")}"); foreach (Type type in findPrimesToTypes) { IFindPrimesTo findPrimesToTester = (IFindPrimesTo)Activator.CreateInstance(type); PerformanceTests.FindPrimesToTimer(findPrimesToTester, isPrimeTester, numberOfPrimesToFind); } } else { Console.WriteLine("No FindPrimesTo Classes found to test with"); } Console.WriteLine(); }
public FindPrimesToTestBase(IFindPrimesTo tester, IIsPrime primeTester) { this.Tester = tester; this.PrimeTester = primeTester; }