Project Euler – Problem 9 Solution

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).

Enjoy what you’re reading? Subscribe to my newsletter and get more content on AWS and serverless technologies delivered straight to your inbox.


Yan Cui

I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.

You can contact me via Email, Twitter and LinkedIn.

Hire me.


Check out my new course, Complete Guide to AWS Step Functions.

In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, design patterns and best practices.

Get Your Copy