Skip to content

Instantly share code, notes, and snippets.

@monkeygroover
Created May 12, 2016 20:45
Show Gist options
  • Save monkeygroover/ea881fe36ae0806df2e2f2bd6b5f8b98 to your computer and use it in GitHub Desktop.
Save monkeygroover/ea881fe36ae0806df2e2f2bd6b5f8b98 to your computer and use it in GitHub Desktop.
euler27
-module(euler27).
%% API exports
-export([run/0]).
%%====================================================================
%% API functions
%%====================================================================
run() -> lists:foldl(
fun({A, B}, {Longest, Product}) ->
QuadFn = quad(A,B),
RunLength = countConsecutivePrimes(QuadFn),
if RunLength > Longest -> {RunLength, A*B};
RunLength =< Longest -> {Longest, Product}
end
end,
{0, 0},
values()
) .
%%====================================================================
%% Internal functions
%%====================================================================
%%
values() -> [ {X, Y} || X <-lists:seq(-1000,1000), Y <- lists:seq(-1000,1000)].
quad(A,B) -> fun(N) -> N * N + A * N + B end .
countConsecutivePrimes(Fn) -> count_acc(Fn, 0) .
count_acc(QuadFn, N) ->
case primes:is_prime(QuadFn(N)) of
true -> count_acc(QuadFn, N+1);
false -> N
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment