Skip to content

Instantly share code, notes, and snippets.

@temochka
Last active August 29, 2015 14:06
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 temochka/af5f429de24b87cca566 to your computer and use it in GitHub Desktop.
Save temochka/af5f429de24b87cca566 to your computer and use it in GitHub Desktop.
Solving Project Euler’s problems in Erlang. Most of the proposed solutions are very naive and inefficient and therefore only viable for educational purposes (if there were any other).
-module(euler).
-compile(export_all).
%% 1
%% Find the sum of all the multiples of 3 or 5 below 1000.
problem1() ->
lists:sum([I || I <- lists:seq(0,999), I rem 3 =:= 0 orelse I rem 5 =:= 0]).
%% 2
%% By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
fib(1) -> [1];
fib(2) -> [1,2];
fib(Max) ->
fib(Max - 1, 1, 2, [1]).
fib(Max, _, Next, Seq) when Next > Max ->
lists:reverse(Seq);
fib(Max, Current, Next, Seq) ->
fib(Max, Next, Current + Next, [Next|Seq]).
problem2() ->
lists:sum([I || I <- fib(4000000), I rem 2 =:= 0]).
%% 3
%% What is the largest prime factor of the number 600851475143?
primes_below(Max) ->
primes_below(Max, 3, [2]).
primes_below(Max, _, _) when Max =< 2 ->
[];
primes_below(Max, Current, Primes) when Max =< Current ->
Primes;
primes_below(Max, Current, Primes) ->
case lists:all(fun(I) -> Current rem I > 0 end, Primes) of
false -> primes_below(Max, Current + 1, Primes);
true -> primes_below(Max, Current + 1, [Current|Primes])
end.
problem3() ->
problem3(600851475143).
%% Note: sqrt(Num) is a dirty trick and is mathematically incorrect b/c unusual non-prime numbers are not taken into account.
problem3(Num) ->
Primes = primes_below(trunc(math:sqrt(Num))),
[Max|_] = lists:dropwhile(fun(I) -> Num rem I > 0 end, Primes),
Max.
%% 4
%% Find the largest palindrome made from the product of two 3-digit numbers.
inverse(Num) ->
inverse(Num, 0).
inverse(0, Target) ->
Target;
inverse(Num, Target) ->
inverse(Num div 10, (Target * 10) + Num rem 10).
is_palindrome(Num) ->
Num =:= inverse(Num).
problem4() ->
ThreeDigitNumbers = lists:seq(100, 999),
Products = [X * Y || X <- ThreeDigitNumbers, Y <- ThreeDigitNumbers],
lists:max([I || I <- Products, is_palindrome(I)]).
%% 5
%% What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
is_evenly_divisible_by(Seq, N) ->
lists:all(fun(I) -> N rem I =:= 0 end, Seq).
problem5() ->
problem5(2520).
problem5(N) ->
case is_evenly_divisible_by(lists:seq(1, 20), N) of
false -> problem5(N + 20);
true -> N
end.
%% 6
%% Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
problem6() ->
Seq = lists:seq(0,100),
Sum = lists:sum(Seq),
SquaredSum = Sum * Sum,
SumOfSquares = lists:sum([I * I || I <- Seq]),
SquaredSum - SumOfSquares.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment