mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Fixed bug in getPrimes where it would skip adding 2 on odd numbers
This commit is contained in:
@@ -96,13 +96,11 @@ std::vector<T> getPrimes(T goalNumber){
|
||||
std::vector<T> primes;
|
||||
bool foundFactor = false;
|
||||
|
||||
//If the number is 0 or a negative number return an empty vector
|
||||
if(goalNumber < 1){
|
||||
//If the number is 1, 0, or a negative number return an empty vector
|
||||
if(goalNumber <= 1){
|
||||
return primes;
|
||||
}
|
||||
|
||||
//If the number is even 2 is a factor
|
||||
if((goalNumber % 2) == 0){
|
||||
else{
|
||||
primes.push_back(2);
|
||||
}
|
||||
//We can now start at 3 and skip all of the even numbers
|
||||
@@ -134,8 +132,8 @@ std::vector<T> getNumPrimes(T numberOfPrimes){
|
||||
primes.reserve(numberOfPrimes); //Saves cycles later
|
||||
bool foundFactor = false;
|
||||
|
||||
//If the number is 0 or a negative number return an empty vector
|
||||
if(numberOfPrimes < 1){
|
||||
//If the number is 1, 0, or a negative number return an empty vector
|
||||
if(numberOfPrimes <= 1){
|
||||
return primes;
|
||||
}
|
||||
//Otherwise 2 is the first prime number
|
||||
|
||||
Reference in New Issue
Block a user