Skip to main content

Prime Factorization

Naive Algorithm

Assume that we generated prime numbers using the Eratosthenes's Sieve. If is in the "prime-generated" range, we can actually do prime factorization in . Make another array. While we are doing the Sieve, for composite numbers, put "the first prime that verified that this number is composite" and for prime numbers, put itself. This is easy to implement.

Then we can start with , and continue to divide the prime numbers in the array.

If not, we can just check through all primes less than and divide by those primes until we can't. If all the primes multiply up to , we are done. If not, there is exactly one prime more than that divides .

Pollard's Rho Algorithm

Another algorithm involving prime factorization is Pollard's rho algorithm - since the pseudocode is simple, I'll leave the wikipedia link. https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm#Algorithm