Project Euler — Problem 39 Solution

Problem

If p is the perime­ter of a right angle tri­an­gle with inte­gral length sides, {a,b,c}, there are exact­ly three solu­tions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which val­ue of p <= 1000, is the num­ber of solu­tions max­imised?

Solution

// define function to find the number of solutions for p
let countSolutions p =
    [4*p/10..6*p/10]
    |> List.filter (fun c ->
        [1..p]
        |> Seq.takeWhile (fun b -> b + c < p)
        |> Seq.exists (fun b -> (pown (p-b-c) 2 + pown b 2) = pown c 2))
    |> List.length

let answer = [1..1000] |> List.maxBy countSolutions