Skip to content

Instantly share code, notes, and snippets.

View Koitaro's full-sized avatar

Yasusi Koibuti Koitaro

View GitHub Profile
@Koitaro
Koitaro / problem50.erl
Created March 19, 2012 21:01
Erlang : Project Euler 50-59
-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]),
@Koitaro
Koitaro / problem40.erl
Created March 18, 2012 16:57
Erlang : Project Euler 40-49
-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)),
@Koitaro
Koitaro / problem30.erl
Created March 4, 2012 13:05
Erlang : Project Euler 30-39
-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]).
@Koitaro
Koitaro / max_heap.erl
Created February 19, 2012 18:07
Erlang library for Project Euler
-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;
@Koitaro
Koitaro / problem20.erl
Created February 17, 2012 23:22
Erlang : Project Euler 20-29
-module(problem20).
-include("euler.hrl").
answer() -> lists:sum(i2d(fact(100))).
fact(0) -> 1;
fact(N) -> N * fact(N-1).
@Koitaro
Koitaro / problem10.erl
Created February 17, 2012 22:39
Erlang : Project Euler 10-19
-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.
@Koitaro
Koitaro / euler.hrl
Created February 17, 2012 15:32
Erlang : Project Euler 1-9
-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.
@Koitaro
Koitaro / 206.swi
Created January 4, 2012 09:12
CHR : Project Euler 200-299
:- 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]).
@Koitaro
Koitaro / 139.swi
Created October 1, 2011 19:47
CHR : Project Euler 130-139
:- 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).
@Koitaro
Koitaro / 348.swi
Created September 16, 2011 15:49
CHR : Project Euler 300-
:- 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),