Published at Jul 07 2020
·
0 comments

Instructions

Test suite

Solution

Compute the prime factors of a given natural number.

A prime number is only evenly divisible by itself and 1.

Note that 1 is not a prime number.

What are the prime factors of 60?

- Our first divisor is 2. 2 goes into 60, leaving 30.
- 2 goes into 30, leaving 15.
- 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3.

- 3 goes cleanly into 15, leaving 5.
- 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're left only with 1, so now, we're done.

Our successful divisors in that computation represent the list of prime factors of 60: 2, 2, 3, and 5.

You can check this yourself:

- 2 * 2 * 3 * 5
- = 4 * 15
- = 60
- Success!

Execute the tests with:

```
$ mix test
```

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by
commenting out the relevant `@tag :pending`

with a `#`

symbol.

For example:

```
# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
```

Or, you can enable all the tests by commenting out the
`ExUnit.configure`

line in the test suite.

```
# ExUnit.configure exclude: :pending, trace: true
```

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

The Prime Factors Kata by Uncle Bob http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
defmodule PrimeFactorsTest do
use ExUnit.Case
# @tag :pending
test "1" do
assert PrimeFactors.factors_for(1) == []
end
@tag :pending
test "2" do
assert PrimeFactors.factors_for(2) == [2]
end
@tag :pending
test "3" do
assert PrimeFactors.factors_for(3) == [3]
end
@tag :pending
test "4" do
assert PrimeFactors.factors_for(4) == [2, 2]
end
@tag :pending
test "6" do
assert PrimeFactors.factors_for(6) == [2, 3]
end
@tag :pending
test "8" do
assert PrimeFactors.factors_for(8) == [2, 2, 2]
end
@tag :pending
test "9" do
assert PrimeFactors.factors_for(9) == [3, 3]
end
@tag :pending
test "27" do
assert PrimeFactors.factors_for(27) == [3, 3, 3]
end
@tag :pending
test "625" do
assert PrimeFactors.factors_for(625) == [5, 5, 5, 5]
end
@tag :pending
test "901255" do
assert PrimeFactors.factors_for(901_255) == [5, 17, 23, 461]
end
@tag :pending
test "93819012551" do
assert PrimeFactors.factors_for(93_819_012_551) == [11, 9539, 894_119]
end
@tag :pending
# @tag timeout: 2000
#
# The timeout tag above will set the below test to fail unless it completes
# in under two sconds. Uncomment it if you want to test the efficiency of your
# solution.
test "10000000055" do
assert PrimeFactors.factors_for(10_000_000_055) == [5, 2_000_000_011]
end
end
```

```
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
```

```
defmodule PrimeFactors do
@doc """
Compute the prime factors for 'number'.
The prime factors are prime numbers that when multiplied give the desired
number.
The prime factors of 'number' will be ordered lowest to highest.
"""
@spec factors_for(pos_integer) :: [pos_integer]
def factors_for(number),
do: prime_factors(number)
# |> find_prime_factors(number, []) |> Enum.reverse()
defp prime_factors(1), do: []
defp prime_factors(num) when rem(num, 2) == 0, do: [2 | prime_factors(div(num, 2))]
defp prime_factors(num) when 4 > num, do: [num]
defp prime_factors(num), do: prime_factors(num, 3)
defp prime_factors(num, next) when rem(num, next) == 0,
do: [next | prime_factors(div(num, next))]
defp prime_factors(num, next) when next + next > num, do: [num]
defp prime_factors(num, next), do: prime_factors(num, next + 2)
# defp factor(prime, {number, factors}) when rem(number, prime) == 0,
# do: factor(prime, {div(number, prime), [prime | factors]})
#
# defp factor(_prime, {number, factors}), do: {number, factors}
#
# def n_primes(count) when count < 1, do: []
#
# def n_primes(count),
# do:
# Stream.iterate(2, &(&1 + 1))
# |> Stream.filter(&prime?/1)
# |> Stream.take(count)
# |> Enum.reduce_while({count, []}, fn prime, {running_number, acc} ->
# IO.inspect(prime)
#
# if running_number > 1,
# do: {:cont, factor(prime, {running_number, acc})},
# else: {:halt, acc}
# end)
#
# defp prime?(2), do: true
# defp prime?(n) when rem(n, 2) == 0, do: false
# defp prime?(n), do: prime?(n, 3)
#
# defp prime?(n, k) when n < k * k, do: true
# defp prime?(n, k) when rem(n, k) == 0, do: false
# defp prime?(n, k), do: prime?(n, k + 2)
end
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,394 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments