Created
February 17, 2012 23:22
-
-
Save Koitaro/1856098 to your computer and use it in GitHub Desktop.
Erlang : Project Euler 20-29
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(problem20). | |
-include("euler.hrl"). | |
answer() -> lists:sum(i2d(fact(100))). | |
fact(0) -> 1; | |
fact(N) -> N * fact(N-1). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(problem21). | |
-import(prime_factors, [sum_factors/1]). | |
-include("euler.hrl"). | |
answer() -> | |
A = array:map(fun (N,_) -> sum_factors(N) end, array:new(10000)), | |
F = fun (K,V,Acc) when K =< V -> Acc; | |
(K,V,Acc) -> N = array:get(V,A), | |
if K =:= N -> K+V+Acc; true -> Acc end | |
end, | |
array:foldl(F,0,A). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(problem22). | |
-include("euler.hrl"). | |
answer() -> | |
{ok,FD} = file:open("names.txt",read), | |
{ok,S} = file:read_line(FD), | |
file:close(FD), | |
L = [string:strip(W,both,$") || W <- string:tokens(S, ",")], | |
solve(1, lists:sort(L)). | |
solve(_,[]) -> 0; | |
solve(N,[H|T]) -> N*value(H) + solve(N+1, T). | |
value(L) -> lists:foldl(fun(X,Y) -> X-$A+1+Y end, 0, L). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(problem23). | |
-import(prime_factors, [sum_factors/1]). | |
-include("euler.hrl"). | |
answer() -> | |
MAX = 28123, | |
F = fun (N,_) -> | |
case sum_factors(N) of X when X > N -> true; _ -> false end | |
end, | |
Abundants = array:map(F, array:new(MAX+1)), | |
L = lists:seq(1, MAX), | |
L1 = [N || N <- L, array:get(N, Abundants)], | |
lists:sum([N || N <- L, solve(N, L1, Abundants)]). | |
solve(N,[H|_],_) when N < 2*H -> true; | |
solve(N,[H|T],Arr) -> | |
case array:get(N-H,Arr) of true -> false; _ -> solve(N,T,Arr) end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(problem24). | |
-include("euler.hrl"). | |
answer() -> solve(indices(1000000,9), lists:seq(0,9), 0). | |
solve([],[],N) -> N; | |
solve([H|T],L,N) -> solve(T, del_ix(H,L), 10*N+lists:nth(H+1,L)). | |
del_ix(0,[_|T]) -> T; | |
del_ix(N,[H|T]) -> [H] ++ del_ix(N-1,T). | |
indices(_,0) -> [0]; | |
indices(N,X) -> F = fact(X), I = (N-1) div F, [I] ++ indices(N-(F*I), X-1). | |
fact(0) -> 1; fact(N) -> N * fact(N-1). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(problem25). | |
-include("euler.hrl"). | |
answer() -> solve(fib(1,1,1), pow(10,999)). | |
solve({N,A,_},Max) when A > Max -> N; | |
solve({_,_,F},Max) -> solve(F(),Max). | |
fib(N,A,B) -> {N, A, fun() -> fib(N+1,B,B+A) end}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(problem26). | |
-include("euler.hrl"). | |
answer() -> | |
L = [{N,cycle(N)} || N <- lists:seq(2,999)], | |
element(1, lists:last(lists:keysort(2,L))). | |
cycle(N) -> cycle(10,N,[]). | |
cycle(M,N,L) -> | |
X = M rem N, | |
case lists:member(X,L) of | |
true -> length(L); | |
false -> cycle(10*X, N, [X|L]) | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(problem27). | |
-import(primes, [is_prime/1]). | |
-include("euler.hrl"). | |
answer() -> | |
As = [N || N <- lists:seq(-999,999), ?odd(N)], | |
Bs = [N || N <- lists:seq(2,999), is_prime(N)], | |
{_,Answer} = lists:max([{prime_length(0,A,B), A*B} || A <- As, B <- Bs]), | |
Answer. | |
prime_length(N,A,B) -> | |
case is_prime(N*N + A*N + B) of | |
true -> prime_length(N+1,A,B); _ -> N | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(problem28). | |
-include("euler.hrl"). | |
answer() -> 1 + solve(spiral(3)). | |
spiral(N) -> {N, 4*N*N-6*(N-1), fun () -> spiral(N+2) end}. | |
solve({I,_,_}) when I > 1001 -> 0; | |
solve({_,N,F}) -> N + solve(F()). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(problem29). | |
-include("euler.hrl"). | |
answer() -> L = lists:seq(2,100), | |
length(lists:usort([pow(A,B) || A <- L, B <- L])). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment