ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Mar 15 2021
·
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!

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3`

is used as it is the official build and
dependency management tool for erlang now. Please refer to the tracks installation
instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

```
$ rebar3 eunit
```

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

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.

```
%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/exercises/prime-factors/canonical-data.json
%% This file is automatically generated from the exercises canonical data.
-module(prime_factors_tests).
-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").
'1_no_factors_test_'() ->
{"no factors",
?_assertEqual(lists:sort([]),
lists:sort(prime_factors:factors(1)))}.
'2_prime_number_test_'() ->
{"prime number",
?_assertEqual(lists:sort([2]),
lists:sort(prime_factors:factors(2)))}.
'3_square_of_a_prime_test_'() ->
{"square of a prime",
?_assertEqual(lists:sort([3, 3]),
lists:sort(prime_factors:factors(9)))}.
'4_cube_of_a_prime_test_'() ->
{"cube of a prime",
?_assertEqual(lists:sort([2, 2, 2]),
lists:sort(prime_factors:factors(8)))}.
'5_product_of_primes_and_non_primes_test_'() ->
{"product of primes and non-primes",
?_assertEqual(lists:sort([2, 2, 3]),
lists:sort(prime_factors:factors(12)))}.
'6_product_of_primes_test_'() ->
{"product of primes",
?_assertEqual(lists:sort([5, 17, 23, 461]),
lists:sort(prime_factors:factors(901255)))}.
'7_factors_include_a_large_prime_test_'() ->
{"factors include a large prime",
?_assertEqual(lists:sort([11, 9539, 894119]),
lists:sort(prime_factors:factors(93819012551)))}.
```

```
-module(prime_factors).
-export([factors/1]).
factors(Value) ->
factors(Value, 2, []).
factors(1, _Factor, Acc) ->
lists:reverse(Acc);
factors(Value, Factor, Acc) when Value rem Factor =:= 0 ->
factors(Value div Factor, Factor, [Factor | Acc]);
factors(Value, Factor, Acc) ->
factors(Value, Factor + 1, Acc).
```

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,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments