What is the best approach to calculating the largest prime factor of a number?
I'm thinking the most efficient would be the following:
- Find lowest prime number that divides cleanly
- Check if result of division is prime
- If not, find next lowest
- Go to 2.
I'm basing this assumption on it being easier to calculate the small prime factors. Is this about right? What other approaches should I look into?
Edit: I've now realised that my approach is futile if there are more than 2 prime factors in play, since step 2 fails when the result is a product of two other primes, therefore a recursive algorithm is needed.
Edit again: And now I've realised that this does still work, because the last found prime number has to be the highest one, therefore any further testing of the non-prime result from step 2 would result in a smaller prime.
1.
find any number that divides clearly (for i = 2 to int(sqr(num)) )2.
divide by that number (num = num/i) and recur until nothing is found in 1.'s interval3.
num is the largest factor – user3819867