Prime Factor
This algorithm finds all prime factors of given number.
/*****Please include following header files*****/
// cmath
// vector
/***********************************************/
/*****Please use following namespaces*****/
// std
/*****************************************/
vector<int> PrimeFactor(int number)
{
vector<int> factors;
while (number % 2 == 0) {
factors.push_back(2);
number /= 2;
}
for (int i = 3; i <= sqrt(number); i += 2) {
while (number % i == 0) {
factors.push_back(i);
number /= i;
}
}
if (number > 2) {
factors.push_back(number);
}
return factors;
}
Example
vector<int> factors = PrimeFactor(488);
Output
2
2
2
61