Project Euler in F# – Problem 25
After a long break, let's try to resume with a regular publication scheme, starting with Project Euler's problem 25, an easy one, which says:
The Fibonacci sequence is defined by the recurrence relation:
Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1.
...
The 12th term, F12, is the first term to contain three digits.
What is the first term in the Fibonacci sequence to contain 1000 digits?
The problem definition also contains the algorithm to solve it, I told you it was easy.
Actually, we just have to write a function to compute the Fibonacci sequence and stop when a term contains a thousand digits.
Since we don't know when we are done evaluating the terms of the sequence we have to generate an infinite sequence and check every new item to see if we can stop.
Let's give a look at the F# code:
#light
open Microsoft.FSharp.Math.BigInt
let has1000Digits (n : Microsoft.FSharp.Math.BigInt) =
n.ToString().Length = 1000
let euler25 =
((1I,1I) |> Seq.unfold
(fun (n0, n1) ->
(if has1000Digits n0 then None
else Some(n0, (n1, n0 + n1))))
|> Seq.length) + 1
The Seq.unfold function is used to generate the infinite sequence, returning Some when we want to continue and None when we are done searching (i.e. the term has 1000 digits).
In the latter case, the length of the sequence represents the number of elements generated before the one with a thousand digits, so we have to sum 1 to get the desired answer.
Even Digits Multiple Of Nine
The mathematical exercise we are going to solve comes from mathschallenge.net and can be summarized in a single sentence:
Find the smallest multiple of nine containing only even digits.
We just need a function to check if a number contains only even digits and then pass to it the multiples of nine.
This algorithm can be translated into the following code:
#light
open System
let isEven n = (n % 2 = 0)
let rec allEvenDigits n =
match n with
| n when n < 10 -> isEven n
| n -> (isEven (n % 10)) && (allEvenDigits (Int32.div n 10))
let nums = 9 |> Seq.unfold (fun i -> Some (i, i + 9))
let answer = Seq.find allEvenDigits nums
The isEven function is just a "shortcut" for the modulus operator and it is used inside allEvenDigits, where it is applied to each digit of the given number.
Then, we have to generate the multiples of nine, but we don't know when to stop, so we need an infinite sequence, that can be produced by the Seq.unfold function.
At the end we use Seq.find, which takes a boolean function (allEvenDigits) and a sequence (nums), and returns the first element of the sequence for which the given function returns "true", i.e. the smallest multiple of nine containing only even digits, that is exactly what we had to find.


