diff --git a/Algorithms.hpp b/Algorithms.hpp index 6d37cd4..dca330a 100644 --- a/Algorithms.hpp +++ b/Algorithms.hpp @@ -27,6 +27,7 @@ #include #include #include +#include //This library is licensed under lgplv3 //You can find more information at gmplib.org //In order to use this functionality you must use the -DNEEDGMP flag in your compiler @@ -103,7 +104,8 @@ std::vector getPrimes(T goalNumber){ //We can now start at 3 and skip all of the even numbers for(T possiblePrime = 3;possiblePrime <= goalNumber;possiblePrime += 2){ //Step through every element in the current primes. If you don't find anything that divides it, it must be a prime itself - for(uint64_t cnt = 0;(cnt < primes.size()) && ((primes.at(cnt) * primes.at(cnt)) < goalNumber);++cnt){ + uint64_t topPossibleFactor = ceil(sqrt(possiblePrime)); + for(uint64_t cnt = 0;(cnt < primes.size()) && (primes.at(cnt) <= topPossibleFactor);++cnt){ if((possiblePrime % primes.at(cnt)) == 0){ foundFactor = true; break; @@ -141,7 +143,8 @@ std::vector getNumPrimes(T numberOfPrimes){ //Using possiblePrime >= 3 to make sure it doesn't loop back around in an overflow error and create an infinite loop for(T possiblePrime = 3;(primes.size() < numberOfPrimes) && (possiblePrime >= 3);possiblePrime += 2){ //Step through every element in the current primes. If you don't find anything that divides it, it must be a prime itself - for(uint64_t cnt = 0;(cnt < primes.size()) && ((primes.at(cnt) * primes.at(cnt)) < possiblePrime);++cnt){ + uint64_t topPossibleFactor = ceil(sqrt(possiblePrime)); + for(uint64_t cnt = 0;(cnt < primes.size()) && (primes.at(cnt) <= topPossibleFactor);++cnt){ if((possiblePrime % primes.at(cnt)) == 0){ foundFactor = true; break;