Added a function to get all the prime factos of a number

This commit is contained in:
2019-01-28 13:13:13 -05:00
parent aa5ae520c3
commit 04a1c29a4b

View File

@@ -39,6 +39,9 @@ namespace mee{
//This is a function that returns all the primes <= goalNumber and returns a vector with those prime numbers
template<class T>
std::vector<T> getPrimes(T goalNumber);
//This function returns all prime factors of a number
template<class T>
std::vector<T> getFactors(T goalNumber);
//This is a function that gets all the divisors of num and returns a vector containing the divisors
template<class T>
std::vector<T> getDivisors(T num);
@@ -108,6 +111,34 @@ std::vector<T> getPrimes(T goalNumber){
return primes;
}
template<class T>
std::vector<T> getFactors(T goalNumber){
//Get all the prime numbers up to sqrt(number). If there is a prime < goalNumber it will have to be <= sqrt(goalNumber)
std::vector<T> primes = getPrimes((T)ceil(sqrt(goalNumber))); //Make sure you are getting a vector of the correct type
std::vector<T> factors;
//Need to step through each prime and see if it is a factor of the number
for(int cnt = 0;cnt < primes.size():){
if((goalNumber % primes[cnt]) == 0){
factors.push_back(primes[cnt]);
goalNumber /= primes[cnt];
}
else{
++cnt;
}
}
//If it didn't find any factors in the primes the number itself must be prime
if(factors.size() == 0){
factors.push_back(goalNumber);
goalNumber /= goalNumber;
}
///Should add some kind of error throwing inc ase the number != 1 after searching for all prime factors
return 0;
}
template<class T>
std::vector<T> getDivisors(T num){
std::vector<T> divisors; //Holds the number of divisors