Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of JorgeDS

JorgeDS's solution

to Prime Factors in the Erlang Track

Published at Mar 17 2021 · 0 comments
Test suite

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!

Running tests

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

Submitting Incomplete Solutions

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.



'1_no_factors_test_'() ->
    {"no factors",

'2_prime_number_test_'() ->
    {"prime number",

'3_square_of_a_prime_test_'() ->
    {"square of a prime",
     ?_assertEqual(lists:sort([3, 3]),

'4_cube_of_a_prime_test_'() ->
    {"cube of a prime",
     ?_assertEqual(lists:sort([2, 2, 2]),

'5_product_of_primes_and_non_primes_test_'() ->
    {"product of primes and non-primes",
     ?_assertEqual(lists:sort([2, 2, 3]),

'6_product_of_primes_test_'() ->
    {"product of primes",
     ?_assertEqual(lists:sort([5, 17, 23, 461]),

'7_factors_include_a_large_prime_test_'() ->
    {"factors include a large prime",
     ?_assertEqual(lists:sort([11, 9539, 894119]),


factors(1) -> [];
factors(_Value) -> factors(_Value,2,[]).

factors(_Value, Factor, PFactors) when Factor  > _Value -> PFactors;
factors(_Value, Factor, PFactors) -> case (_Value rem Factor) of
                                       0 -> factors(_Value div Factor,Factor,[Factor | PFactors]);
                                       _ when Factor =:= 2 -> factors(_Value,3,PFactors);
                                       _ -> factors(_Value,Factor + 2,PFactors)

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?