🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of shandaman

shandaman's solution

to Prime Factors in the Erlang Track

Published at Apr 07 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.

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

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

Questions?

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.

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_tests.erl

%% 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, isPrime/1, identifyNextPrime/2]).

factors(Value) ->
    factors(Value, [], [2]).

factors(Value, Factors, [NextPrime | _Tail] = Primes) when Value rem NextPrime == 0 ->
    factors(Value div NextPrime, [NextPrime | Factors], Primes);
factors(Value, Factors, [NextPrime | _Tail] = Primes)
    when Value >= NextPrime * NextPrime ->
    factors(Value, Factors, [identifyNextPrime(NextPrime + 1, Value) | Primes]);
factors(1, Factors, _) ->
    Factors;
factors(Value, Factors, _) ->
    [Value | Factors].

identifyNextPrime(Number, Number) ->
    Number;
% identifyNextPrime()
identifyNextPrime(Start, Number) ->
    Identified = isPrime(Start),
    if Identified ->
           Start;
       true ->
           identifyNextPrime(Start + 1, Number)
    end.

isPrime(Number) when Number =< 1 ->
    false;
isPrime(Number) when Number =< 3 ->
    true;
isPrime(Number) when (Number rem 2 == 0) or (Number rem 3 == 0) ->
    false;
isPrime(Number) ->
    isPrime(Number, lists:seq(5, trunc(math:sqrt(Number)) + 1, 6)).

isPrime(_, []) ->
    true;
isPrime(Number, [Head | Tail]) when Number rem Head =/= 0 ->
    isPrime(Number, Tail);
isPrime(_, _) ->
    false.

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?