Project Euler – Problem 2 Solution

Check out my new course Learn you some Lambda best practice for great good! and learn the best practices for performance, cost, security, resilience, observability and scalability.


Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

Find the sum of all the even-valued terms in the sequence which do not exceed four million.


Here’s my solution in F#:

let fibonacciSeq = Seq.unfold (fun (current, next) -> Some(current, (next, current + next))) (0, 1)

let fibTotal =
    |> Seq.takeWhile (fun n -> n < 4000000)
    |> Seq.filter (fun n -> n % 2 = 0)
    |> Seq.sum

Here I’ve used a sequence, whilst a sequence is similar to a list or array in F# in that it holds a series of elements, there’s a crucial difference, each sequence element is computed only as required so it provides better performance than a list in situations which not all elements are used. If that sounds familiar to you, that’s because a sequence is basically an IEnumerable<T>!

In the first step of this code I’m building up the fibonacci sequence using the Seq.unfold function which given an initial value, generates a sequence by continuously applying some computation to work out each subsequent element in the sequence:

let fibonacciSeq = Seq.unfold (fun (current, next) -> Some(current, (next, current + next))) (0, 1)

This sequence, if iterated through, will contain all the numbers in the fibonacci sequence to infinity, which is why in the next line I’ve specified that we should take values from the sequence until the value exceeds 4 million:

|> Seq.takeWhile (fun n -> n < 4000000)

The next two lines then identifies and sums all the even numbers in the sequence:

&#91;code lang="fsharp"&#93;
|> Seq.filter (fun n -> n % 2 = 0) // only interested in even numbers
|> Seq.sum // add them up!
Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.
Subscribe to my newsletter

Hi, I’m Yan. I’m an AWS Serverless Hero and the author of Production-Ready Serverless.

I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

Hire me.

Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. Enrol now and enjoy a special preorder price of £9.99 (~$13).

Start Learning

Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!

Find a workshop near you