challenges/e3/e3.erl

19 lines
642 B
Erlang

-module(e3).
-export([e3/0, factor/3]).
% e3 get largest prime factor
% returns list of prime factors
factor(N, Factors, Divisor) when N == 1; Divisor >= N-1 -> %io:format("done ~p/~p\n", [N, Divisor]),
[N]++Factors;
factor(N, Factors, Divisor) when N rem Divisor == 0 -> %io:format("match ~p/~p\n", [N, Divisor]),
Factors++factor(Divisor, [], 2) ++ factor(trunc(N/Divisor), [], 2);
factor(N, Factors, Divisor) -> %{io:format("inc ~p/~p\n", [N, Divisor]),}%
factor(N, Factors, Divisor+1).
max([], Max) -> Max;
max([H|T], Max) when H > Max -> max(T, H);
max([H|T], Max) -> max(T, Max).
e3() -> max(factor(600851475143, [], 2), 0).