# altuntasfatih's solution

## to Prime Factors in the Elixir Track

Published at May 31 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.

## Example

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!

## Running tests

Execute the tests with:

``````\$ mix test
``````

### Pending tests

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.

## Source

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

## Submitting Incomplete Solutions

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

### prime_factors_test.exs

``````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``````

### test_helper.exs

``````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.

Test results ->
PrimeFactorsTest
* test 4 (0.00ms)
* test 93819012551 (0.3ms)
* test 1 (0.00ms)
* test 2 (0.00ms)
* test 8 (0.00ms)
* test 10000000055 (0.6ms)
* test 901255 (0.00ms)
* test 625 (0.00ms)
* test 3 (0.00ms)
* test 9 (0.00ms)
* test 6 (0.00ms)
* test 27 (0.00ms)

Finished in 0.03 seconds
12 tests, 0 failures
"""
@spec factors_for(pos_integer) :: [pos_integer]
def factors_for(number) do
factors_for(number, 2, [])
|> Enum.reverse()
end

def factors_for(1, _, acc), do: acc

def factors_for(number, factor, acc) when rem(number, factor) == 0 do
number = div(number, factor)
acc = [factor | acc]

if is_prime?(number) do
[number | acc]
else
factors_for(number, factor, acc)
end
end

def factors_for(number, factor, acc), do: factors_for(number, factor + 1, acc)

def is_prime?(2), do: true
def is_prime?(3), do: true

def is_prime?(number) when number > 4 do
limit = :math.sqrt(number) |> trunc
is_prime?(number, 2, limit)
end

def is_prime?(_), do: false

def is_prime?(number, factor, _) when rem(number, factor) == 0, do: false

def is_prime?(number, factor, limit) when factor < limit do
is_prime?(number, factor + 1, limit)
end

def is_prime?(_, _, _), do: true
end``````