mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Updated to increase efficiency of both prime number functions
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
#include <cinttypes>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
//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<T> 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<T> 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;
|
||||
|
||||
Reference in New Issue
Block a user