Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
33 lines (25 sloc) 766 Bytes
%% Problem
%% ---------------------
%% A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2
%% For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
%%
%% There exists exactly one Pythagorean triplet for which a + b + c = 1000.
%% Find the product abc.
%% ---------------------
-module(p009).
-export([solve/0]).
-include_lib("eunit/include/eunit.hrl").
solve() -> triplets(1000).
%% Solution
%% ---------------------
%% Does not work for big P, i.e. 1,000,000
%% ---------------------
triplets(P) ->
[ {A,B,round(math:sqrt(A*A + B*B))} ||
A <- lists:seq(1, P-2),
B <- lists:seq(A+1, P-1),
P*P =:= 2*(A*P + B*P - A*B)
].
%% Tests
triplets_12_test() ->
?assertEqual([{3,4,5}], triplets(12)).