Haskell: Project Euler problem 2

by Gregor Uhlenheuer on January 12, 2012

Today’s post features the second problem of Project Euler involving the famous Fibonacci numbers.

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, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

My first attempt used a straight-forward implementation of a single fibonacci number and calculating each one less than four million separately:

Since this solution runs not very efficently I tried to come up with a slightly improved version:

Now after reviewing the written code I notice that it is easily possible to rewrite the fibSeq' function using guards. The resulting function looks more readable to me:

This post is tagged with euler, haskell and programming