Skip to content

Project Euler Problem 65

December 22, 2011

Project Euler Problem 65 solution is a breeze. Reading thru this reveals that “the numerator of the third convergent is formed by multiplying the numerator of the second convergent by the third quotient, and adding the numerator of the first convergent”, or symbolically n(i+1) = f(i+1) * n(i) + n(i-1), where n(i) is numerator of i-th convergent and f(i) is i-th member of continued fraction.

Translating above into the code yields:

let problem065 () =
    Seq.initInfinite (fun x -> seq [1I; 2I*(bigint x); 1I])
    |> (Seq.skip 1 >> Seq.collect id >> Seq.skip 1)
    |> Seq.scan (fun (``n(i)``, ``n(i-1)``) i ->
                                    (i * ``n(i)`` + ``n(i-1)``, ``n(i)``))
                                    (3I, 2I)
    |> (Seq.skip 98 >> Seq.head >> fst >> string >>
   (string >> int) >> Seq.sum)

From → Project Euler

One Comment

Trackbacks & Pingbacks

  1. Project Euler Problem 66 « In F# Major

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: