Skip to content

Instantly share code, notes, and snippets.

@iain17
Last active April 8, 2017 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iain17/7b00db0026bd5b04597d5d225a18def8 to your computer and use it in GitHub Desktop.
Save iain17/7b00db0026bd5b04597d5d225a18def8 to your computer and use it in GitHub Desktop.
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