Skip to main content

Project Euler #009

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

let pythagorean_triplet a b c = (((a |> float) ** 2.0) + ((b |> float) ** 2.0) = ((c |> float) ** 2.0)) && (a + b + c = 1000)

let calc max =
    let data = [1..(max - 2)] |> List.collect (fun x -> [(x + 1)..(max - 1)] |> List.collect (fun y -> [(y + 2)..max] |> List.collect (fun z -> [x :: [y; z]])))
    data |> List.filter (fun x -> pythagorean_triplet x.[0] x.[1] x.[2])

// This takes a lot of time (5 minutes) because of 500
calc 500 |> List.head |> List.fold (fun acc x -> acc * x) 1
comments powered by Disqus