This file contains hidden or 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(problem50). | |
| -include("euler.hrl"). | |
| answer() -> | |
| F = fun(L) -> longest(lists:reverse(L)) end, | |
| element(2, lists:max(lists:map(F, tails(primes())))). | |
| longest([]) -> {0,0}; | |
| longest([H|T]) -> | |
| X = lists:sum([H|T]), |
This file contains hidden or 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(problem40). | |
| -include("euler.hrl"). | |
| answer() -> prod([digit(N) || N <- [1,10,100,1000,10000,100000,1000000]]). | |
| digit(N) -> digit(N,0,0). | |
| digit(N,0,0) when N < 10 -> N; | |
| digit(N,D,T) -> | |
| X = 9 * trunc(math:pow(10,D)), |
This file contains hidden or 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(problem30). | |
| -include("euler.hrl"). | |
| answer() -> sum(tree([]), max_digits(1)). | |
| tree([]) -> {[], [fun() -> tree([X]) end || X <- lists:seq(0,9)]}; | |
| tree([H|T]) -> {[H|T], [fun() -> tree([X,H|T]) end || X <- lists:seq(0,H)]}. | |
| sum({L,_}, Max) when length(L) =:= Max -> sum_fifth(L); | |
| sum({L,Fs},Max) -> sum_fifth(L) + lists:sum([sum(F(),Max) || F <- Fs]). |
This file contains hidden or 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(max_heap). | |
| -export([new/0, push/2, pop/1]). | |
| new() -> empty. | |
| push(X,H) -> merge({X,[]}, H). | |
| pop({X,L}) -> {X, lists:foldl(fun merge/2, empty, pair(L))}. | |
| merge(X,empty) -> X; |
This file contains hidden or 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 hidden or 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(problem10). | |
| -include("euler.hrl"). | |
| answer() -> | |
| S = primes(2000000), | |
| try | |
| ets:foldl(fun({X},Y) -> X+Y end, 0, S) | |
| after | |
| ets:delete(S) | |
| end. |
This file contains hidden or 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
| -compile(export_all). | |
| -define(even(X), X rem 2 =:= 0). | |
| -define(odd(X), X rem 2 =/= 0). | |
| main() -> timer:tc(?MODULE, answer, []). | |
| pow(_,0) -> 1; | |
| pow(X,Y) when Y > 0, ?even(Y) -> Z = pow(X, Y div 2), Z*Z; | |
| pow(X,Y) when Y > 0 -> Z = pow(X, Y div 2), X*Z*Z. |
This file contains hidden or 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
| :- use_module(library(chr)). | |
| :- chr_option(optimize, full). | |
| :- chr_type list(T) ---> []; [T|list(T)]. | |
| :- chr_constraint | |
| digit(+int), | |
| num(+int,+list(int)), | |
| answer(+int), | |
| problem206. | |
| problem206 <=> digit(9), num(1,[0]). |
This file contains hidden or 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
| :- use_module(library(chr)). | |
| :- chr_option(optimize, full). | |
| :- chr_constraint triangle(+int,+int,+int), answer(+int), problem139. | |
| problem139 <=> triangle(3,4,5). | |
| next_triangle(A,B,C) :- | |
| X is -A-2*B+2*C, Y is -2*A-B+2*C, Z is -2*A-2*B+3*C, | |
| triangle(X,Y,Z). |
This file contains hidden or 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
| :- use_module(library(chr)). | |
| :- chr_option(optimize, full). | |
| :- chr_type list(T) ---> []; [T|list(T)]. | |
| :- chr_type heap(T) ---> int-list(heap(T)). | |
| :- chr_constraint | |
| heap(+heap(int)), | |
| next(+int,+int,+int), | |
| way(+int), | |
| cube(+int,+int,+int), | |
| diff(+int), |