Skip to content

Project Euler Problem 2

September 28, 2011

Solution to Project Euler Problem 2 perfectly fits into the pattern “select->filter->aggregate”. The only twist is that instead of sequence of natural numbers our function solve should accept sequence of Fibonacci numbers.

let inline multipleOf divider number = number % divider = 0
let multipleOf2 = multipleOf 2

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

fibnums
|> solve ((>=) 4000000) multipleOf2 Seq.sum
|> printfn "Problem 2 Answer: %d"

Worked like a charm! :grin:

Also solver aside, the solution would be

let inline multipleOf divider number = number % divider = 0
let multipleOf2 = multipleOf 2

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

let problem002 () =
    fibnums
    |> Seq.takeWhile ((>=) 4000000)
    |> Seq.filter multipleOf2
    |> Seq.sum
About these ads

From → Project Euler

One Comment

Trackbacks & Pingbacks

  1. Project Euler Problem 25 « 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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: