/// <summary> /// Finds a certain amount of prime numbers based on a starting number using every thread the system has to offer /// </summary> /// <param name="startingNumber">Number from which starts the verification of prime numbers</param> /// <param name="qtdPrimes">Quantity of prime numbers the function will find</param> /// <param name="op">Object that will tell which operations needs to be implemented</param> /// <returns>List of prime numbers between the defined interval</returns> private static List <int> FindPrimesBasedOnNumberParallel(int startingNumber, int qtdPrimes, OperationHandler op) { startingNumber = op.Iterator(startingNumber); // Does not test the startingNumber List <int> primeNumbers = new List <int>(); int start; int end = startingNumber; while (primeNumbers.Count < qtdPrimes) { // Creates a interval to test prime numbers in multiple threads start = end; end = op.Operator(start, qtdPrimes - primeNumbers.Count); // if the "end" is lesser than the "start" then: int startFor; int endFor; if (end < start) { // Reverses the start to the end in order to the Parallel.For to understand the interval startFor = end; endFor = start; } else { startFor = start; endFor = end; } // Prevents testing of negative numbers if (endFor < 0) { endFor = 0; } // If the first number to be tested is 0, then there was not enough prime numbers to find if (startFor == 0) { throw new Exception("Not enough prime numbers to find"); } // Tests each number in a parallel for Parallel.For(startFor, endFor, i => { if (TestaPrimo.TestaPrimo3(i)) { // Creates a lock on primeNumbers to prevent Race Condition lock (primeNumbers) { primeNumbers.Add(i); } } }); } return(primeNumbers); }
/// <summary> /// Finds a certain amount of prime numbers based on a starting number /// </summary> /// <param name="startingNumber">Number from which starts the verification of prime numbers</param> /// <param name="qtdPrimes">Quantity of prime numbers the function will find</param> /// <param name="op">Object that will tell which operations needs to be implemented</param> /// <returns>List of prime numbers between the defined interval</returns> private static List <int> FindPrimesBasedOnNumber(int startingNumber, int qtdPrimes, OperationHandler op) { startingNumber = op.Iterator(startingNumber); // Does not test the startingNumber List <int> primeNumbers = new List <int>(); for (int i = startingNumber; primeNumbers.Count < qtdPrimes; i = op.Iterator(i)) { if (i < 0) { throw new Exception("Not enough prime numbers to find"); } if (TestaPrimo.TestaPrimo3(i)) { primeNumbers.Add(i); } } return(primeNumbers); }