Skip to content

Project Euler Problem 39

November 22, 2011

Let’s approach Project Euler Problem 39 by doing few preliminary observations:

p = a + b + c
a*a + b*b = c*c
a + b > c; b + c > a; a + c > b; a<= b < c

a cannot exceed 1/3rd of p; b cannot exceed (p – a)/2

Based on this observations we may come up with a function that for a given value will return number of different pythagorean triples that add to the value. Having such function in hand the rest of the task is trivial.

let pythagoreanTriplesNo perimeter =
    [for a = 1 to perimeter/3 do
        for b = a to (perimeter - a)/2 do
            let c = perimeter - a - b
            if a*a + b*b = c*c then
                yield (a, b, c)]
    |> List.length

let problem039 () =
    |> (fun x -> (x,(pythagoreanTriplesNo x)))
    |> List.maxBy (snd)
    |> (fst)

From → Project Euler

One Comment

Trackbacks & Pingbacks

  1. Project Euler Problem 75 « In F# Major

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: