Project Euler – Problem 39 Solution

Yan Cui

I help clients go faster for less using serverless technologies.

Problem

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?

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

 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


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 *