Project Euler – Problem 3 Solution

Problem

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

Solution

open System

let findFactorsOf(n:int64) =
    let upperBound = int64(Math.Sqrt(double(n)))
    [2L..upperBound] |> Seq.filter (fun x -> n % x = 0L)

let isPrime(n:int64) = findFactorsOf(n) |> Seq.length = 0

let findMaxPrimeFactorOf(n:int64) =
    let upperBound = int64(Math.Sqrt(double(n)))

    [2L..upperBound]
    |> Seq.filter (fun x -> n % x = 0L)
    |> Seq.filter isPrime
    |> Seq.max

let maxPrime = findMaxPrimeFactorOf(600851475143L)

As this solution requires a bit more work, I created two helper functions findFactorsOf and isPrime, and as you’ve probably noticed, I had explicitly declare that the value n should be a int64 type:

let findFactorsOf(n:int64)

That’s because the number 600851475143 in the problem is outside of the range of the int32 type so all the functions must be able to work with a int64 type. This is one of few cases where you can’t just rely on the type inference in F#.

Another thing you might have noticed is that the syntax for casting in F# is type(value), e.g.

int64(n)

as opposed to the syntax in C#:

(long)n

And did you notice that I used the Math.Sqrt method in my solution? That’s right, you can use any CLR types in F# just as you can in C#! In order to import a namespace in F#, you need to use the open keyword:

open System

which is equivalent to the using directive in C#:

using System

Project Euler – Problem 2 Solution

Problem

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.

Solution

Here’s my solution in F#:

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

let fibTotal =
    fibonacciSeq
    |> 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:

fibonacciSeq
|> Seq.takeWhile (fun n -> n < 4000000)
&#91;/code&#93;

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!

Project Euler – Problem 1 Solution

Introduction

Having spent a bit of time learning the basics of F# I decided to try my hands on actually writing some code and get a better feel of the language and get more used to writing function code in general. And for that purpose, Project Euler provides a great source for small, isolated problems well suited for a functional language like F#.

As of today there are a total of 300 questions, ordered in such a way that they’re progressively more difficult to solve, and whilst I’ll be posting my own answers written in F# you could just as easily solve these problems in a variety of languages.

Problem

So back to the first problem, here’s the brief:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Solution

Here’s a one liner solution in F#:

let total = [1..999] |> List.map (fun i -> if i % 5 = 0 || i % 3 = 0 then i else 0) |> List.sum

Let me break down the solution a little, and go through it step by step.

I started off with [1..999] which is one of many ways you can create and initialize a new list in F#, doing this gives me a list with the values 1, 2, 3, … 998, 999, i.e. all the natural numbers below 1000.

|> is known as the forward pipe operator, it allows you to pass the result of the left side of the operator to the function on the right side. For a C# developer it is perhaps easier to simply think of it as chaining a couple of methods in a Linq query, e.g.

var total = Enumerable.Range(1, 999).Select(x => x % 3 == 0 || x % 5 == 0 ? x : 0).Sum();

The List.map function lets you apply a function to each element of a list and put all the results into a new list, as you’ve seen from the above C# code, it provides the same projection capability you get with the Enumerable.Select method in Linq.

In this particular case, I’m simply saying “for each element in the list, return the element if it’s a multiple of 3 or 5, otherwise return 0”, whilst this does not filter out the elements that are not multiples of 3 or 5 the final List.sum will simply ignore the zeros returned from the previous function.

You could equally use a predicate to filter out the elements which are not multiples of 3 or 5:

let total = [1..999] |> List.filter (fun i -> i % 5 = 0 || i % 3 = 0) |> List.sum

Notice that the List.filter works similar to the Enumerable.Where method in Linq.