The point to simple factorization by trial division is the most effective solution for factorizing only one number, it does not require any simple testing.
You simply list your possible factors in ascending order and continue to separate them by the amount in question - all of which, in this way, guaranteed factors guarantee that they will be the main ones. Stop when the square of the current coefficient exceeds the current factorized number. See code in user448810 answer .
Usually simple factorization by trial division is faster on primes than on all numbers (or coefficients, etc.), but if you decompose only one number, to find primes first to check their separation, it can cost more than just go ahead with an increase in the flow of possible factors. This listing is O (n), the primary generation is O (n log log n), Sieve of Eratosthenes (SoE) , where n = sqrt (N) for the upper limit of N. With trial division (TD), the complexity is O (n 1,5 / (log n) 2 ) i>.
Of course, asymptotics should be taken as a guide; actual code constants can change the picture. Example, runtime for Haskell code obtained from here and here , factorization 600851475149 (simple):
2.. 0.57 sec 2,3,5,... 0.28 sec 2,3,5,7,11,13,17,19,... 0.21 sec primes, segmented TD 0.65 sec first try 0.05 sec subsequent runs (primes are memoized) primes, list-based SoE 0.44 sec first try 0.05 sec subsequent runs (primes are memoized) primes, array-based SoE 0.15 sec first try 0.06 sec subsequent runs (primes are memoized)
so it depends. Of course, the factorization of the composite number in question, 600851475143, is almost instantaneous, so it doesnβt matter there.