Project Euler in F#: Problem 3

What is the largest prime factor of the number 600851475143 ?

I tried the most naive possible solution, and that solved the problem in less than 1ms, so I didn’t bother optimizing this at all. I can’t help but think that this solution could be written more elegantly using pattern matching, though. Any suggestions?

module Problem3 =
    let rec factors n m ms =
        if n % m = 0L then
            factors (n/m) m (m::ms)
        elif n > m then
            factors n (m+1L) ms
        else
            ms

    let ans = factors 600851475143L 2L [] |> List.head
Advertisements
Project Euler in F#: Problem 3

One thought on “Project Euler in F#: Problem 3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s