Project Euler in F#: Problem 9

There exists exactly one Pythagorean triplet for which abc = 1000.
Find the product abc.

module Problem9 =
    let max = 1000
    let isPythagoreanTriple (a,b,c) = a*a + b*b = c*c
    let product (a,b,c) = a*b*c
    let triples = seq{for c in 1 .. max do
                        for a in 1 .. (max - c) do
                            yield (a ,(max - c - a), c)}

    let ans = Seq.find isPythagoreanTriple triples |> product

I think it would be more efficient to do this by generating Pythagorean triples, rather than numbers summing to 1000, but this took all of 10 seconds to write up, and perhaps 10 µs to run, so I’m going to use this solution and move on.

Advertisements
Project Euler in F#: Problem 9

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s