mirror of
https://github.com/correl/euler.git
synced 2024-11-27 11:09:54 +00:00
Merge branch 'erlang'
This commit is contained in:
commit
ab1b1b95bf
5 changed files with 170 additions and 0 deletions
27
erlang/e001.erl
Normal file
27
erlang/e001.erl
Normal file
|
@ -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)]).
|
52
erlang/e002.erl
Normal file
52
erlang/e002.erl
Normal file
|
@ -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))]).
|
36
erlang/e003.erl
Normal file
36
erlang/e003.erl
Normal file
|
@ -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)]).
|
30
erlang/e004.erl
Normal file
30
erlang/e004.erl
Normal file
|
@ -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)]).
|
25
erlang/e005.erl
Normal file
25
erlang/e005.erl
Normal file
|
@ -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)]).
|
Loading…
Reference in a new issue