Skip to content

Commit

Permalink
Introduced solve function to all modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Paramonov committed Mar 6, 2010
1 parent 75e6f24 commit 8b7a0ff
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 51 deletions.
12 changes: 5 additions & 7 deletions p001.erl
Expand Up @@ -7,16 +7,14 @@
%% ---------------------

-module(p001).
-export([solve/0]).
-include_lib("eunit/include/eunit.hrl").


solve() -> lists:sum(multiples35(999)).

multiples35(N) ->
[X || X <- lists:seq(1, N), (X rem 3 =:= 0) or (X rem 5 =:= 0)].
[ X || X <- lists:seq(1, N), (X rem 3 =:= 0) or (X rem 5 =:= 0) ].

multiples35_test() ->
?assertEqual(466, length(multiples35(999))).

result() ->
lists:sum(multiples35(999)).

result_test() ->
?assertEqual(233168, result()).
23 changes: 11 additions & 12 deletions p002.erl
Expand Up @@ -8,13 +8,15 @@
%% ---------------------

-module(p002).
-include_lib("eunit/include/eunit.hrl").
-export([solve1/0, solve2/0, solve3/0]).

%% Solution
%% Solution 1
%% ---------------------
%% Streight forward
%% Basic brute force
%% ---------------------

solve1() -> fib(4000000).

fib(Max) ->
fib(1, 1, Max).

Expand All @@ -28,33 +30,30 @@ fib(Fst, Snd, Max, Acc) when Snd rem 2 =:= 0 ->
fib(Fst, Snd, Max, Acc) ->
fib(Snd, Fst+Snd, Max, Acc).

fib_test() ->
?assertEqual(4613732, fib(4000000)).

%% Solution
%% Solution 2
%% ---------------------
%% Found on Euler forum
%% ---------------------

solve2() -> fse(4000000).

fse(Max) -> fse({1, 1}, Max, 0).

fse({P1, P2}, Max, T) when (P1 + P2) < Max ->
fse({P1 + 2 * P2, 2 * P1 + 3 * P2}, Max, T + P1 + P2);
fse({P1, P2}, Max, T) when (P1 + P2) >= Max -> T.

fse_test() ->
?assertEqual(4613732, fse(4000000)).

%% Solution
%% Solution 3
%% ---------------------
%% 2 8 34 144...
%% E(n) = 4*E(n-1) + E(n-2)
%% ---------------------

solve3() -> f(4000000).

f(M) -> f(2, 8, M, 10).

f(A, B, Max, R) when A + 4*B > Max -> R;
f(A, B, Max, R) -> f(B, A + 4*B, Max, R + A + 4*B).

f_test() ->
?assertEqual(4613732, f(4000000)).
6 changes: 3 additions & 3 deletions p003.erl
Expand Up @@ -5,6 +5,7 @@
%% ---------------------

-module(p003).
-export([solve/0]).
-include_lib("eunit/include/eunit.hrl").

%% Solution
Expand All @@ -13,6 +14,8 @@
%% http://thetaoishere.blogspot.com/2008/05/largest-prime-factor-of-number.html
%% ---------------------

solve() -> lpf(600851475143).

lpf(1) -> 1;
lpf(2) -> 2;
lpf(N) when N rem 2 == 0 -> lpf(erlang:max(2, N div 2));
Expand Down Expand Up @@ -42,6 +45,3 @@ lpf_20_test() ->

lpf_17_test() ->
?assertEqual(17, lpf(17)).

problem_test() ->
?assertEqual(6857, lpf(600851475143)).
10 changes: 3 additions & 7 deletions p004.erl
Expand Up @@ -7,14 +7,13 @@
%% ---------------------

-module(p004).
-export([solve/0]).
-include_lib("eunit/include/eunit.hrl").

%% Solution
%% ---------------------
%% Brute force
%% Brute force solution
%% ---------------------

find_largest_palindrome() ->
solve() ->
lists:max(palindromes(lists:seq(100, 999))).

palindromes(Factors) ->
Expand All @@ -32,6 +31,3 @@ is_palindrome_true_test() ->

is_palindrome_false_test() ->
?assertEqual(false, is_palindrome(9001)).

