started challenge 3 with erlang

This commit is contained in:
Dan Ballard 2011-10-20 18:18:37 -07:00
parent 589397355b
commit b9fa965567
1 changed files with 18 additions and 0 deletions

18
e3/e3.erl Normal file
View File

@ -0,0 +1,18 @@
-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).