# ndpar/algorithms

Introduced solve function to all modules

1 parent 75e6f24 commit 8b7a0ff2602f69d2b686223a6c564c6bf737cf42 Andrey Paramonov committed Mar 6, 2010
Showing with 45 additions and 51 deletions.
1. +5 −7 p001.erl
2. +11 −12 p002.erl
3. +3 −3 p003.erl
4. +3 −7 p004.erl
5. +4 −2 p005.erl
6. +5 −4 p006.erl
7. +4 −3 p007.erl
8. +2 −4 p008.erl
9. +4 −3 p009.erl
10. +4 −6 p010.erl
 @@ -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()).
 @@ -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). @@ -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)).
 @@ -5,6 +5,7 @@ %% --------------------- -module(p003). +-export([solve/0]). -include_lib("eunit/include/eunit.hrl"). %% Solution @@ -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)); @@ -42,6 +45,3 @@ lpf_20_test() -> lpf_17_test() -> ?assertEqual(17, lpf(17)). - -problem_test() -> - ?assertEqual(6857, lpf(600851475143)).
 @@ -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) -> @@ -32,6 +31,3 @@ is_palindrome_true_test() -> is_palindrome_false_test() -> ?assertEqual(false, is_palindrome(9001)). - -find_test() -> - ?assertEqual(906609, find_largest_palindrome()).
 @@ -7,8 +7,12 @@ %% --------------------- -module(p005). +-export([solve/0]). -include_lib("eunit/include/eunit.hrl"). + +solve() -> find(20). + %% Solution %% --------------------- %% Inspired by: @@ -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 %% ---------------------
 @@ -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]). @@ -38,6 +42,3 @@ diff_10_test() -> diff_100_test() -> ?assertEqual(25164150, diff(100)). - -diff2_100_test() -> - ?assertEqual(25164150, diff2(100)).
 @@ -7,8 +7,12 @@ %% --------------------- -module(p007). +-export([solve/0]). -include_lib("eunit/include/eunit.hrl"). + +solve() -> find_prime(10001). + %% Solution %% --------------------- %% Inspired by: @@ -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/
 @@ -4,6 +4,7 @@ %% --------------------- -module(p008). +-export([solve/0]). -include_lib("eunit/include/eunit.hrl"). %% Solution @@ -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 @@ -46,6 +47,3 @@ fivelets_test() -> prod_test() -> ?assertEqual(120, prod("12345")). - -find_test() -> - ?assertEqual(40824, find()).
 @@ -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 @@ -26,6 +30,3 @@ triplets(P) -> triplets_12_test() -> ?assertEqual([{3,4,5}], triplets(12)). - -triplets_k_test() -> - ?assertEqual([{200,375,425}], triplets(1000)).
 @@ -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)).