# Project Euler Problem 39

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 () = [3..1000] |> List.map (fun x -> (x,(pythagoreanTriplesNo x))) |> List.maxBy (snd) |> (fst)

Advertisements

## Trackbacks & Pingbacks