Project Euler – Problem 39 Solution


If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

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

For which value of p <= 1000, is the number of solutions maximised?


// define function to find the number of solutions for p
let countSolutions p =
    |> List.filter (fun c ->
        |> 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


Learn to build Production-Ready Serverless applications

Want to learn how to build Serverless applications and follow best practices? Subscribe to my newsletter and join over 5,000 AWS & Serverless enthusiasts who have signed up already.

1 thought on “Project Euler – Problem 39 Solution”

  1. I have to remind myself that sometimes it’s better to work backwards. As usual, your solution is better than mine :) You started with possible Cs and generated possible As and Bs from that, whereas I started with As and Bs, computed C, and filtered any combination thereof that didn’t look “right” (get it? it’s a triangle joke! I kill me).

    At one point, I must have known that the hypotenuse is between 0.4p and 0.6p, but apparently I’ve forgotten that in the 30 years since I learned it.

Leave a Comment

Your email address will not be published. Required fields are marked *