From 279528fe284cf69e953241d52bb37861de0b17ee Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 30 Jun 2021 17:14:56 -0400 Subject: [PATCH] Added tests for Sieve of Eratosthenes --- Algorithms.hpp | 45 +++++++++++++++++++++++++++++++++++++++++++-- testAlgorithms.cpp | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/Algorithms.hpp b/Algorithms.hpp index 3bd0097..c158f8b 100644 --- a/Algorithms.hpp +++ b/Algorithms.hpp @@ -108,7 +108,9 @@ private: T possiblePrime; //The next number that could be a prime std::unordered_map> 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 SieveOfEratosthenesAlt{ +private: + T possiblePrime; //The next number that could be a prime + std::map> 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; diff --git a/testAlgorithms.cpp b/testAlgorithms.cpp index 5bbaecd..a7c2c03 100644 --- a/testAlgorithms.cpp +++ b/testAlgorithms.cpp @@ -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 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 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 sieve; + std::vector 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 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 mpzSieve; + std::vector 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 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