From 4d2e456eceb34a3406a1bd1e71330b3c7d89ad07 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Fri, 23 Sep 2011 22:15:48 -0400 Subject: [PATCH 1/6] Starting an erlang branch, starting with #001 --- e001.erl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 e001.erl diff --git a/e001.erl b/e001.erl new file mode 100644 index 0000000..cb4b917 --- /dev/null +++ b/e001.erl @@ -0,0 +1,27 @@ +% Add all the natural numbers below one thousand that are multiples of 3 or 5. + +% 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. + +-module(e001). +-export([ + main/1 + ]). + +sum(3) -> + 0; +sum(Max) -> + N = Max - 1, + if + N rem 3 == 0 -> + A = N; + N rem 5 == 0 -> + A = N; + true -> + A = 0 + end, + sum(N) + A. + +main(_) -> + io:format("Sum of < 10: ~w~n", [sum(10)]), + io:format("Sum of < 1000: ~w~n", [sum(1000)]). From 1cb2bacbbda2711be3a96f478ce682e44e92d6c9 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Fri, 23 Sep 2011 22:32:58 -0400 Subject: [PATCH 2/6] Excercise 002 --- e002.erl | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 e002.erl diff --git a/e002.erl b/e002.erl new file mode 100644 index 0000000..20b1781 --- /dev/null +++ b/e002.erl @@ -0,0 +1,52 @@ +% Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million. + +% 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, ... +% Find the sum of all the even-valued terms in the sequence which do not exceed four million. + +-module(e002). +-export([ + main/1, + fibonacci_max/1 + ]). + +fibonacci_max(Max) -> + fibonacci_max_([], Max). + +fibonacci_max_([], Max) -> + if + Max < 1 -> + []; + true -> + fibonacci_max_([1], Max) + end; +fibonacci_max_([N], Max) -> + Next = N + N, + if + Max < Next -> + [N]; + true -> + fibonacci_max_([Next, N], Max) + end; +fibonacci_max_([N1, N2 | Seq], Max) -> + Next = N1 + N2, + if + Max < Next -> + [N1, N2 | Seq]; + true -> + fibonacci_max_([Next, N1, N2 | Seq], Max) + end. + +sum_if_even([]) -> + 0; +sum_if_even([N | Seq]) -> + if + N rem 2 == 0 -> + N + sum_if_even(Seq); + true -> + sum_if_even(Seq) + end. + +main(_) -> + Max = 4000000, + io:format("Sum for fibonacci <= ~w: ~w~n", [Max, sum_if_even(fibonacci_max(Max))]). From 625ee578b65986331f92b73fb7ae343b61bd2764 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Fri, 23 Sep 2011 23:56:21 -0400 Subject: [PATCH 3/6] Excercise 003 --- e003.erl | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 e003.erl diff --git a/e003.erl b/e003.erl new file mode 100644 index 0000000..e5dd402 --- /dev/null +++ b/e003.erl @@ -0,0 +1,36 @@ +% Find the largest prime factor of a composite number. + +% The prime factors of 13195 are 5, 7, 13 and 29. +% What is the largest prime factor of the number 600851475143 ? + +-module(e003). +-export([ + main/1, + pfactor/1 +]). + +pfactor(N) -> + pfactor_(N, []). +pfactor_(N, F) -> + Next = pfactor_next(N, 2), + if + Next == N -> + [N | F]; + true -> + pfactor_(trunc(N / Next), [Next | F]) + end. + +pfactor_next(N, Factor) -> + if + Factor == N -> + Factor; + N rem Factor == 0 -> + Factor; + true -> + pfactor_next(N, Factor + 1) + end. + + +main(_) -> + io:format("Prime Factors of 13195: ~w~n", [pfactor(13195)]), + io:format("Prime Factors of 600851475143: ~w~n", [pfactor(600851475143)]). From 5b86e3b1327d3e502411935c7cc1b4667c6b1372 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Sat, 24 Sep 2011 20:48:50 -0400 Subject: [PATCH 4/6] Exercise 004 --- e004.erl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 e004.erl diff --git a/e004.erl b/e004.erl new file mode 100644 index 0000000..2db10f1 --- /dev/null +++ b/e004.erl @@ -0,0 +1,30 @@ +% Find the largest palindrome made from the product of two 3-digit numbers. + +% A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99. +% Find the largest palindrome made from the product of two 3-digit numbers. + +-module(e004). +-export([ + main/1 +]). + +product(Min, Max) -> + product(Min, Max, Max, Max, 0). +product(Min, _, N1, _, Product) when N1 < Min -> + Product; +product(Min, Max, N1, N2, Product) when N2 < Min -> + product(Min, Max, N1 - 1, Max, Product); +product(Min, Max, N1, N2, Product) -> + P = trunc(N1 * N2), + S = integer_to_list(trunc(N1 * N2)), + R = lists:reverse(S), + if + S == R, P > Product -> + product(Min, Max, N1, N2 - 1, P); + true -> + product(Min, Max, N1, N2 - 1, Product) + end. + +main(_) -> + io:format("Largest palindrome product of 2-digit numbers: ~w~n", [product(10, 99)]), + io:format("Largest palindrome product of 3-digit numbers: ~w~n", [product(100, 999)]). From 24feecb51fa63de08d754a926ceae502ba7479ce Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Sat, 24 Sep 2011 21:18:08 -0400 Subject: [PATCH 5/6] Excercise 005 --- e005.erl | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 e005.erl diff --git a/e005.erl b/e005.erl new file mode 100644 index 0000000..bfb8912 --- /dev/null +++ b/e005.erl @@ -0,0 +1,25 @@ +% What is the smallest number divisible by each of the numbers 1 to 20? + +% 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. +% What is the smallest number that is evenly divisible by all of the numbers from 1 to 20? + +-module(e005). +-export([ + main/1 +]). + +divisible(N) -> + divisible(N, N, 1). +divisible(_, C, X) when C == 1 -> + X; +divisible(N, C, X) -> + if + X rem C == 0 -> + divisible(N, C - 1, X); + true -> + divisible(N, N, X + 1) + end. + +main(_) -> + io:format("Smallest number divisible by 1 to 10: ~w~n", [divisible(10)]), + io:format("Smallest number divisible by 1 to 10: ~w~n", [divisible(20)]). From 0f5f741cd6d86d5425282edb8c52cff9bbdd9b5e Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Mon, 10 Oct 2011 22:55:56 -0400 Subject: [PATCH 6/6] Put erlang solutions into their own folder --- e001.erl => erlang/e001.erl | 0 e002.erl => erlang/e002.erl | 0 e003.erl => erlang/e003.erl | 0 e004.erl => erlang/e004.erl | 0 e005.erl => erlang/e005.erl | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename e001.erl => erlang/e001.erl (100%) rename e002.erl => erlang/e002.erl (100%) rename e003.erl => erlang/e003.erl (100%) rename e004.erl => erlang/e004.erl (100%) rename e005.erl => erlang/e005.erl (100%) diff --git a/e001.erl b/erlang/e001.erl similarity index 100% rename from e001.erl rename to erlang/e001.erl diff --git a/e002.erl b/erlang/e002.erl similarity index 100% rename from e002.erl rename to erlang/e002.erl diff --git a/e003.erl b/erlang/e003.erl similarity index 100% rename from e003.erl rename to erlang/e003.erl diff --git a/e004.erl b/erlang/e004.erl similarity index 100% rename from e004.erl rename to erlang/e004.erl diff --git a/e005.erl b/erlang/e005.erl similarity index 100% rename from e005.erl rename to erlang/e005.erl