mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2026-02-04 03:12:27 -05:00
Updated to increase efficiency of both prime number functions
This commit is contained in:
@@ -27,6 +27,7 @@
|
|||||||
#include <cinttypes>
|
#include <cinttypes>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <cmath>
|
||||||
//This library is licensed under lgplv3
|
//This library is licensed under lgplv3
|
||||||
//You can find more information at gmplib.org
|
//You can find more information at gmplib.org
|
||||||
//In order to use this functionality you must use the -DNEEDGMP flag in your compiler
|
//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
|
//We can now start at 3 and skip all of the even numbers
|
||||||
for(T possiblePrime = 3;possiblePrime <= goalNumber;possiblePrime += 2){
|
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
|
//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){
|
if((possiblePrime % primes.at(cnt)) == 0){
|
||||||
foundFactor = true;
|
foundFactor = true;
|
||||||
break;
|
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
|
//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){
|
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
|
//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){
|
if((possiblePrime % primes.at(cnt)) == 0){
|
||||||
foundFactor = true;
|
foundFactor = true;
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user