August 23, 2024

P36 - Determine the prime factors of a given positive integer (2).

Construct a map containing the prime factors and their multiplicity.

> p36:primeFactorMultiplicity(315).
    #{3 => 2,5 => 1,7 => 1}

erlang

%Determine the prime factors of a given positive integer (2).
-module(p36).
-export([primeFactorMultiplicity/1]).

primeFactorMultiplicity(N) ->
    Ls = p35:primeFactors(N),
    lists:foldl(fun(Elem, Map) -> maps:put(Elem, maps:get(Elem, Map, 0) + 1 , Map) end,#{}, Ls).
Be first to comment
Leave a reply