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.
In [49]:
// if I go with this approach need to heavily use memoisation
let flattenTriplets triplet =
snd triplet
|> List.map (fun x -> fst triplet, fst x, snd x)
let tuplesAddingTo n =
[0..n]
|> List.map (fun x -> (x, (n-x)))
let tripletsAddingTo n =
[0..n]
|> List.map (fun x -> (x, (tuplesAddingTo (n-x))))
|> List.map flattenTriplets
|> List.concat
let square x = x*x
let isPythagorean (a,b,c) =
(square a) + (square b) = (square c)
let sumTriplet (a,b,c) =
a + b + c
let productTriplet (a,b,c) =
a * b * c
triplesAddingTo 1000
|> List.filter isPythagorean
|> List.filter (fun x -> 1000 = (sumTriplet x))
|> List.map productTriplet
|> List.filter (fun x -> x <> 0)
Out[49]:
In [15]: