/// <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 Execute(ulong startNumber, ulong endNumber, IIsPrime primeChecker) { if (endNumber + 1 <= 0) { throw new ArgumentException($"Number has be be less than {ulong.MaxValue.ToString("#,##0")}", "number"); } this.StartNumber = startNumber; this.EndNumber = endNumber; this.PrimeChecker = primeChecker; this.PrimeArray = new BitArray((int)(endNumber - startNumber) + 1, true); if (startNumber == 0) { this.PrimeArray[0] = false; // Number 0 = Non-Prime this.PrimeArray[1] = false; // Number 1 = Non-Prime } if (startNumber == 1) { this.PrimeArray[0] = false; // Number 1 = Non-Prime } ulong maxTestNumber = (ulong)Math.Sqrt(endNumber) + 1; Console.WriteLine($"Testing to {maxTestNumber.ToString("#,##0")}"); bool useParallel = false; if (useParallel) { this.ExecuteParallel(); } else { Stopwatch sw = new Stopwatch(); for (ulong i = 2; i <= maxTestNumber; i++) { sw.Restart(); Debug.WriteIf(i < 1000, $"{i} took "); if ((primeChecker.CheckIsPrime((ulong)i))) { ulong diffrence = startNumber % i; ulong loopStartNumber = (i > startNumber ? (i * 2) : (startNumber - diffrence)); for (ulong j = loopStartNumber; j <= endNumber && j > 0; j = j + i) { if (j >= startNumber) { int arrayValue = (int)(j - startNumber); this.PrimeArray[arrayValue] = false; } } } Debug.WriteIf(i < 100, $"{Utilities.ElaspedTimeFormatter(sw.Elapsed)}"); Debug.WriteLineIf(i < 100, ""); } } }
public void Execute(int number, IIsPrime primeChecker) { if (number + 1 < 0) { throw new ArgumentException($"Number has be less than {int.MaxValue.ToString("#,##0")}", "number"); } this.EndNumber = number; bool[] primeArray = new bool[number + 1].Populate(true); primeArray[0] = false; primeArray[1] = false; for (int i = 2; i <= number; i++) { if ((primeArray[i]) && (primeChecker.CheckIsPrime((uint)i))) { //int maxTestNumber = number - (i - 1); //for (int j = i * 2; j <= number; j = j + i) int maxTestNumber = number - (number % i); for (int j = (i + i); j <= maxTestNumber; j = j + i) { primeArray[j] = false; } } } this.PrimeArray = new BitArray(primeArray); }
public void Execute(int number, IIsPrime primeChecker) { if (number + 1 < 0) { throw new ArgumentException($"Number has be be less than {int.MaxValue.ToString("#,##0")}", "number"); } this.EndNumber = number; this.PrimeArray = new BitArray(number + 1, true); this.PrimeArray[0] = false; this.PrimeArray[1] = false; int maxTestNumber = (int)Math.Ceiling(Math.Sqrt(number)) + 1; // Race condition. var loop = Parallel.For(2, maxTestNumber, i => { if ( //(this.PrimeArray[i]) && (primeChecker.CheckIsPrime((uint)i)) ) { int startNumber = (i * 2); for (int j = startNumber; j <= number && j > 0; j = j + i) { // Un-Thread Safe this.PrimeArray[j] = false; } } }); }
public void Execute(int number, IIsPrime primeChecker) { if (number + 1 < 0) { throw new ArgumentException($"Number has be be less than {int.MaxValue.ToString("#,##0")}", "number"); } this.EndNumber = number; this.PrimeArray = new BitArray(number + 1, true); this.PrimeArray[0] = false; this.PrimeArray[1] = false; int maxTestNumber = (int)Math.Sqrt(number) + 1; for (int i = 2; i <= maxTestNumber; i++) { if ((this.PrimeArray[i]) && (primeChecker.CheckIsPrime((uint)i))) { for (int j = (i * 2); j <= number && j > 0; j = j + i) { this.PrimeArray[j] = false; } } } }
public void Execute(ulong startNumber, ulong endNumber, IIsPrime primeChecker) { if (startNumber > endNumber) { throw new ArgumentException($"Start number is greater than the end number"); } if (endNumber + 1 < 0) { throw new ArgumentException($"Number has be be less than {ulong.MaxValue.ToString("#,##0")}", "number"); } if ((endNumber - startNumber) >= (ulong)int.MaxValue) { throw new ArgumentException($"The range between the start and end numbers have to be less than ${int.MaxValue - 1}"); } this.StartNumber = startNumber; this.EndNumber = endNumber; this.PrimeArray = new BitArray((int)(endNumber - startNumber) + 1, true); decimal rangeCalculation = endNumber - startNumber; if (rangeCalculation >= int.MaxValue) { //throw new ArgumentOutOfRangeException() } if (startNumber == 0) { this.PrimeArray[0] = false; // Number 0 = Non-Prime this.PrimeArray[1] = false; // Number 1 = Non-Prime } if (startNumber == 1) { this.PrimeArray[0] = false; // Number 1 = Non-Prime } ulong maxTestNumber = (ulong)Math.Sqrt((ulong)endNumber) + 1; for (ulong i = 2; i <= maxTestNumber; i++) { if ((primeChecker.CheckIsPrime((ulong)i))) { for (ulong j = (i * 2); j <= endNumber && j > 0; j = j + i) { if (j >= startNumber) { int arrayValue = (int)(j - startNumber); this.PrimeArray[arrayValue] = false; } } } } }
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); } }
public ICollection <int> FindPrimesTo(int number, IIsPrime primeChecker) { if (number + 1 < 0) { throw new ArgumentException($"Number has be be less than {int.MaxValue.ToString("#,##0")}", "number"); } this.PrimeArray = new BitArray(number + 1, true); List <int> results = new List <int>(); this.PrimeArray[0] = false; this.PrimeArray[1] = false; int maxTestNumber = (int)Math.Sqrt(number) + 1; for (int i = 2; i <= maxTestNumber; i++) { if ((this.PrimeArray[i]) && (primeChecker.CheckIsPrime((uint)i))) { for (int j = (i * 2); j <= number && j > 0; j = j + i) { this.PrimeArray[j] = false; } } } new PrimeDataV2().SavePrimes(string.Format("{0}-V1_{1}_{2}.txt", this.GetType().Name, number, DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")), this.PrimeArray); new PrimeDataV3().SavePrimes(string.Format("{0}-V2_{1}_{2}.txt", this.GetType().Name, number, DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")), this.PrimeArray); for (int i = 0; i <= number; i++) { if (this.PrimeArray[i]) { results.Add(i); } } return(results); }
public void Execute(int number, IIsPrime primeChecker) { this.EndNumber = number; this.PrimeArray = new BitArray(number, false); for (int i = number; i > 0; --i) { if (primeChecker.CheckIsPrime((uint)i)) { this.PrimeArray[i - 1] = true; } } }
/// <summary> /// Wraps the test with a stopwatch to monitor the time taken to process the requested tester. /// </summary> /// <param name="tester">The class to do the testing with.</param> /// <param name="numberToTest">The number to use with the test.</param> protected static void IsPrimeTimer(IIsPrime tester, ulong numberToTest) { bool result = false; System.Diagnostics.Debug.WriteLine($"{tester.GetType().Name}"); Console.Write($"{DateTime.Now.ToString()} : {tester.GetType().Name} : "); Stopwatch sw = new Stopwatch(); sw.Restart(); result = tester.CheckIsPrime(numberToTest); sw.Stop(); Console.Write($"Result = {result} : Time Taken : {PrimeNumbers.Model.Utilities.ElaspedTimeFormatter(sw.Elapsed)}"); Console.WriteLine(); }
public void Execute(int number, IIsPrime primeChecker) { BitArray results = new BitArray(number + 1, true); this.EndNumber = number; results[0] = false; results[1] = false; for (int i = 2; i <= number; i++) { if (!primeChecker.CheckIsPrime((uint)i)) { results[i] = false; } } this.PrimeArray = results; }
public void Execute(ulong startNumber, ulong endNumber, IIsPrime primeChecker) { if (endNumber + 1 <= 0) { throw new ArgumentException($"Number has be be less than {ulong.MaxValue.ToString("#,##0")}", "number"); } this.PrimeArray = new BitArray((int)(endNumber - startNumber) + 1, true); this.StartNumber = startNumber; this.EndNumber = endNumber; this.PrimeArray = new BitArray((int)(endNumber - startNumber) + 1, true); if (startNumber == 0) { this.PrimeArray[0] = false; // Number 0 = Non-Prime this.PrimeArray[1] = false; // Number 1 = Non-Prime } if (startNumber == 1) { this.PrimeArray[0] = false; // Number 1 = Non-Prime } ulong maxTestNumber = (ulong)Math.Sqrt((ulong)endNumber); for (ulong i = 2; i <= maxTestNumber; i++) { if ((primeChecker.CheckIsPrime((ulong)i))) { ulong diffrence = startNumber % i; ulong loopStartNumber = (i > startNumber ? (i * 2) : (startNumber - diffrence)); for (ulong j = loopStartNumber; j <= endNumber && j > 0; j = j + i) { if (j >= startNumber) { int arrayValue = (int)(j - startNumber); this.PrimeArray[arrayValue] = false; } } } } }
public void Execute(ulong startNumber, ulong endNumber, IIsPrime primeChecker) { this.PrimeArray = new BitArray((int)(endNumber - startNumber) + 1); this.StartNumber = startNumber; this.EndNumber = endNumber; ulong loopNumber = startNumber; if (startNumber == 0) { loopNumber = 1; } for (ulong i = loopNumber; i <= endNumber; i++) { if (primeChecker.CheckIsPrime(i)) { this.PrimeArray[(int)(i - startNumber)] = true; } } }
/// <summary> /// /// </summary> /// <param name="startNumber"></param> /// <param name="endNumber"></param> /// <param name="primeChecker"></param> /// <param name="primeFinder"></param> /// <param name="primeFileManagement"></param> public FindPrimesBetweenGenerator(ulong startNumber, ulong endNumber, IIsPrime primeChecker, IFindPrimesBetween primeFinder, ISavePrimes primeFileManagement) : base(startNumber, endNumber) { if (primeChecker == null) { throw new ArgumentNullException("primeChecker"); } if (primeFinder == null) { throw new ArgumentNullException("primeFinder"); } if (primeFileManagement == null) { throw new ArgumentNullException("primeFileManagement"); } this.PrimeChecker = primeChecker; this.PrimeFinder = primeFinder; this.PrimeFileSaver = primeFileManagement; }
public void Execute(int number, IIsPrime primeChecker) { if (number + 1 < 0) { throw new ArgumentException($"Number has be be less than {int.MaxValue.ToString("#,##0")}", "number"); } this.EndNumber = number; bool[] primeArray = new bool[number + 1]; primeArray.Populate(true); System.Diagnostics.Debug.WriteLine($"Total Memory Used : {GC.GetTotalMemory(false).ToString("#,##0")}"); primeArray[0] = false; primeArray[1] = false; int maxTestNumber = (int)Math.Sqrt(number) + 1; for (int i = 2; i <= maxTestNumber; i++) { if ((primeArray[i]) && (primeChecker.CheckIsPrime((uint)i))) { for (int j = (i * 2); j <= number && j > 0; j = j + i) { primeArray[j] = false; } } } //List<int> results = new List<int>(); //results = primeArray.Select((item, index) => new { item, index }) // .Where(o => o.item) // .Select(o => o.index) // .ToList(); this.PrimeArray = new BitArray(primeArray); }
/// <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(); }
/// <summary> /// /// </summary> /// <param name="numberToTestWith"></param> /// <param name="allowedVersionNumbers"></param> protected static void TestIsPrimePerformance(ulong numberToTestWith, int[] allowedVersionNumbers = null) { Type[] isPrimeTypes = PerformanceTests.GetTestingTypes("PrimeNumbers.Model", "IsPrimeV", allowedVersionNumbers); if ((isPrimeTypes != null) && (isPrimeTypes.Any())) { Console.WriteLine($"IsPrime Testing with {numberToTestWith.ToString("#,##0")}"); foreach (Type type in isPrimeTypes) { IIsPrime isPrimeTester = (IIsPrime)Activator.CreateInstance(type); PerformanceTests.IsPrimeTimer(isPrimeTester, numberToTestWith); } } else { Console.WriteLine("No IsPrime Classes found to test with"); } Console.WriteLine(); }
/// <summary> /// /// </summary> /// <param name="startNumber"></param> /// <param name="endNumber"></param> /// <param name="isPrimeTester"></param> /// <param name="allowedVersionNumbers"></param> protected static void TestFindPrimesBetweenPerformance(ulong startNumber, ulong endNumber, IIsPrime isPrimeTester, int[] allowedVersionNumbers = null) { Type[] findPrimesBetweenTypes = PerformanceTests.GetTestingTypes("PrimeNumbers.Model", "FindPrimesBetweenV", allowedVersionNumbers); if ((findPrimesBetweenTypes != null) && (findPrimesBetweenTypes.Any())) { Console.WriteLine($"FindPrimesBetween Testing with {startNumber.ToString("#,##0")} - {endNumber.ToString("#,##0")}"); foreach (Type type in findPrimesBetweenTypes) { IFindPrimesBetween findPrimesBetweenTester = (IFindPrimesBetween)Activator.CreateInstance(type); PerformanceTests.FindPrimesBetweenTimer(findPrimesBetweenTester, isPrimeTester, startNumber, endNumber); } } else { Console.WriteLine("No FindPrimesBetween Classes found to test with"); } Console.WriteLine(); }
/// <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); }
public ICollection <ulong> FindPrimesBetween(ulong startNumber, ulong endNumber, IIsPrime primeChecker) { if (endNumber + 1 < 0) { throw new ArgumentException($"Number has be be less than {ulong.MaxValue.ToString("#,##0")}", "number"); } this.PrimeArray = new BitArray((int)(endNumber - startNumber) + 1, true); if (startNumber == 0) { this.PrimeArray[0] = false; // Number 0 = Non-Prime this.PrimeArray[1] = false; // Number 1 = Non-Prime } if (startNumber == 1) { this.PrimeArray[0] = false; // Number 1 = Non-Prime } ulong maxTestNumber = (ulong)Math.Sqrt(endNumber) + 1; for (ulong i = 2; i <= maxTestNumber; i++) { if ((primeChecker.CheckIsPrime((ulong)i))) { ulong diffrence = startNumber % i; ulong loopStartNumber = (i > startNumber ? (i * 2) : (startNumber - diffrence)); for (ulong j = loopStartNumber; j <= endNumber && j > 0; j = j + i) { if (j >= startNumber) { int arrayValue = (int)(j - startNumber); this.PrimeArray[arrayValue] = false; } } } } new PrimeDataV2().SavePrimes(string.Format(@"E:\Primes\{0}_V1_{1}-{2}_{3}.txt", this.GetType().Name, startNumber, endNumber, DateTime.Now.ToString("yyyy-MM-dd'T'HH-mm-ss-fffffff")), this.PrimeArray); new PrimeDataV3().SavePrimes(string.Format(@"E:\Primes\{0}_V2_{1}-{2}_{3}.txt", this.GetType().Name, startNumber, endNumber, DateTime.Now.ToString("yyyy-MM-dd'T'HH-mm-ss-fffffff")), this.PrimeArray); List <ulong> results = new List <ulong>(); for (int i = 0; i < this.PrimeArray.Length; i++) { if (this.PrimeArray[i]) { ulong resultValue = startNumber + (uint)i; results.Add(resultValue); } } return(results); }
public FindPrimesToTestBase(IFindPrimesTo tester, IIsPrime primeTester) { this.Tester = tester; this.PrimeTester = primeTester; }
public ICollection <ulong> FindPrimesBetween(ulong startNumber, ulong endNumber, IIsPrime primeChecker) { List <ulong> results = new List <ulong>(); for (int i = 0; i < this.PrimeArray.Length; i++) { if (this.PrimeArray[i]) { ulong resultValue = startNumber + (uint)i; results.Add(resultValue); } } return(results); }
/// <summary> /// /// </summary> /// <param name="tester"></param> /// <param name="trier"></param> /// <param name="startNumber"></param> /// <param name="endNumber"></param> /// <returns></returns> protected static PrimeDataDto FindPrimesBetweenTimer(IFindPrimesBetween tester, IIsPrime trier, ulong startNumber, ulong endNumber) { PrimeDataDto results = null; System.Diagnostics.Debug.WriteLine($"{tester.GetType().Name} : {trier.GetType().Name} : {startNumber.ToString("#,##0")} - {endNumber.ToString("#,##0")}"); Console.Write($"{DateTime.Now.ToString()} : {tester.GetType().Name} : {trier.GetType().Name} : {startNumber.ToString("#,##0")} - {endNumber.ToString("#,##0")} : "); Stopwatch sw = new Stopwatch(); sw.Restart(); tester.Execute(startNumber, endNumber, trier); sw.Stop(); results = tester.Data; //Console.WriteLine($"{DateTime.Now.ToString()} {Environment.NewLine}{tester.GetType().Name} : {trier.GetType().Name}{Environment.NewLine}{startNumber.ToString("#,##0")} - {endNumber.ToString("#,##0")}{Environment.NewLine} Time Taken : {PrimeNumbers.Model.Utilities.ElaspedTimeFormatter(sw.Elapsed)}{Environment.NewLine}Result : {result.Count.ToString("#,##0")}{Environment.NewLine}"); Console.Write($"Result = {results.PrimeNumbers.Count().ToString("#,##0")} : Time Taken : {PrimeNumbers.Model.Utilities.ElaspedTimeFormatter(sw.Elapsed)}"); Console.WriteLine(); return(results); }
public FindPrimesBetweenTestBase(IFindPrimesBetween tester, IIsPrime primeTester) { this.Tester = tester; this.PrimeTester = primeTester; }
public IsPrimeTestBase(IIsPrime tester) { this.Tester = tester; }