mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2026-02-04 03:12:27 -05:00
Updated getNumPrimes to be more efficient
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
//myClasses/Algorithms.hpp
|
//myClasses/Algorithms.hpp
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 11-8-18
|
// Created: 11-8-18
|
||||||
//Modified: 1-29-19
|
//Modified: 1-30-19
|
||||||
//This file contains the declarations and implementations to several algoritms that I have found useful
|
//This file contains the declarations and implementations to several algoritms that I have found useful
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2018 Matthew Ellison
|
Copyright (C) 2018 Matthew Ellison
|
||||||
@@ -141,7 +141,7 @@ 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();++cnt){
|
for(uint64_t cnt = 0;(cnt < primes.size()) && ((primes.at(cnt) * primes.at(cnt)) < possiblePrime);++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