Project Euler – Problem 9 Solution

Yan Cui

I help clients go faster for less using serverless technologies.

Problem

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.

Find the product abc.

Solution

let isPythagoreanTriplet(numbers : int list) =
    match List.sort(numbers) with
    | [a; b; c] -> a*a + b*b = c*c
    | _ –> false

let getTriplets =
    seq {
        for a = 1 to 1000 do
            for b = 1 to 1000 do
                for c = 1 to 1000 do
                    if a + b + c = 1000 then yield [a; b; c]
    }

let pythagoreanTriplet = getTriplets |> Seq.filter isPythagoreanTriplet |> Seq.head
let product = pythagoreanTriplet |> Seq.fold (fun acc x -> acc * x) 1

Here I’ve created a function called isPythagoreanTriplet, which takes a integer list and checks whether it’s a pythagorean triplet. If this is the first time you’ve seen the match …. with syntax, it’s used to match a pattern in F#. The patterns are evaluated from top to bottom, the first pattern decomposes a sorted version of the supplied int list into three values a, b and c, and evaluates whether a, b and c makes a pythagorean triplet:

| [a; b; c] -> a*a + b*b = c*c

If the list does not have exactly three elements, then the next pattern is evaluate. The _ character is the wildcard character and in this case it simply returns false always. e.g.:

isPythagoreanTriplet [3;4;5;0];;
val it : bool = false

isPythagoreanTriplet [];;
val it : bool = false

isPythagoreanTriplet [3;4;5];;
val it : bool = true

The getTriplets function on the hand, supplies a sequence of all triplets which matches the a + b + c = 1000 requirement. Note that I used the yield keyword to produce values that become part of the returned sequence.

The next line of code simply identifies the first (Seq.head) triplet from the above sequence which is a pythagorean triplet.

And finally, the product of the pythagorean triplet is calculated using the Seq.fold function (identical to the function used in the problem 8 solution, which describes the function in more detail).


 

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.

 


2 thoughts on “Project Euler – Problem 9 Solution”

  1. Pingback: Project Euler — Problem 21 Solution | theburningmonk.com

  2. you don’t have to go until 1000 cuz they have to have a sum of 1000 so obviously the first one can’t be more than 1000/3 and second has to be bigger than first but at least -1 smaller than last
    this is my solution:

    let p8 = [for a in [1..1000/3] do
    for b in [(a+1)..(1000-a)/2] do
    let c = 1000 – (a+b);
    if(a*a+b*b = c*c) then yield (a,b,c,a*b*c)]

Leave a Comment

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