Skip to content

Prime Factorization

Assume that we generated prime numbers using the Eratosthenes’s Sieve. If nn is in the “prime-generated” range, we can actually do prime factorization in O(logn)O(\log n). 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 nn, and continue to divide the prime numbers in the array.

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

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