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 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  3. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

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 *