Since it didn't take much time to come up with a solution for problem 1, I decided to do problem 2 today as well. Here is what I came up with:
-module(euler2).
-export([solve/0, solve/1]).
solve() ->
solve(4000000).
solve(Max) ->
solve(Max, 1, 1, 0).
solve(Max, Current, Prev, Total) ->
if
Current > Max ->
Total;
Current rem 2 == 0 ->
solve(Max, Current+Prev, Current, Total+Current);
true ->
solve(Max, Current+Prev, Current, Total)
end.
No comments:
Post a Comment