Last active
August 19, 2016 23:55
-
-
Save HFahlstedt/57868d0ff983b0071ffa4032037712fc to your computer and use it in GitHub Desktop.
Project Euler in Erlang
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(project_euler). | |
-export([prob1/0, prob2/0, prob3/0, prob4/0, prob5/0, prob6/0, prob7/0, prob8/0, prob9/0]). | |
prob1() -> lists:sum([X || X <- lists:seq(3, 999), (X rem 3 == 0) or (X rem 5 == 0)]). | |
prob2_help(A, B, Sum) when (A+B) > 4000000 -> Sum; | |
prob2_help(A, B, Sum) when (A+B) rem 2 == 0 -> prob2_help(B, A+B, Sum+A+B); | |
prob2_help(A, B, Sum) -> prob2_help(B, A+B, Sum). | |
prob2() -> prob2_help(1, 2, 2). | |
prime_factors(N) -> prime_factors(N, [], 2). | |
prime_factors(N, R, I) when I*I > N -> [N|R]; | |
prime_factors(N, R, I) when N rem I == 0 -> prime_factors(N div I, [I|R], I); | |
prime_factors(N, R, 2) -> prime_factors(N, R, 3); | |
prime_factors(N, R, I) -> prime_factors(N, R, I+2). | |
prob3() -> lists:max(prime_factors(600851475143)). | |
is_palindrome_number(N) -> integer_to_list(N) == lists:reverse(integer_to_list(N)). | |
prob4() -> lists:max([X*Y|| X <- lists:seq(100, 999), Y <- lists:seq(100, 999), is_palindrome_number(X*Y)]). | |
list_product(List) -> lists:foldl(fun(X, Prod) -> X * Prod end, 1, List). | |
prob5() -> list_product(lists:foldl(fun(E, List) -> (E -- List) ++ List end, [], lists:map(fun(X) -> prime_factors(X) end, lists:seq(2, 20)))). | |
sum_of_squares(N) -> N*(N+1)*(2*N+1)/6. | |
square_of_sum(N) -> N*N*(N+1)*(N+1)/4. | |
prob6() -> square_of_sum(100) - sum_of_squares(100). | |
nth_prime(N) -> nth_prime(3, [2], N). | |
nth_prime(_, Primes, 0) -> Primes; | |
nth_prime(X, Primes, N) -> case lists:any(fun(A) -> X rem A == 0 end, Primes) of | |
true -> nth_prime(X+2, Primes, N); | |
false -> nth_prime(X+2, Primes ++ [X], N-1) | |
end. | |
prob7() -> lists:nth(10001, nth_prime(10001)). | |
string_to_integer_list(S) -> lists:map(fun(A) -> A - 48 end, S). | |
max_prod(S) -> max_prod(string_to_integer_list(S), 1, 0). | |
max_prod(List, Index, Max) -> case Index + 13 > length(List) + 1 of | |
true -> Max; | |
false -> max_prod(List, Index + 1, lists:max([Max, list_product(lists:sublist(List, Index, 13))])) | |
end. | |
prob8_digits() -> "731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238793035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450". | |
prob8() -> | |
lists:max( | |
lists:map(fun(B) -> max_prod(B) end, | |
lists:filter(fun(A) -> string:len(A) >= 13 end, | |
string:tokens(prob8_digits(), "0")))). | |
prob9() -> trunc(list_product( | |
tuple_to_list( | |
lists:nth( | |
1, | |
lists:filter( | |
fun({X1, X2, X3}) -> | |
(trunc(X3) == X3) and | |
(X1 + X2 + X3 == 1000) and | |
(X1 < X2) | |
end, | |
[{A, B, math:sqrt(A*A+B*B)}|| A <- lists:seq(3, 332), B <- lists:seq(4, 499)]))))). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment