Added tests for Sieve of Eratosthenes

This commit is contained in:
2021-06-30 17:14:56 -04:00
parent 83c20463de
commit 279528fe28
2 changed files with 75 additions and 4 deletions

View File

@@ -108,7 +108,9 @@ private:
T possiblePrime; //The next number that could be a prime
std::unordered_map<T, std::vector<T>> compositeMap; //A map to keep track of all of the composite numbers
public:
SieveOfEratosthenes(): possiblePrime(2){}
SieveOfEratosthenes(){
possiblePrime = 2;
}
bool hasNext(){
return true;
}
@@ -124,14 +126,53 @@ public:
//Delete the current entry
compositeMap.erase(possiblePrime);
}
//If the number is a prime
//Save that it is a prime
prime = possiblePrime;
//Add the next entry to the prime
compositeMap[prime * 3].push_back(prime);
//Move on to the next possible prime
possiblePrime += 2;
}
else{
//Return 2 and move to 3
prime = possiblePrime++;
}
return prime;
}
};
template <class T>
class SieveOfEratosthenesAlt{
private:
T possiblePrime; //The next number that could be a prime
std::map<T, std::vector<T>> compositeMap; //A map to keep track of all of the composite numbers
public:
SieveOfEratosthenesAlt(){
possiblePrime = 2;
}
bool hasNext(){
return true;
}
T next(){
T prime;
if(possiblePrime > 2){
//Loop until you find a prime number
for(;compositeMap[possiblePrime].size() > 0;possiblePrime += 2){
//Create the next entry for all entries in the map
for(T num : compositeMap[possiblePrime]){
compositeMap[possiblePrime + num + num].push_back(num);
}
//Delete the current entry
compositeMap.erase(possiblePrime);
}
//Save that it is a prime
prime = possiblePrime;
//Add the next entry to the prime
compositeMap[prime * 3].push_back(prime);
//Move on to the next possible prime
possiblePrime += 2;
}
else{
//Return 2 and move to 3
prime = possiblePrime++;
}
return prime;

View File

@@ -57,6 +57,7 @@ bool testFactorial();
bool testIsPalindrome();
bool testToBin();
bool testPrintVector();
bool testSieveOfEratosthenes();
int main(){
@@ -64,10 +65,10 @@ int main(){
bool passedTest = false;
std::vector<boolFn> functions {testGetPrimes, testGetNumPrimes, testIsPrime, testGetFactors, tetsGetDivisors, testGetSum, testGetProduct, testIsFound, testGetPermutations,
testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax, testFindNumOccurrence, testFactorial, testIsPalindrome, testToBin,
testPrintVector};
testPrintVector, testSieveOfEratosthenes};
std::vector<std::string> names {"getPrimes", "getNumPrimes", "isPrime", "getFactors", "getDivisors", "getSum", "getProduct", "isFound", "getPermutations",
"getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax", "findNumOccurrence", "factorial", "isPalindrome", "toBin",
"testPrintVector"};
"testPrintVector", "testSieveOfEratosthenes"};
//Start doing tests and print out the results of each
for(int cnt = 0;cnt < functions.size();++cnt){
@@ -633,6 +634,35 @@ bool testPrintVector(){
return true;
}
bool testSieveOfEratosthenes(){
//Test 1
mee::SieveOfEratosthenes<int> sieve;
std::vector<int> correctAnswer{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
std::vector<int> answer;
for(int cnt = 0;cnt < 25;++cnt){
int prime = sieve.next();
answer.push_back(prime);
}
if(correctAnswer != answer){
return false;
}
//Test 2
mee::SieveOfEratosthenesAlt<mpz_class> mpzSieve;
std::vector<mpz_class> bigCorrectAnswer{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
std::vector<mpz_class> bigAnswer;
for(int cnt = 0;cnt < 25;++cnt){
mpz_class prime = mpzSieve.next();
bigAnswer.push_back(prime);
}
if(correctAnswer != answer){
return false;
}
//If it hasn't failed a test then return true for passing all the tests
return true;
}
/* Results:
Function getPrimes() passed the test