Added getProduct() and fixed bugs in getDivisors()

This commit is contained in:
Matthew Ellison
2019-02-08 23:43:27 -05:00
parent 6a06c2f1aa
commit bae2a3e3fc

View File

@@ -53,9 +53,12 @@ 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);
//This is a function that gets the sum of all elements in a vector and returns the number
//This is a function that returns the sum of all elements in a vector
template <class T>
T getSum(std::vector<T> numbers);
//This is a function that returns the product of all elements in a vector
template <class T>
T getProduct(std::vector<T> nums);
//This is a function that searches a vecter for an element. Returns true if num is found in list
template<class T>
bool isFound(T num, std::vector<T> list);
@@ -204,10 +207,7 @@ std::vector<T> getDivisors(T num){
}
else if(num == 1){
divisors.push_back(1);
}
else{
divisors.push_back(1);
divisors.push_back(num);
return divisors;
}
//You only need to check up to sqrt(num)
T topPossibleDivisor = ceil(sqrt(num));
@@ -229,6 +229,7 @@ std::vector<T> getDivisors(T num){
return divisors;
}
//This is a function that returns the sum of all elements in a vector
template <class T>
T getSum(std::vector<T> numbers){
T sum = 0;
@@ -238,6 +239,16 @@ T getSum(std::vector<T> numbers){
return sum;
}
//This is a function that returns the product of all elmements in a vector
template <class T>
T getProduct(std::vector<T> nums){
T prod = 1;
for(T cnt = 0;cnt < nums.size();++cnt){
prod *= nums.at(cnt);
}
return prod;
}
template<class T>
bool isFound(T num, std::vector<T> list){
for(int cnt = 0;cnt < list.size();++cnt){