Prime Factors

Ruby

Create a method called prime_factors that accepts a number to perform prime factoring on.

What are the prime factors of 60?

  • Our first divisor is 2. 2 goes into 60, 30 remains.
  • 2 goes into 30, 15 remains.
  • 2 doesn't go cleanly into 15. So let's move on to the next divisor, 3.
  • 3 goes cleanly into 15, 5 remains.
  • 3 does not go cleanly into 5. The next possible factor is 4.
  • 4 does not go cleanly into 5. The next possible factor is 5.
  • 5 does go cleanly into 5.

We are left with a remainder of 1, so we have finished the cycle.

Our successful divisors in that computation are 2, 2, 3, and 5.

Verify this yourself below:

2 * 2 * 3 * 5 = 4 * 15 = 60

Any additional libraries needed for this exercise are already included in the code behind the scenes.

def prime_factors total end
< >