Skip to content

Instantly share code, notes, and snippets.

@iain17 iain17/problem1.erl

Last active Apr 8, 2017
Embed
What would you like to do?
Project euler erlang problems.
-module(problem1).
-export([solve/0]).
%Assignment: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
%Find the sum of all the multiples of 3 or 5 below 1000.
solve() ->
solve(1, 0).
solve(Number, Sum) when Number == 1000 ->
Sum;
solve(Number, Sum) when ((Number rem 3) == 0) or ((Number rem 5) == 0) ->
solve(Number + 1, Sum + Number);
solve(Number, Sum) ->
solve(Number + 1, Sum).
-module(problem2).
-export([solve/0]).
%Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
%1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
%By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
solve() ->
solve(1, 2, 0).
solve(_, N1, Sum) when N1 > 4000000 ->
Sum;
solve(N2, N1, Sum) when (N1 rem 2) == 0 -> solve(N1, N2 + N1, Sum + N1);
solve(N2, N1, Sum) -> solve(N1, N2 + N1, Sum).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.