Prime Factors

What are prime factors? Every number can be written as a product of prime numbers.

Remember: When you are adding, the result is called the sum. When you are multiplying, the result is called the product. And, prime numbers are numbers that can only be divided by 1 and itself.

For example:

  • 40 = 2 x 2 x 2 x 5
  • 126 = 2 x 3 x 3 x 7
  • 28 = 2 x 2 x 7

How do we write a number as a product of its prime factors?

For example, let's write 24 as a product of its prime factors. There are lots of ways to solve this. Here is one:

  • Start with the smallest prime number that divides into 24, which is 2. So, we can write 24 as: 24 = 2 x 12

    2 divides into all even numbers

  • Now think of the smallest prime number that divides into 12. Again, we can use 2, and write the 12 as 2 x 6. Now we have 24 = 2 x 2 x 6

  • 6 can also be divided by 2 (6 = 2 x 3), so we have 24 = 2 x 2 x 2 x 3

2, 2, 2 and 3 are all prime numbers, so we have our answer.

Write a function to find all prime factors of a number. Return the prime factors in an array with ascending order.

def primeFactors(number)
  # your magic
end

PrimeFactors(24) should return [2, 2, 2, 3].

primeFactors(24)
# => [2, 2, 2, 3]

results matching ""

    No results matching ""