Skip to content

Project Euler Problem 3

October 6, 2011

Project Euler Problem 3 from the select->filter->aggregate pattern standpoint is still fits perfectly:

first we need to factor the original number, and then just take greatest of its prime factors. No select or filter, just make a sequence of prime factors and then aggregate it by finding the max item in this sequence.

Factoring is implemented by the method that is taught in schools.

let primeFactors n =
    let rec factorize n f =
        seq {
        if n <= f then
            yield f
        else
            match n%f with
            | 0L -> yield f; yield! factorize (n/f) f
            | _ -> yield! factorize n (f + 1L)
        }

    factorize n 2L

let problem003 () =
    (primeFactors >> Seq.max) 600851475143L

Going forward solutions are to be in the form of a function taking unit and returning solution answer as a value of type ‘a to be invoked by a shared runner . Also all source code can be accessed now at github.com.

Advertisements

From → Project Euler

One Comment

Trackbacks & Pingbacks

  1. Project Euler Problem 12 « In F# Major

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

%d bloggers like this: