We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Prime Factorization

Let's solve a commonly misunderstood problem in computer science - finding the prime factors of a number. Almost all modern cryptography, including your browser's HTTPS encryption, is based on the fact that prime factorization is slow.

For now, let's focus on the speed of factorization, and how it relates to P and NP.

Finding a number's prime factors is an NP algorithm.

  • When given two primes and their product, all we need to do is some simple multiplication to verify correctness. (polynomial time)
  • Given a number, finding its prime factors is a much more difficult problem. Exponential time is the best we know of.

The trouble is that no one has formally proven that there is not a polynomial time algorithm for finding prime factors. So, we're technically unsure if the problem is in P or if it's NP-complete.

Either way, let's build it!

The Algorithm

Given a large number, return a list of all the prime factors.

  • prime_factors(8) -> [2, 2, 2]
  • prime_factors(10) -> [2, 5]
  • prime_factors(24) -> [2, 2, 2, 3]

Assignment

Complete the prime_factors function according to the given algorithm. Notice how the algorithm gets much slower as the size of the input (in bits) grows.

The returned list should only contain ints, no floats.