find_test() ->
?assertEqual(906609, find_largest_palindrome()).
6 changes: 4 additions & 2 deletions p005.erl
Expand Up @@ -7,8 +7,12 @@
%% ---------------------

-module(p005).
-export([solve/0]).
-include_lib("eunit/include/eunit.hrl").


solve() -> find(20).

%% Solution
%% ---------------------
%% Inspired by:
Expand All @@ -28,8 +32,6 @@ lcm(List) -> lists:foldl(fun mymath:lcm/2, 1, List).
find_10_test() ->
?assertEqual(2520, find(10)).

find_20_test() ->
?assertEqual(232792560, find(20)).

%% Solution from bitRAKE
%% ---------------------
Expand Down
9 changes: 5 additions & 4 deletions p006.erl
Expand Up @@ -12,9 +12,13 @@
%% ---------------------

-module(p006).
-export([solve/0]).
-include_lib("eunit/include/eunit.hrl").

%% Solution (long)

solve() -> diff2(100).

%% Solution (slow)
%% ---------------------

diff(Max) -> 2 * lists:sum([M * N || M <- lists:seq(1, Max), N <- lists:seq(1, Max), M < N]).
Expand All @@ -38,6 +42,3 @@ diff_10_test() ->

diff_100_test() ->
?assertEqual(25164150, diff(100)).

diff2_100_test() ->
?assertEqual(25164150, diff2(100)).
7 changes: 4 additions & 3 deletions p007.erl
Expand Up @@ -7,8 +7,12 @@
%% ---------------------

-module(p007).
-export([solve/0]).
-include_lib("eunit/include/eunit.hrl").


solve() -> find_prime(10001).

%% Solution
%% ---------------------
%% Inspired by:
Expand All @@ -33,9 +37,6 @@ find_prime_10_test() ->
upper_bound_estimate_test() ->
?assertEqual(114319, upper_bound_estimate(10001)).

find_prime_10001_test() ->
?assertEqual(104743, find_prime(10001)).


%% See also:
%% http://primes.utm.edu/lists/small/
6 changes: 2 additions & 4 deletions p008.erl
Expand Up @@ -4,6 +4,7 @@
%% ---------------------

-module(p008).
-export([solve/0]).
-include_lib("eunit/include/eunit.hrl").

%% Solution
Expand Down Expand Up @@ -36,7 +37,7 @@ fivelets([A,B,C,D,E|Xs]) -> [[A,B,C,D,E]|fivelets([B,C,D,E|Xs])].

prod(List) -> lists:foldl(fun(X, Prod) -> (X-$0) * Prod end, 1, List).

find() -> lists:max(lists:map(fun prod/1, fivelets(number()))).
solve() -> lists:max(lists:map(fun prod/1, fivelets(number()))).


%% Tests
Expand All @@ -46,6 +47,3 @@ fivelets_test() ->

prod_test() ->
?assertEqual(120, prod("12345")).

find_test() ->
?assertEqual(40824, find()).
7 changes: 4 additions & 3 deletions p009.erl
Expand Up @@ -8,8 +8,12 @@
%% ---------------------

-module(p009).
-export([solve/0]).
-include_lib("eunit/include/eunit.hrl").


solve() -> triplets(1000).

%% Solution
%% ---------------------
%% Does not work for big P, i.e. 1,000,000
Expand All @@ -26,6 +30,3 @@ triplets(P) ->

triplets_12_test() ->
?assertEqual([{3,4,5}], triplets(12)).

triplets_k_test() ->
?assertEqual([{200,375,425}], triplets(1000)).
10 changes: 4 additions & 6 deletions p010.erl
Expand Up @@ -5,12 +5,10 @@
%% Find the sum of all the primes below two million.
%% ---------------------

-module(p010).
-include_lib("eunit/include/eunit.hrl").
-module(p010).
-export([solve/0]).

sum_primes(N) -> lists:sum(mymath:primes_upto(N)).

% Tests
solve() -> sum_primes(2000000).

sum_primes_test() ->
?assertEqual(142913828922, sum_primes(2000000)).
sum_primes(N) -> lists:sum(mymath:primes_upto(N)).

0 comments on commit 8b7a0ff

Please sign in to comment.